ngtcp2_pq.c 3.9 KB

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