/src/nghttp2/lib/nghttp2_pq.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * nghttp2 - HTTP/2 C Library |
3 | | * |
4 | | * Copyright (c) 2012 Tatsuhiro Tsujikawa |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining |
7 | | * a copy of this software and associated documentation files (the |
8 | | * "Software"), to deal in the Software without restriction, including |
9 | | * without limitation the rights to use, copy, modify, merge, publish, |
10 | | * distribute, sublicense, and/or sell copies of the Software, and to |
11 | | * permit persons to whom the Software is furnished to do so, subject to |
12 | | * the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be |
15 | | * included in all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21 | | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22 | | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23 | | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | | */ |
25 | | #include "nghttp2_pq.h" |
26 | | |
27 | | #include <stdio.h> |
28 | | #include <assert.h> |
29 | | |
30 | | #include "nghttp2_helper.h" |
31 | | |
32 | 0 | int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem) { |
33 | 0 | pq->mem = mem; |
34 | 0 | pq->capacity = 0; |
35 | 0 | pq->q = NULL; |
36 | 0 | pq->length = 0; |
37 | 0 | pq->less = less; |
38 | 0 | return 0; |
39 | 0 | } |
40 | | |
41 | 0 | void nghttp2_pq_free(nghttp2_pq *pq) { |
42 | 0 | nghttp2_mem_free(pq->mem, pq->q); |
43 | 0 | pq->q = NULL; |
44 | 0 | } |
45 | | |
46 | 0 | static void swap(nghttp2_pq *pq, size_t i, size_t j) { |
47 | 0 | nghttp2_pq_entry *a = pq->q[i]; |
48 | 0 | nghttp2_pq_entry *b = pq->q[j]; |
49 | |
|
50 | 0 | pq->q[i] = b; |
51 | 0 | b->index = i; |
52 | 0 | pq->q[j] = a; |
53 | 0 | a->index = j; |
54 | 0 | } |
55 | | |
56 | 0 | static void bubble_up(nghttp2_pq *pq, size_t index) { |
57 | 0 | size_t parent; |
58 | 0 | while (index != 0) { |
59 | 0 | parent = (index - 1) / 2; |
60 | 0 | if (!pq->less(pq->q[index], pq->q[parent])) { |
61 | 0 | return; |
62 | 0 | } |
63 | 0 | swap(pq, parent, index); |
64 | 0 | index = parent; |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | 0 | int nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item) { |
69 | 0 | if (pq->capacity <= pq->length) { |
70 | 0 | void *nq; |
71 | 0 | size_t ncapacity; |
72 | |
|
73 | 0 | ncapacity = nghttp2_max(4, (pq->capacity * 2)); |
74 | |
|
75 | 0 | nq = nghttp2_mem_realloc(pq->mem, pq->q, |
76 | 0 | ncapacity * sizeof(nghttp2_pq_entry *)); |
77 | 0 | if (nq == NULL) { |
78 | 0 | return NGHTTP2_ERR_NOMEM; |
79 | 0 | } |
80 | 0 | pq->capacity = ncapacity; |
81 | 0 | pq->q = nq; |
82 | 0 | } |
83 | 0 | pq->q[pq->length] = item; |
84 | 0 | item->index = pq->length; |
85 | 0 | ++pq->length; |
86 | 0 | bubble_up(pq, pq->length - 1); |
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | 0 | nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) { |
91 | 0 | if (pq->length == 0) { |
92 | 0 | return NULL; |
93 | 0 | } else { |
94 | 0 | return pq->q[0]; |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | 0 | static void bubble_down(nghttp2_pq *pq, size_t index) { |
99 | 0 | size_t i, j, minindex; |
100 | 0 | for (;;) { |
101 | 0 | j = index * 2 + 1; |
102 | 0 | minindex = index; |
103 | 0 | for (i = 0; i < 2; ++i, ++j) { |
104 | 0 | if (j >= pq->length) { |
105 | 0 | break; |
106 | 0 | } |
107 | 0 | if (pq->less(pq->q[j], pq->q[minindex])) { |
108 | 0 | minindex = j; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | if (minindex == index) { |
112 | 0 | return; |
113 | 0 | } |
114 | 0 | swap(pq, index, minindex); |
115 | 0 | index = minindex; |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | 0 | void nghttp2_pq_pop(nghttp2_pq *pq) { |
120 | 0 | if (pq->length > 0) { |
121 | 0 | pq->q[0] = pq->q[pq->length - 1]; |
122 | 0 | pq->q[0]->index = 0; |
123 | 0 | --pq->length; |
124 | 0 | bubble_down(pq, 0); |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) { |
129 | 0 | assert(pq->q[item->index] == item); |
130 | | |
131 | 0 | if (item->index == 0) { |
132 | 0 | nghttp2_pq_pop(pq); |
133 | 0 | return; |
134 | 0 | } |
135 | | |
136 | 0 | if (item->index == pq->length - 1) { |
137 | 0 | --pq->length; |
138 | 0 | return; |
139 | 0 | } |
140 | | |
141 | 0 | pq->q[item->index] = pq->q[pq->length - 1]; |
142 | 0 | pq->q[item->index]->index = item->index; |
143 | 0 | --pq->length; |
144 | |
|
145 | 0 | if (pq->less(item, pq->q[item->index])) { |
146 | 0 | bubble_down(pq, item->index); |
147 | 0 | } else { |
148 | 0 | bubble_up(pq, item->index); |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | 0 | int nghttp2_pq_empty(nghttp2_pq *pq) { return pq->length == 0; } |
153 | | |
154 | 0 | size_t nghttp2_pq_size(nghttp2_pq *pq) { return pq->length; } |
155 | | |
156 | 0 | void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) { |
157 | 0 | size_t i; |
158 | 0 | int rv = 0; |
159 | 0 | if (pq->length == 0) { |
160 | 0 | return; |
161 | 0 | } |
162 | 0 | for (i = 0; i < pq->length; ++i) { |
163 | 0 | rv |= (*fun)(pq->q[i], arg); |
164 | 0 | } |
165 | 0 | if (rv) { |
166 | 0 | for (i = pq->length; i > 0; --i) { |
167 | 0 | bubble_down(pq, i - 1); |
168 | 0 | } |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 0 | int nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) { |
173 | 0 | size_t i; |
174 | |
|
175 | 0 | if (pq->length == 0) { |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 | for (i = 0; i < pq->length; ++i) { |
179 | 0 | if ((*fun)(pq->q[i], arg)) { |
180 | 0 | return 1; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | return 0; |
184 | 0 | } |