You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heap.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "libutil/heap.h"
  18. struct rspamd_min_heap {
  19. GPtrArray *ar;
  20. };
  21. #define __SWAP(a, b) \
  22. do { \
  23. __typeof__(a) _a = (a); \
  24. __typeof__(b) _b = (b); \
  25. a = _b; \
  26. b = _a; \
  27. } while (0)
  28. #define heap_swap(h, e1, e2) \
  29. do { \
  30. __SWAP((h)->ar->pdata[(e1)->idx - 1], (h)->ar->pdata[(e2)->idx - 1]); \
  31. __SWAP((e1)->idx, (e2)->idx); \
  32. } while (0)
  33. #define min_elt(e1, e2) ((e1)->pri <= (e2)->pri ? (e1) : (e2))
  34. /*
  35. * Swims element added (or changed) to preserve heap's invariant
  36. */
  37. static void
  38. rspamd_min_heap_swim(struct rspamd_min_heap *heap,
  39. struct rspamd_min_heap_elt *elt)
  40. {
  41. struct rspamd_min_heap_elt *parent;
  42. while (elt->idx > 1) {
  43. parent = g_ptr_array_index(heap->ar, elt->idx / 2 - 1);
  44. if (parent->pri > elt->pri) {
  45. heap_swap(heap, elt, parent);
  46. }
  47. else {
  48. break;
  49. }
  50. }
  51. }
  52. /*
  53. * Sinks the element popped (or changed) to preserve heap's invariant
  54. */
  55. static void
  56. rspamd_min_heap_sink(struct rspamd_min_heap *heap,
  57. struct rspamd_min_heap_elt *elt)
  58. {
  59. struct rspamd_min_heap_elt *c1, *c2, *m;
  60. while (elt->idx * 2 < heap->ar->len) {
  61. c1 = g_ptr_array_index(heap->ar, elt->idx * 2 - 1);
  62. c2 = g_ptr_array_index(heap->ar, elt->idx * 2);
  63. m = min_elt(c1, c2);
  64. if (elt->pri > m->pri) {
  65. heap_swap(heap, elt, m);
  66. }
  67. else {
  68. break;
  69. }
  70. }
  71. if (elt->idx * 2 - 1 < heap->ar->len) {
  72. m = g_ptr_array_index(heap->ar, elt->idx * 2 - 1);
  73. if (elt->pri > m->pri) {
  74. heap_swap(heap, elt, m);
  75. }
  76. }
  77. }
  78. struct rspamd_min_heap *
  79. rspamd_min_heap_create(gsize reserved_size)
  80. {
  81. struct rspamd_min_heap *heap;
  82. heap = g_malloc(sizeof(*heap));
  83. heap->ar = g_ptr_array_sized_new(reserved_size);
  84. return heap;
  85. }
  86. void rspamd_min_heap_push(struct rspamd_min_heap *heap,
  87. struct rspamd_min_heap_elt *elt)
  88. {
  89. g_assert(heap != NULL);
  90. g_assert(elt != NULL);
  91. /* Add to the end */
  92. elt->idx = heap->ar->len + 1;
  93. g_ptr_array_add(heap->ar, elt);
  94. /* Now swim it up */
  95. rspamd_min_heap_swim(heap, elt);
  96. }
  97. struct rspamd_min_heap_elt *
  98. rspamd_min_heap_pop(struct rspamd_min_heap *heap)
  99. {
  100. struct rspamd_min_heap_elt *elt, *last;
  101. g_assert(heap != NULL);
  102. if (heap->ar->len == 0) {
  103. return NULL;
  104. }
  105. elt = g_ptr_array_index(heap->ar, 0);
  106. last = g_ptr_array_index(heap->ar, heap->ar->len - 1);
  107. if (elt != last) {
  108. /* Now replace elt with the last element and sink it if needed */
  109. heap_swap(heap, elt, last);
  110. g_ptr_array_remove_index_fast(heap->ar, heap->ar->len - 1);
  111. rspamd_min_heap_sink(heap, last);
  112. }
  113. else {
  114. g_ptr_array_remove_index_fast(heap->ar, heap->ar->len - 1);
  115. }
  116. return elt;
  117. }
  118. void rspamd_min_heap_update_elt(struct rspamd_min_heap *heap,
  119. struct rspamd_min_heap_elt *elt, unsigned int npri)
  120. {
  121. unsigned int oldpri;
  122. g_assert(heap != NULL);
  123. g_assert(elt->idx > 0 && elt->idx <= heap->ar->len);
  124. oldpri = elt->pri;
  125. elt->pri = npri;
  126. if (npri > oldpri) {
  127. /* We might need to sink */
  128. rspamd_min_heap_sink(heap, elt);
  129. }
  130. else if (npri < oldpri) {
  131. /* We might need to swim */
  132. rspamd_min_heap_swim(heap, elt);
  133. }
  134. }
  135. void rspamd_min_heap_remove_elt(struct rspamd_min_heap *heap,
  136. struct rspamd_min_heap_elt *elt)
  137. {
  138. struct rspamd_min_heap_elt *first;
  139. g_assert(heap != NULL);
  140. g_assert(elt->idx > 0 && elt->idx <= heap->ar->len);
  141. first = g_ptr_array_index(heap->ar, 0);
  142. if (elt != first) {
  143. elt->pri = first->pri - 1;
  144. rspamd_min_heap_swim(heap, elt);
  145. }
  146. /* Now the desired element is on the top of queue */
  147. (void) rspamd_min_heap_pop(heap);
  148. }
  149. void rspamd_min_heap_destroy(struct rspamd_min_heap *heap)
  150. {
  151. if (heap) {
  152. g_ptr_array_free(heap->ar, TRUE);
  153. g_free(heap);
  154. }
  155. }
  156. struct rspamd_min_heap_elt *
  157. rspamd_min_heap_index(struct rspamd_min_heap *heap, unsigned int idx)
  158. {
  159. g_assert(heap != NULL);
  160. g_assert(idx < heap->ar->len);
  161. return g_ptr_array_index(heap->ar, idx);
  162. }