michael@0: /* michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Copyright (c) 2006 Maxim Yegorushkin michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _MIN_HEAP_H_ michael@0: #define _MIN_HEAP_H_ michael@0: michael@0: #include "event2/event-config.h" michael@0: #include "event2/event.h" michael@0: #include "event2/event_struct.h" michael@0: #include "event2/util.h" michael@0: #include "util-internal.h" michael@0: #include "mm-internal.h" michael@0: michael@0: typedef struct min_heap michael@0: { michael@0: struct event** p; michael@0: unsigned n, a; michael@0: } min_heap_t; michael@0: michael@0: static inline void min_heap_ctor(min_heap_t* s); michael@0: static inline void min_heap_dtor(min_heap_t* s); michael@0: static inline void min_heap_elem_init(struct event* e); michael@0: static inline int min_heap_elt_is_top(const struct event *e); michael@0: static inline int min_heap_elem_greater(struct event *a, struct event *b); michael@0: static inline int min_heap_empty(min_heap_t* s); michael@0: static inline unsigned min_heap_size(min_heap_t* s); michael@0: static inline struct event* min_heap_top(min_heap_t* s); michael@0: static inline int min_heap_reserve(min_heap_t* s, unsigned n); michael@0: static inline int min_heap_push(min_heap_t* s, struct event* e); michael@0: static inline struct event* min_heap_pop(min_heap_t* s); michael@0: static inline int min_heap_erase(min_heap_t* s, struct event* e); michael@0: static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e); michael@0: static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e); michael@0: michael@0: int min_heap_elem_greater(struct event *a, struct event *b) michael@0: { michael@0: return evutil_timercmp(&a->ev_timeout, &b->ev_timeout, >); michael@0: } michael@0: michael@0: void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; } michael@0: void min_heap_dtor(min_heap_t* s) { if (s->p) mm_free(s->p); } michael@0: void min_heap_elem_init(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; } michael@0: int min_heap_empty(min_heap_t* s) { return 0u == s->n; } michael@0: unsigned min_heap_size(min_heap_t* s) { return s->n; } michael@0: struct event* min_heap_top(min_heap_t* s) { return s->n ? *s->p : 0; } michael@0: michael@0: int min_heap_push(min_heap_t* s, struct event* e) michael@0: { michael@0: if (min_heap_reserve(s, s->n + 1)) michael@0: return -1; michael@0: min_heap_shift_up_(s, s->n++, e); michael@0: return 0; michael@0: } michael@0: michael@0: struct event* min_heap_pop(min_heap_t* s) michael@0: { michael@0: if (s->n) michael@0: { michael@0: struct event* e = *s->p; michael@0: min_heap_shift_down_(s, 0u, s->p[--s->n]); michael@0: e->ev_timeout_pos.min_heap_idx = -1; michael@0: return e; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: int min_heap_elt_is_top(const struct event *e) michael@0: { michael@0: return e->ev_timeout_pos.min_heap_idx == 0; michael@0: } michael@0: michael@0: int min_heap_erase(min_heap_t* s, struct event* e) michael@0: { michael@0: if (-1 != e->ev_timeout_pos.min_heap_idx) michael@0: { michael@0: struct event *last = s->p[--s->n]; michael@0: unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2; michael@0: /* we replace e with the last element in the heap. We might need to michael@0: shift it upward if it is less than its parent, or downward if it is michael@0: greater than one or both its children. Since the children are known michael@0: to be less than the parent, it can't need to shift both up and michael@0: down. */ michael@0: if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last)) michael@0: min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, last); michael@0: else michael@0: min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last); michael@0: e->ev_timeout_pos.min_heap_idx = -1; michael@0: return 0; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: int min_heap_reserve(min_heap_t* s, unsigned n) michael@0: { michael@0: if (s->a < n) michael@0: { michael@0: struct event** p; michael@0: unsigned a = s->a ? s->a * 2 : 8; michael@0: if (a < n) michael@0: a = n; michael@0: if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p))) michael@0: return -1; michael@0: s->p = p; michael@0: s->a = a; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e) michael@0: { michael@0: unsigned parent = (hole_index - 1) / 2; michael@0: while (hole_index && min_heap_elem_greater(s->p[parent], e)) michael@0: { michael@0: (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index; michael@0: hole_index = parent; michael@0: parent = (hole_index - 1) / 2; michael@0: } michael@0: (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index; michael@0: } michael@0: michael@0: void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e) michael@0: { michael@0: unsigned min_child = 2 * (hole_index + 1); michael@0: while (min_child <= s->n) michael@0: { michael@0: min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]); michael@0: if (!(min_heap_elem_greater(e, s->p[min_child]))) michael@0: break; michael@0: (s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index; michael@0: hole_index = min_child; michael@0: min_child = 2 * (hole_index + 1); michael@0: } michael@0: (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index; michael@0: } michael@0: michael@0: #endif /* _MIN_HEAP_H_ */