minheap-internal.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
  3. *
  4. * Copyright (c) 2006 Maxim Yegorushkin <maxim.yegorushkin@gmail.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef MINHEAP_INTERNAL_H_INCLUDED_
  29. #define MINHEAP_INTERNAL_H_INCLUDED_
  30. #include "event2/event-config.h"
  31. #include "evconfig-private.h"
  32. #include "event2/event.h"
  33. #include "event2/event_struct.h"
  34. #include "event2/util.h"
  35. #include "util-internal.h"
  36. #include "mm-internal.h"
  37. typedef struct min_heap
  38. {
  39. struct event** p;
  40. unsigned n, a;
  41. } min_heap_t;
  42. static inline void min_heap_ctor_(min_heap_t* s);
  43. static inline void min_heap_dtor_(min_heap_t* s);
  44. static inline void min_heap_elem_init_(struct event* e);
  45. static inline int min_heap_elt_is_top_(const struct event *e);
  46. static inline int min_heap_empty_(min_heap_t* s);
  47. static inline unsigned min_heap_size_(min_heap_t* s);
  48. static inline struct event* min_heap_top_(min_heap_t* s);
  49. static inline int min_heap_reserve_(min_heap_t* s, unsigned n);
  50. static inline int min_heap_push_(min_heap_t* s, struct event* e);
  51. static inline struct event* min_heap_pop_(min_heap_t* s);
  52. static inline int min_heap_adjust_(min_heap_t *s, struct event* e);
  53. static inline int min_heap_erase_(min_heap_t* s, struct event* e);
  54. static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
  55. static inline void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e);
  56. static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);
  57. #define min_heap_elem_greater(a, b) \
  58. (evutil_timercmp(&(a)->ev_timeout, &(b)->ev_timeout, >))
  59. void min_heap_ctor_(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
  60. void min_heap_dtor_(min_heap_t* s) { if (s->p) mm_free(s->p); }
  61. void min_heap_elem_init_(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
  62. int min_heap_empty_(min_heap_t* s) { return 0u == s->n; }
  63. unsigned min_heap_size_(min_heap_t* s) { return s->n; }
  64. struct event* min_heap_top_(min_heap_t* s) { return s->n ? *s->p : 0; }
  65. int min_heap_push_(min_heap_t* s, struct event* e)
  66. {
  67. if (s->n == UINT32_MAX || min_heap_reserve_(s, s->n + 1))
  68. return -1;
  69. min_heap_shift_up_(s, s->n++, e);
  70. return 0;
  71. }
  72. struct event* min_heap_pop_(min_heap_t* s)
  73. {
  74. if (s->n)
  75. {
  76. struct event* e = *s->p;
  77. min_heap_shift_down_(s, 0u, s->p[--s->n]);
  78. e->ev_timeout_pos.min_heap_idx = -1;
  79. return e;
  80. }
  81. return 0;
  82. }
  83. int min_heap_elt_is_top_(const struct event *e)
  84. {
  85. return e->ev_timeout_pos.min_heap_idx == 0;
  86. }
  87. int min_heap_erase_(min_heap_t* s, struct event* e)
  88. {
  89. if (-1 != e->ev_timeout_pos.min_heap_idx)
  90. {
  91. struct event *last = s->p[--s->n];
  92. unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
  93. /* we replace e with the last element in the heap. We might need to
  94. shift it upward if it is less than its parent, or downward if it is
  95. greater than one or both its children. Since the children are known
  96. to be less than the parent, it can't need to shift both up and
  97. down. */
  98. if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
  99. min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, last);
  100. else
  101. min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
  102. e->ev_timeout_pos.min_heap_idx = -1;
  103. return 0;
  104. }
  105. return -1;
  106. }
  107. int min_heap_adjust_(min_heap_t *s, struct event *e)
  108. {
  109. if (-1 == e->ev_timeout_pos.min_heap_idx) {
  110. return min_heap_push_(s, e);
  111. } else {
  112. unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
  113. /* The position of e has changed; we shift it up or down
  114. * as needed. We can't need to do both. */
  115. if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], e))
  116. min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, e);
  117. else
  118. min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e);
  119. return 0;
  120. }
  121. }
  122. int min_heap_reserve_(min_heap_t* s, unsigned n)
  123. {
  124. if (s->a < n)
  125. {
  126. struct event** p;
  127. unsigned a = s->a ? s->a * 2 : 8;
  128. if (a < n)
  129. a = n;
  130. #if (SIZE_MAX == UINT32_MAX)
  131. if (a > SIZE_MAX / sizeof *p)
  132. return -1;
  133. #endif
  134. if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p)))
  135. return -1;
  136. s->p = p;
  137. s->a = a;
  138. }
  139. return 0;
  140. }
  141. void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)
  142. {
  143. unsigned parent = (hole_index - 1) / 2;
  144. do
  145. {
  146. (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
  147. hole_index = parent;
  148. parent = (hole_index - 1) / 2;
  149. } while (hole_index && min_heap_elem_greater(s->p[parent], e));
  150. (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
  151. }
  152. void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
  153. {
  154. unsigned parent = (hole_index - 1) / 2;
  155. while (hole_index && min_heap_elem_greater(s->p[parent], e))
  156. {
  157. (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
  158. hole_index = parent;
  159. parent = (hole_index - 1) / 2;
  160. }
  161. (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
  162. }
  163. void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
  164. {
  165. unsigned min_child = 2 * (hole_index + 1);
  166. while (min_child <= s->n)
  167. {
  168. min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
  169. if (!(min_heap_elem_greater(e, s->p[min_child])))
  170. break;
  171. (s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
  172. hole_index = min_child;
  173. min_child = 2 * (hole_index + 1);
  174. }
  175. (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
  176. }
  177. #endif /* MINHEAP_INTERNAL_H_INCLUDED_ */