Пример #1
0
void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
{
    unsigned min_child = 2 * (hole_index + 1);
    while (min_child <= s->n)
	{
	min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
	if (!(min_heap_elem_greater(e, s->p[min_child])))
	    break;
	(s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
	hole_index = min_child;
	min_child = 2 * (hole_index + 1);
	}
    (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
Пример #2
0
void min_heap_shift_down_(struct min_heap* s, unsigned hole_index, element* e)
{
    unsigned min_child = 2 * (hole_index + 1);
    while (min_child <= s->n)
    {
        min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
        if (!(min_heap_elem_greater(e, s->p[min_child])))
            break;
        ELEMENT_SET_INDEX((s->p[hole_index] = s->p[min_child]), hole_index);
        hole_index = min_child;
        min_child = 2 * (hole_index + 1);

    }
    ELEMENT_SET_INDEX((s->p[hole_index] = e), hole_index);
}
Пример #3
0
void
min_heap_shift_down_(min_heap_t * s, unsigned hole_index, event_t *e)
{
    unsigned min_child = 2 * (hole_index + 1);
    while (min_child <= s->n) {
        min_child -= 
            ((min_child == s->n) || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]));
        if (!min_heap_elem_greater(e, s->p[min_child])) break;
        s->p[hole_index] = s->p[min_child];
        s->p[hole_index]->min_heap_idx = hole_index;
        hole_index = min_child;
        min_child = 2 * (hole_index + 1);
    }
    s->p[hole_index] = e;
    s->p[hole_index]->min_heap_idx = hole_index;
}
Пример #4
0
void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
    unsigned parent = (hole_index - 1) / 2;
    while (hole_index && min_heap_elem_greater(s->p[parent], e))
    {
	(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
	hole_index = parent;
	parent = (hole_index - 1) / 2;
    }
    (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
Пример #5
0
void min_heap_shift_up_(struct min_heap* s, unsigned hole_index, element* e)
{
    unsigned parent = (hole_index - 1) / 2;
    while (hole_index && min_heap_elem_greater(s->p[parent], e))
    {
        ELEMENT_SET_INDEX((s->p[hole_index] = s->p[parent]), hole_index);
        hole_index = parent;
        parent = (hole_index - 1) / 2;

    }
    ELEMENT_SET_INDEX((s->p[hole_index] = e), hole_index);
}
Пример #6
0
void 
min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, event_t *e)
{
    unsigned parent = (hole_index - 1) / 2;
    do {
        s->p[hole_index] = s->p[parent];
        s->p[hole_index]->min_heap_idx = hole_index;
        hole_index = parent;
        parent = (hole_index - 1) / 2;
    } while (hole_index && min_heap_elem_greater(s->p[parent], e));
    s->p[hole_index] = e;
    s->p[hole_index]->min_heap_idx = hole_index;
}
Пример #7
0
void 
min_heap_shift_up_(min_heap_t *s, unsigned hole_index, event_t *e)
{
    unsigned parent = (hole_index - 1) / 2;
    while (hole_index && min_heap_elem_greater(s->p[parent], e)) {//min time on time
        s->p[hole_index] = s->p[parent];
        s->p[hole_index]->min_heap_idx = hole_index;//TODO ev_timeout_pos???
        hole_index = parent;
        parent = (hole_index - 1) / 2;
    }
    s->p[hole_index] = e;
    s->p[hole_index]->min_heap_idx = hole_index;
}
Пример #8
0
int min_heap_erase_(min_heap_t *s, event_t *e)
{
//    if (e->min_heap_idx != -1) {
    event_t *last = s->p[--s->n];
    unsigned parent = (e->min_heap_idx - 1 ) / 2;
    if (e->min_heap_idx > 0   &&  min_heap_elem_greater(s->p[parent], last) ) {
        min_heap_shift_up_unconditional_(s, e->min_heap_idx, last);
    } else {
        min_heap_shift_down_(s, e->min_heap_idx, last);
    }
    e->min_heap_idx = -1;
    return 0;
}
Пример #9
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;
}
Пример #10
0
/*
 *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;
}