nghttp3_pq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. * Copyright (c) 2012 nghttp2 contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "nghttp3_pq.h"
  28. #include <assert.h>
  29. #include "nghttp3_macro.h"
  30. void nghttp3_pq_init(nghttp3_pq *pq, nghttp3_pq_less less,
  31. const nghttp3_mem *mem) {
  32. pq->q = NULL;
  33. pq->mem = mem;
  34. pq->length = 0;
  35. pq->capacity = 0;
  36. pq->less = less;
  37. }
  38. void nghttp3_pq_free(nghttp3_pq *pq) {
  39. if (!pq) {
  40. return;
  41. }
  42. nghttp3_mem_free(pq->mem, pq->q);
  43. }
  44. static void swap(nghttp3_pq *pq, size_t i, size_t j) {
  45. nghttp3_pq_entry *a = pq->q[i];
  46. nghttp3_pq_entry *b = pq->q[j];
  47. pq->q[i] = b;
  48. b->index = i;
  49. pq->q[j] = a;
  50. a->index = j;
  51. }
  52. static void bubble_up(nghttp3_pq *pq, size_t index) {
  53. size_t parent;
  54. while (index) {
  55. parent = (index - 1) / 2;
  56. if (!pq->less(pq->q[index], pq->q[parent])) {
  57. return;
  58. }
  59. swap(pq, parent, index);
  60. index = parent;
  61. }
  62. }
  63. int nghttp3_pq_push(nghttp3_pq *pq, nghttp3_pq_entry *item) {
  64. if (pq->capacity <= pq->length) {
  65. void *nq;
  66. size_t ncapacity;
  67. ncapacity = nghttp3_max_size(4, pq->capacity * 2);
  68. nq = nghttp3_mem_realloc(pq->mem, pq->q,
  69. ncapacity * sizeof(nghttp3_pq_entry *));
  70. if (nq == NULL) {
  71. return NGHTTP3_ERR_NOMEM;
  72. }
  73. pq->capacity = ncapacity;
  74. pq->q = nq;
  75. }
  76. pq->q[pq->length] = item;
  77. item->index = pq->length;
  78. ++pq->length;
  79. bubble_up(pq, item->index);
  80. return 0;
  81. }
  82. nghttp3_pq_entry *nghttp3_pq_top(const nghttp3_pq *pq) {
  83. assert(pq->length);
  84. return pq->q[0];
  85. }
  86. static void bubble_down(nghttp3_pq *pq, size_t index) {
  87. size_t i, j, minindex;
  88. for (;;) {
  89. j = index * 2 + 1;
  90. minindex = index;
  91. for (i = 0; i < 2; ++i, ++j) {
  92. if (j >= pq->length) {
  93. break;
  94. }
  95. if (pq->less(pq->q[j], pq->q[minindex])) {
  96. minindex = j;
  97. }
  98. }
  99. if (minindex == index) {
  100. return;
  101. }
  102. swap(pq, index, minindex);
  103. index = minindex;
  104. }
  105. }
  106. void nghttp3_pq_pop(nghttp3_pq *pq) {
  107. assert(pq->length);
  108. pq->q[0] = pq->q[pq->length - 1];
  109. pq->q[0]->index = 0;
  110. --pq->length;
  111. bubble_down(pq, 0);
  112. }
  113. void nghttp3_pq_remove(nghttp3_pq *pq, nghttp3_pq_entry *item) {
  114. assert(pq->q[item->index] == item);
  115. if (item->index == 0) {
  116. nghttp3_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 nghttp3_pq_empty(const nghttp3_pq *pq) { return pq->length == 0; }
  133. size_t nghttp3_pq_size(const nghttp3_pq *pq) { return pq->length; }
  134. int nghttp3_pq_each(const nghttp3_pq *pq, nghttp3_pq_item_cb fun, void *arg) {
  135. size_t i;
  136. if (pq->length == 0) {
  137. return 0;
  138. }
  139. for (i = 0; i < pq->length; ++i) {
  140. if ((*fun)(pq->q[i], arg)) {
  141. return 1;
  142. }
  143. }
  144. return 0;
  145. }
  146. void nghttp3_pq_clear(nghttp3_pq *pq) { pq->length = 0; }