nghttp2_pq.c 4.3 KB

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