示例#1
0
文件: heapq.c 项目: 19emtuck/meinheld
static void
_siftup(heapq_t *q, uint32_t pos)
{
    uint32_t startpos, childpos, rightpos;
    TimerObject *newitem, *childpositem;
    uint32_t size = q->size;
    TimerObject **p = q->heap;

    startpos = pos;
    newitem = p[pos];
    childpos = (pos << 1) + 1;

    while(likely(childpos < size)){
        rightpos = childpos + 1;
        childpositem = p[childpos];
        if(rightpos < size && childpositem->seconds > p[rightpos]->seconds){
            childpos = rightpos;
            childpositem = p[childpos];
        }
        p[pos] = childpositem;
        pos = childpos;
        childpos = (pos << 1) + 1;
    }
    p[pos] = newitem;
    _siftdown(q, startpos, pos);
}
示例#2
0
文件: heapq.c 项目: 19emtuck/meinheld
int
heappush(heapq_t *q, TimerObject *val)
{
    TimerObject **new_heap;
    uint32_t max;

    DEBUG("heappush size %d", q->size);
    if(q->size >= q->max){
        //realloc
        max = q->max * 2;
        new_heap = (TimerObject**)realloc(q->heap, sizeof(TimerObject*) * max);
        if(new_heap == NULL){
            PyErr_SetString(PyExc_Exception, "size over timer queue");
            return -1;
        }
        q->max = max;
        q->heap = new_heap;
        DEBUG("realloc max:%d", q->max);
    }
    Py_INCREF(val);
    q->heap[q->size] = val;
    q->size++;
    _siftdown(q, 0, q->size -1);
    return 1;
}
static int
_siftup(PyListObject *heap, Py_ssize_t pos)
{
    Py_ssize_t startpos, endpos, childpos, rightpos;
    int cmp;
    PyObject *newitem, *tmp;

    assert(PyList_Check(heap));
    endpos = PyList_GET_SIZE(heap);
    startpos = pos;
    if (pos >= endpos) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return -1;
    }
    newitem = PyList_GET_ITEM(heap, pos);
    Py_INCREF(newitem);

    /* Bubble up the smaller child until hitting a leaf. */
    childpos = 2*pos + 1;    /* leftmost child position  */
    while (childpos < endpos) {
        /* Set childpos to index of smaller child.   */
        rightpos = childpos + 1;
        if (rightpos < endpos) {
            cmp = PyObject_RichCompareBool(
                PyList_GET_ITEM(heap, childpos),
                PyList_GET_ITEM(heap, rightpos),
                Py_LT);
            if (cmp == -1) {
                Py_DECREF(newitem);
                return -1;
            }
            if (cmp == 0)
                childpos = rightpos;
        }
        /* Move the smaller child up. */
        tmp = PyList_GET_ITEM(heap, childpos);
        Py_INCREF(tmp);
        Py_DECREF(PyList_GET_ITEM(heap, pos));
        PyList_SET_ITEM(heap, pos, tmp);
        pos = childpos;
        childpos = 2*pos + 1;
    }

    /* The leaf at pos is empty now.  Put newitem there, and and bubble
       it up to its final resting place (by sifting its parents down). */
    Py_DECREF(PyList_GET_ITEM(heap, pos));
    PyList_SET_ITEM(heap, pos, newitem);
    return _siftdown(heap, startpos, pos);
}
示例#4
0
static int
_siftup(PyListObject *heap, Py_ssize_t pos)
{
    Py_ssize_t startpos, endpos, childpos, rightpos, limit;
    PyObject *tmp1, *tmp2;
    int cmp;

    assert(PyList_Check(heap));
    endpos = PyList_GET_SIZE(heap);
    startpos = pos;
    if (pos >= endpos) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return -1;
    }

    /* Bubble up the smaller child until hitting a leaf. */
    limit = endpos / 2;          /* smallest pos that has no child */
    while (pos < limit) {
        /* Set childpos to index of smaller child.   */
        childpos = 2*pos + 1;    /* leftmost child position  */
        rightpos = childpos + 1;
        if (rightpos < endpos) {
            cmp = PyObject_RichCompareBool(
                PyList_GET_ITEM(heap, childpos),
                PyList_GET_ITEM(heap, rightpos),
                Py_LT);
            if (cmp == -1)
                return -1;
            if (cmp == 0)
                childpos = rightpos;
            if (endpos != PyList_GET_SIZE(heap)) {
                PyErr_SetString(PyExc_RuntimeError,
                                "list changed size during iteration");
                return -1;
            }
        }
        /* Move the smaller child up. */
        tmp1 = PyList_GET_ITEM(heap, childpos);
        tmp2 = PyList_GET_ITEM(heap, pos);
        PyList_SET_ITEM(heap, childpos, tmp2);
        PyList_SET_ITEM(heap, pos, tmp1);
        pos = childpos;
    }
    /* Bubble it up to its final resting place (by sifting its parents down). */
    return _siftdown(heap, startpos, pos);
}
示例#5
0
void _siftup(Heap * self, int pos) {
    int startpos = pos;
    int childpos = 2 * pos + 1;
    self->load(-1, pos, self->data, self->weight);
    while(childpos < self->length) {
        int rightpos = childpos + 1;
        if(rightpos < self->length && 
                !(self->cmp_lt(childpos, rightpos, self->weight))) {
            childpos = rightpos;
        }
        self->load(pos, childpos, self->data, self->weight);
        pos = childpos;
        childpos = 2 * pos + 1;
    }
    self->load(pos, -1, self->data, self->weight);
    _siftdown(self, startpos, pos);
}
示例#6
0
static PyObject *
heappush(PyObject *self, PyObject *args)
{
    PyObject *heap, *item;

    if (!PyArg_UnpackTuple(args, "heappush", 2, 2, &heap, &item))
        return NULL;

    if (!PyList_Check(heap)) {
        PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
        return NULL;
    }

    if (PyList_Append(heap, item) == -1)
        return NULL;

    if (_siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
        return NULL;
    Py_INCREF(Py_None);
    return Py_None;
}