/src/nghttp2/lib/nghttp2_pq.c
Line | Count | Source |
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 | 99.7k | void nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem) { |
33 | 99.7k | *pq = (nghttp2_pq){ |
34 | 99.7k | .mem = mem, |
35 | 99.7k | .less = less, |
36 | 99.7k | }; |
37 | 99.7k | } |
38 | | |
39 | 99.7k | void nghttp2_pq_free(nghttp2_pq *pq) { |
40 | 99.7k | nghttp2_mem_free(pq->mem, pq->q); |
41 | 99.7k | pq->q = NULL; |
42 | 99.7k | } |
43 | | |
44 | 0 | static void swap(nghttp2_pq *pq, size_t i, size_t j) { |
45 | 0 | nghttp2_pq_entry *a = pq->q[i]; |
46 | 0 | nghttp2_pq_entry *b = pq->q[j]; |
47 | |
|
48 | 0 | pq->q[i] = b; |
49 | 0 | b->index = i; |
50 | 0 | pq->q[j] = a; |
51 | 0 | a->index = j; |
52 | 0 | } |
53 | | |
54 | 0 | static void bubble_up(nghttp2_pq *pq, size_t index) { |
55 | 0 | size_t parent; |
56 | 0 | while (index != 0) { |
57 | 0 | parent = (index - 1) / 2; |
58 | 0 | if (!pq->less(pq->q[index], pq->q[parent])) { |
59 | 0 | return; |
60 | 0 | } |
61 | 0 | swap(pq, parent, index); |
62 | 0 | index = parent; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 0 | int nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item) { |
67 | 0 | if (pq->capacity <= pq->length) { |
68 | 0 | void *nq; |
69 | 0 | size_t ncapacity; |
70 | |
|
71 | 0 | ncapacity = nghttp2_max_size(4, (pq->capacity * 2)); |
72 | |
|
73 | 0 | nq = nghttp2_mem_realloc(pq->mem, pq->q, |
74 | 0 | ncapacity * sizeof(nghttp2_pq_entry *)); |
75 | 0 | if (nq == NULL) { |
76 | 0 | return NGHTTP2_ERR_NOMEM; |
77 | 0 | } |
78 | 0 | pq->capacity = ncapacity; |
79 | 0 | pq->q = nq; |
80 | 0 | } |
81 | 0 | pq->q[pq->length] = item; |
82 | 0 | item->index = pq->length; |
83 | 0 | ++pq->length; |
84 | 0 | bubble_up(pq, pq->length - 1); |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | 199k | nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) { |
89 | 199k | if (pq->length == 0) { |
90 | 199k | return NULL; |
91 | 199k | } else { |
92 | 0 | return pq->q[0]; |
93 | 0 | } |
94 | 199k | } |
95 | | |
96 | 0 | static void bubble_down(nghttp2_pq *pq, size_t index) { |
97 | 0 | size_t i, j, minindex; |
98 | 0 | for (;;) { |
99 | 0 | j = index * 2 + 1; |
100 | 0 | minindex = index; |
101 | 0 | for (i = 0; i < 2; ++i, ++j) { |
102 | 0 | if (j >= pq->length) { |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | if (pq->less(pq->q[j], pq->q[minindex])) { |
106 | 0 | minindex = j; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | if (minindex == index) { |
110 | 0 | return; |
111 | 0 | } |
112 | 0 | swap(pq, index, minindex); |
113 | 0 | index = minindex; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | void nghttp2_pq_pop(nghttp2_pq *pq) { |
118 | 0 | if (pq->length > 0) { |
119 | 0 | pq->q[0] = pq->q[pq->length - 1]; |
120 | 0 | pq->q[0]->index = 0; |
121 | 0 | --pq->length; |
122 | 0 | bubble_down(pq, 0); |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) { |
127 | 0 | assert(pq->q[item->index] == item); |
128 | |
|
129 | 0 | if (item->index == 0) { |
130 | 0 | nghttp2_pq_pop(pq); |
131 | 0 | return; |
132 | 0 | } |
133 | | |
134 | 0 | if (item->index == pq->length - 1) { |
135 | 0 | --pq->length; |
136 | 0 | return; |
137 | 0 | } |
138 | | |
139 | 0 | pq->q[item->index] = pq->q[pq->length - 1]; |
140 | 0 | pq->q[item->index]->index = item->index; |
141 | 0 | --pq->length; |
142 | |
|
143 | 0 | if (pq->less(item, pq->q[item->index])) { |
144 | 0 | bubble_down(pq, item->index); |
145 | 0 | } else { |
146 | 0 | bubble_up(pq, item->index); |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | 752 | int nghttp2_pq_empty(nghttp2_pq *pq) { return pq->length == 0; } |
151 | | |
152 | 0 | size_t nghttp2_pq_size(nghttp2_pq *pq) { return pq->length; } |
153 | | |
154 | 0 | void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) { |
155 | 0 | size_t i; |
156 | 0 | int rv = 0; |
157 | 0 | if (pq->length == 0) { |
158 | 0 | return; |
159 | 0 | } |
160 | 0 | for (i = 0; i < pq->length; ++i) { |
161 | 0 | rv |= (*fun)(pq->q[i], arg); |
162 | 0 | } |
163 | 0 | if (rv) { |
164 | 0 | for (i = pq->length; i > 0; --i) { |
165 | 0 | bubble_down(pq, i - 1); |
166 | 0 | } |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | 0 | int nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) { |
171 | 0 | size_t i; |
172 | |
|
173 | 0 | if (pq->length == 0) { |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | for (i = 0; i < pq->length; ++i) { |
177 | 0 | if ((*fun)(pq->q[i], arg)) { |
178 | 0 | return 1; |
179 | 0 | } |
180 | 0 | } |
181 | 0 | return 0; |
182 | 0 | } |