bench-heap.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <stdlib.h>
  30. #include <err.h>
  31. #include "timeout.h"
  32. #include "bench.h"
  33. #define min_heap_idx interval
  34. typedef timeout_t min_heap_idx_t;
  35. typedef struct min_heap
  36. {
  37. struct timeout** p;
  38. unsigned n, a;
  39. timeout_t curtime;
  40. } min_heap_t;
  41. static inline void min_heap_ctor(min_heap_t* s);
  42. static inline void min_heap_dtor(min_heap_t* s);
  43. static inline void min_heap_elem_init(struct timeout* e);
  44. static inline int min_heap_elem_greater(struct timeout *a, struct timeout *b);
  45. static inline int min_heap_empty(min_heap_t* s);
  46. static inline unsigned min_heap_size(min_heap_t* s);
  47. static inline struct timeout* min_heap_top(min_heap_t* s);
  48. static inline int min_heap_reserve(min_heap_t* s, unsigned n);
  49. static inline int min_heap_push(min_heap_t* s, struct timeout* e);
  50. static inline struct timeout* min_heap_pop(min_heap_t* s);
  51. static inline int min_heap_erase(min_heap_t* s, struct timeout* e);
  52. static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct timeout* e);
  53. static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct timeout* e);
  54. int min_heap_elem_greater(struct timeout *a, struct timeout *b)
  55. {
  56. return a->expires > b->expires;
  57. }
  58. void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
  59. void min_heap_dtor(min_heap_t* s) { if(s->p) free(s->p); }
  60. void min_heap_elem_init(struct timeout* e) { e->min_heap_idx = -1; }
  61. int min_heap_empty(min_heap_t* s) { return 0u == s->n; }
  62. unsigned min_heap_size(min_heap_t* s) { return s->n; }
  63. struct timeout* min_heap_top(min_heap_t* s) { return s->n ? *s->p : 0; }
  64. int min_heap_push(min_heap_t* s, struct timeout* e)
  65. {
  66. if(min_heap_reserve(s, s->n + 1))
  67. return -1;
  68. min_heap_shift_up_(s, s->n++, e);
  69. return 0;
  70. }
  71. struct timeout* min_heap_pop(min_heap_t* s)
  72. {
  73. if(s->n)
  74. {
  75. struct timeout* e = *s->p;
  76. min_heap_shift_down_(s, 0u, s->p[--s->n]);
  77. e->min_heap_idx = -1;
  78. return e;
  79. }
  80. return 0;
  81. }
  82. int min_heap_erase(min_heap_t* s, struct timeout* e)
  83. {
  84. if(((min_heap_idx_t)-1) != e->min_heap_idx)
  85. {
  86. struct timeout *last = s->p[--s->n];
  87. unsigned parent = (e->min_heap_idx - 1) / 2;
  88. /* we replace e with the last element in the heap. We might need to
  89. shift it upward if it is less than its parent, or downward if it is
  90. greater than one or both its children. Since the children are known
  91. to be less than the parent, it can't need to shift both up and
  92. down. */
  93. if (e->min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
  94. min_heap_shift_up_(s, e->min_heap_idx, last);
  95. else
  96. min_heap_shift_down_(s, e->min_heap_idx, last);
  97. e->min_heap_idx = -1;
  98. return 0;
  99. }
  100. return -1;
  101. }
  102. int min_heap_reserve(min_heap_t* s, unsigned n)
  103. {
  104. if(s->a < n)
  105. {
  106. struct timeout** p;
  107. unsigned a = s->a ? s->a * 2 : 8;
  108. if(a < n)
  109. a = n;
  110. if(!(p = (struct timeout**)realloc(s->p, a * sizeof *p)))
  111. return -1;
  112. s->p = p;
  113. s->a = a;
  114. }
  115. return 0;
  116. }
  117. void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct timeout* e)
  118. {
  119. unsigned parent = (hole_index - 1) / 2;
  120. while(hole_index && min_heap_elem_greater(s->p[parent], e))
  121. {
  122. (s->p[hole_index] = s->p[parent])->min_heap_idx = hole_index;
  123. hole_index = parent;
  124. parent = (hole_index - 1) / 2;
  125. }
  126. (s->p[hole_index] = e)->min_heap_idx = hole_index;
  127. }
  128. void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct timeout* e)
  129. {
  130. unsigned min_child = 2 * (hole_index + 1);
  131. while(min_child <= s->n)
  132. {
  133. min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
  134. if(!(min_heap_elem_greater(e, s->p[min_child])))
  135. break;
  136. (s->p[hole_index] = s->p[min_child])->min_heap_idx = hole_index;
  137. hole_index = min_child;
  138. min_child = 2 * (hole_index + 1);
  139. }
  140. min_heap_shift_up_(s, hole_index, e);
  141. }
  142. #endif /* _MIN_HEAP_H_ */
  143. static void *init(struct timeout *timeout, size_t count, int verbose) {
  144. min_heap_t *H;
  145. size_t i;
  146. H = calloc(1, sizeof *H);
  147. min_heap_ctor(H);
  148. if (0 != min_heap_reserve(H, count))
  149. err(1, "realloc");
  150. for (i = 0; i < count; i++) {
  151. min_heap_elem_init(&timeout[i]);
  152. }
  153. return H;
  154. } /* init() */
  155. static void add(void *ctx, struct timeout *to, timeout_t expires) {
  156. min_heap_t *H = ctx;
  157. min_heap_erase(H, to);
  158. to->expires = H->curtime + expires;
  159. if (0 != min_heap_push(H, to))
  160. err(1, "realloc");
  161. } /* add() */
  162. static void del(void *ctx, struct timeout *to) {
  163. min_heap_erase(ctx, to);
  164. } /* del() */
  165. static struct timeout *get(void *ctx) {
  166. min_heap_t *H = ctx;
  167. struct timeout *to;
  168. if ((to = min_heap_top(H)) && to->expires <= H->curtime)
  169. return min_heap_pop(H);
  170. return NULL;
  171. } /* get() */
  172. static void update(void *ctx, timeout_t ts) {
  173. min_heap_t *H = ctx;
  174. H->curtime = ts;
  175. } /* update() */
  176. static void check(void *ctx) {
  177. return;
  178. } /* check() */
  179. static int empty(void *ctx) {
  180. min_heap_t *H = ctx;
  181. return (NULL == min_heap_top(H));
  182. } /* empty() */
  183. static void destroy(void *H) {
  184. free(H);
  185. return;
  186. } /* destroy() */
  187. const struct benchops benchops = {
  188. .init = &init,
  189. .add = &add,
  190. .del = &del,
  191. .get = &get,
  192. .update = &update,
  193. .check = &check,
  194. .empty = &empty,
  195. .destroy = &destroy,
  196. };