/* *min heap push */ int min_heap_push(struct min_heap* s, element* e) { if (min_heap_reserve(s, s->n + 1)) return -1; min_heap_shift_up_(s, s->n++, e); return 0; }
int min_heap_push_(min_heap_t* s, event_t *e) { if (min_heap_reserve_(s, s->n + 1)) return -1; min_heap_shift_up_(s, s->n++, e); return 0; }
int min_heap_erase(min_heap_t* s, struct event* e) { if (((unsigned int)-1) != e->ev_timeout_pos.min_heap_idx) { struct event *last = s->p[--s->n]; unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2; /* we replace e with the last element in the heap. We might need to shift it upward if it is less than its parent, or downward if it is greater than one or both its children. Since the children are known to be less than the parent, it can't need to shift both up and down. */ if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last)) min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, last); else min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last); e->ev_timeout_pos.min_heap_idx = -1; return 0; } return -1; }
/* *Del node */ int min_heap_erase(struct min_heap* s, element* e) { if (-1 != ELEMENT_INDEX(e)) { element *last = s->p[--s->n]; unsigned parent = (ELEMENT_INDEX(e) - 1) / 2; /* we replace e with the last element in the heap. We might need to * shift it upward if it is less than its parent, or downward if it is * greater than one or both its children. Since the children are known * to be less than the parent, it can't need to shift both up and * down. */ if (ELEMENT_INDEX(e) > 0 && min_heap_elem_greater(s->p[parent], last)) min_heap_shift_up_(s, ELEMENT_INDEX(e), last); else min_heap_shift_down_(s, ELEMENT_INDEX(e), last); ELEMENT_SET_INDEX(e, -1); return 0; } return -1; }