min_heap.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006 Maxim Yegorushkin <maxim.yegorushkin@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef _MIN_HEAP_H_
  28. #define _MIN_HEAP_H_
  29. #include "event.h"
  30. #include "evutil.h"
  31. typedef struct min_heap
  32. {
  33. struct event** p;
  34. unsigned n, a;
  35. } min_heap_t;
  36. static inline void min_heap_ctor(min_heap_t* s);
  37. static inline void min_heap_dtor(min_heap_t* s);
  38. static inline void min_heap_elem_init(struct event* e);
  39. static inline int min_heap_elem_greater(struct event *a, struct event *b);
  40. static inline int min_heap_empty(min_heap_t* s);
  41. static inline unsigned min_heap_size(min_heap_t* s);
  42. static inline struct event* min_heap_top(min_heap_t* s);
  43. static inline int min_heap_reserve(min_heap_t* s, unsigned n);
  44. static inline int min_heap_push(min_heap_t* s, struct event* e);
  45. static inline struct event* min_heap_pop(min_heap_t* s);
  46. static inline int min_heap_erase(min_heap_t* s, struct event* e);
  47. static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
  48. static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);
  49. int min_heap_elem_greater(struct event *a, struct event *b)
  50. {
  51. return evutil_timercmp(&a->ev_timeout, &b->ev_timeout, >);
  52. }
  53. void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
  54. void min_heap_dtor(min_heap_t* s) { if(s->p) free(s->p); }
  55. void min_heap_elem_init(struct event* e) { e->min_heap_idx = -1; }
  56. int min_heap_empty(min_heap_t* s) { return 0u == s->n; }
  57. unsigned min_heap_size(min_heap_t* s) { return s->n; }
  58. struct event* min_heap_top(min_heap_t* s) { return s->n ? *s->p : 0; }
  59. int min_heap_push(min_heap_t* s, struct event* e)
  60. {
  61. if(min_heap_reserve(s, s->n + 1))
  62. return -1;
  63. min_heap_shift_up_(s, s->n++, e);
  64. return 0;
  65. }
  66. struct event* min_heap_pop(min_heap_t* s)
  67. {
  68. if(s->n)
  69. {
  70. struct event* e = *s->p;
  71. min_heap_shift_down_(s, 0u, s->p[--s->n]);
  72. e->min_heap_idx = -1;
  73. return e;
  74. }
  75. return 0;
  76. }
  77. int min_heap_erase(min_heap_t* s, struct event* e)
  78. {
  79. if(((unsigned int)-1) != e->min_heap_idx)
  80. {
  81. struct event *last = s->p[--s->n];
  82. unsigned parent = (e->min_heap_idx - 1) / 2;
  83. /* we replace e with the last element in the heap. We might need to
  84. shift it upward if it is less than its parent, or downward if it is
  85. greater than one or both its children. Since the children are known
  86. to be less than the parent, it can't need to shift both up and
  87. down. */
  88. if (e->min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
  89. min_heap_shift_up_(s, e->min_heap_idx, last);
  90. else
  91. min_heap_shift_down_(s, e->min_heap_idx, last);
  92. e->min_heap_idx = -1;
  93. return 0;
  94. }
  95. return -1;
  96. }
  97. int min_heap_reserve(min_heap_t* s, unsigned n)
  98. {
  99. if(s->a < n)
  100. {
  101. struct event** p;
  102. unsigned a = s->a ? s->a * 2 : 8;
  103. if(a < n)
  104. a = n;
  105. if(!(p = (struct event**)realloc(s->p, a * sizeof *p)))
  106. return -1;
  107. s->p = p;
  108. s->a = a;
  109. }
  110. return 0;
  111. }
  112. void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
  113. {
  114. unsigned parent = (hole_index - 1) / 2;
  115. while(hole_index && min_heap_elem_greater(s->p[parent], e))
  116. {
  117. (s->p[hole_index] = s->p[parent])->min_heap_idx = hole_index;
  118. hole_index = parent;
  119. parent = (hole_index - 1) / 2;
  120. }
  121. (s->p[hole_index] = e)->min_heap_idx = hole_index;
  122. }
  123. void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
  124. {
  125. unsigned min_child = 2 * (hole_index + 1);
  126. while(min_child <= s->n)
  127. {
  128. min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
  129. if(!(min_heap_elem_greater(e, s->p[min_child])))
  130. break;
  131. (s->p[hole_index] = s->p[min_child])->min_heap_idx = hole_index;
  132. hole_index = min_child;
  133. min_child = 2 * (hole_index + 1);
  134. }
  135. min_heap_shift_up_(s, hole_index, e);
  136. }
  137. #endif /* _MIN_HEAP_H_ */