This source file includes following definitions.
- min_heap_elem_greater
- min_heap_ctor
- min_heap_dtor
- min_heap_elem_init
- min_heap_empty
- min_heap_size
- min_heap_top
- min_heap_push
- min_heap_pop
- min_heap_elt_is_top
- min_heap_erase
- min_heap_reserve
- min_heap_shift_up_
- min_heap_shift_down_
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 #ifndef _MIN_HEAP_H_
  29 #define _MIN_HEAP_H_
  30 
  31 #include "event2/event-config.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 
  38 typedef struct min_heap
  39 {
  40         struct event** p;
  41         unsigned n, a;
  42 } min_heap_t;
  43 
  44 static inline void           min_heap_ctor(min_heap_t* s);
  45 static inline void           min_heap_dtor(min_heap_t* s);
  46 static inline void           min_heap_elem_init(struct event* e);
  47 static inline int            min_heap_elt_is_top(const struct event *e);
  48 static inline int            min_heap_elem_greater(struct event *a, struct event *b);
  49 static inline int            min_heap_empty(min_heap_t* s);
  50 static inline unsigned       min_heap_size(min_heap_t* s);
  51 static inline struct event*  min_heap_top(min_heap_t* s);
  52 static inline int            min_heap_reserve(min_heap_t* s, unsigned n);
  53 static inline int            min_heap_push(min_heap_t* s, struct event* e);
  54 static inline struct event*  min_heap_pop(min_heap_t* s);
  55 static inline int            min_heap_erase(min_heap_t* s, struct event* e);
  56 static inline void           min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
  57 static inline void           min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);
  58 
  59 int min_heap_elem_greater(struct event *a, struct event *b)
  60 {
  61         return evutil_timercmp(&a->ev_timeout, &b->ev_timeout, >);
  62 }
  63 
  64 void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
  65 void min_heap_dtor(min_heap_t* s) { if (s->p) mm_free(s->p); }
  66 void min_heap_elem_init(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
  67 int min_heap_empty(min_heap_t* s) { return 0u == s->n; }
  68 unsigned min_heap_size(min_heap_t* s) { return s->n; }
  69 struct event* min_heap_top(min_heap_t* s) { return s->n ? *s->p : 0; }
  70 
  71 int min_heap_push(min_heap_t* s, struct event* e)
  72 {
  73         if (min_heap_reserve(s, s->n + 1))
  74                 return -1;
  75         min_heap_shift_up_(s, s->n++, e);
  76         return 0;
  77 }
  78 
  79 struct event* min_heap_pop(min_heap_t* s)
  80 {
  81         if (s->n)
  82         {
  83                 struct event* e = *s->p;
  84                 min_heap_shift_down_(s, 0u, s->p[--s->n]);
  85                 e->ev_timeout_pos.min_heap_idx = -1;
  86                 return e;
  87         }
  88         return 0;
  89 }
  90 
  91 int min_heap_elt_is_top(const struct event *e)
  92 {
  93         return e->ev_timeout_pos.min_heap_idx == 0;
  94 }
  95 
  96 int min_heap_erase(min_heap_t* s, struct event* e)
  97 {
  98         if (-1 != e->ev_timeout_pos.min_heap_idx)
  99         {
 100                 struct event *last = s->p[--s->n];
 101                 unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
 102                 
 103 
 104 
 105 
 106 
 107                 if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
 108                         min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, last);
 109                 else
 110                         min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
 111                 e->ev_timeout_pos.min_heap_idx = -1;
 112                 return 0;
 113         }
 114         return -1;
 115 }
 116 
 117 int min_heap_reserve(min_heap_t* s, unsigned n)
 118 {
 119         if (s->a < n)
 120         {
 121                 struct event** p;
 122                 unsigned a = s->a ? s->a * 2 : 8;
 123                 if (a < n)
 124                         a = n;
 125                 if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p)))
 126                         return -1;
 127                 s->p = p;
 128                 s->a = a;
 129         }
 130         return 0;
 131 }
 132 
 133 void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
 134 {
 135     unsigned parent = (hole_index - 1) / 2;
 136     while (hole_index && min_heap_elem_greater(s->p[parent], e))
 137     {
 138         (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
 139         hole_index = parent;
 140         parent = (hole_index - 1) / 2;
 141     }
 142     (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
 143 }
 144 
 145 void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
 146 {
 147     unsigned min_child = 2 * (hole_index + 1);
 148     while (min_child <= s->n)
 149         {
 150         min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
 151         if (!(min_heap_elem_greater(e, s->p[min_child])))
 152             break;
 153         (s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
 154         hole_index = min_child;
 155         min_child = 2 * (hole_index + 1);
 156         }
 157     (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
 158 }
 159 
 160 #endif