2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "nghttp2_pq.h"
30 #include "nghttp2_helper.h"
32 int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem) {
41 void nghttp2_pq_free(nghttp2_pq *pq) {
42 nghttp2_mem_free(pq->mem, pq->q);
46 static void swap(nghttp2_pq *pq, size_t i, size_t j) {
47 nghttp2_pq_entry *a = pq->q[i];
48 nghttp2_pq_entry *b = pq->q[j];
56 static void bubble_up(nghttp2_pq *pq, size_t index) {
59 parent = (index - 1) / 2;
60 if (!pq->less(pq->q[index], pq->q[parent])) {
63 swap(pq, parent, index);
68 int nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item) {
69 if (pq->capacity <= pq->length) {
73 ncapacity = nghttp2_max(4, (pq->capacity * 2));
75 nq = nghttp2_mem_realloc(pq->mem, pq->q,
76 ncapacity * sizeof(nghttp2_pq_entry *));
78 return NGHTTP2_ERR_NOMEM;
80 pq->capacity = ncapacity;
83 pq->q[pq->length] = item;
84 item->index = pq->length;
86 bubble_up(pq, pq->length - 1);
90 nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) {
91 if (pq->length == 0) {
98 static void bubble_down(nghttp2_pq *pq, size_t index) {
99 size_t i, j, minindex;
103 for (i = 0; i < 2; ++i, ++j) {
104 if (j >= pq->length) {
107 if (pq->less(pq->q[j], pq->q[minindex])) {
111 if (minindex == index) {
114 swap(pq, index, minindex);
119 void nghttp2_pq_pop(nghttp2_pq *pq) {
120 if (pq->length > 0) {
121 pq->q[0] = pq->q[pq->length - 1];
128 void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
129 assert(pq->q[item->index] == item);
131 if (item->index == 0) {
136 if (item->index == pq->length - 1) {
141 pq->q[item->index] = pq->q[pq->length - 1];
142 pq->q[item->index]->index = item->index;
145 if (pq->less(item, pq->q[item->index])) {
146 bubble_down(pq, item->index);
148 bubble_up(pq, item->index);
152 int nghttp2_pq_empty(nghttp2_pq *pq) { return pq->length == 0; }
154 size_t nghttp2_pq_size(nghttp2_pq *pq) { return pq->length; }
156 void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
159 if (pq->length == 0) {
162 for (i = 0; i < pq->length; ++i) {
163 rv |= (*fun)(pq->q[i], arg);
166 for (i = pq->length; i > 0; --i) {
167 bubble_down(pq, i - 1);
172 int nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
175 if (pq->length == 0) {
178 for (i = 0; i < pq->length; ++i) {
179 if ((*fun)(pq->q[i], arg)) {