Пример #1
0
static PyObject *
heapreplace(PyObject *self, PyObject *args)
{
    PyObject *heap, *item, *returnitem;

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

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

    if (PyList_GET_SIZE(heap) < 1) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return NULL;
    }

    returnitem = PyList_GET_ITEM(heap, 0);
    Py_INCREF(item);
    PyList_SET_ITEM(heap, 0, item);
    if (_siftup((PyListObject *)heap, 0) == -1) {
        Py_DECREF(returnitem);
        return NULL;
    }
    return returnitem;
}
Пример #2
0
TimerObject *
heappop(heapq_t *q)
{
    uint32_t size;
    TimerObject *last = NULL, *retitem = NULL;
    TimerObject **p = q->heap;

    if(unlikely(q->size == 0)){
        return NULL;
    }

    q->size--;
    size = q->size;
    last = p[size];

    p[size] = NULL;

    if(likely(size > 0)){
        retitem = *p;
        *p = last;
        _siftup(q, 0);
    }else{
        retitem = last;
    }
    return retitem;
}
static PyObject *
heappop(PyObject *self, PyObject *heap)
{
    PyObject *lastelt, *returnitem;
    Py_ssize_t n;

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

    /* # raises appropriate IndexError if heap is empty */
    n = PyList_GET_SIZE(heap);
    if (n == 0) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return NULL;
    }

    lastelt = PyList_GET_ITEM(heap, n-1) ;
    Py_INCREF(lastelt);
    PyList_SetSlice(heap, n-1, n, NULL);
    n--;

    if (!n)
        return lastelt;
    returnitem = PyList_GET_ITEM(heap, 0);
    PyList_SET_ITEM(heap, 0, lastelt);
    if (_siftup((PyListObject *)heap, 0) == -1) {
        Py_DECREF(returnitem);
        return NULL;
    }
    return returnitem;
}
Пример #4
0
int main() {
    Heap wh;
    wh.cmp_lt = cmp_lt;
    wh.load = load;
    wh.data = data + 1;
    wh.weight = data + 1;
    wh.length = 8;
    int i;
    for (i = 0; i < wh.length; i++) {
      data[i+1] = wh.length - i;
    }
    _heapify(&wh);
    for (i = 0; i < wh.length; i++) {
      printf("%g\n", data[i+1]);
    }
    data[1] = 99;
    _siftup(&wh, 0);
    for (i = 0; i < wh.length; i++) {
      printf("%g\n", data[i+1]);
    }
    return 0;
}
Пример #5
0
static PyObject *
heapify(PyObject *self, PyObject *heap)
{
    Py_ssize_t i, n;

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

    n = PyList_GET_SIZE(heap);
    /* Transform bottom-up.  The largest index there's any point to
       looking at is the largest with a child index in-range, so must
       have 2*i + 1 < n, or i < (n-1)/2.  If n is even = 2*j, this is
       (2*j-1)/2 = j-1/2 so j-1 is the largest, which is n//2 - 1.  If
       n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
       and that's again n//2-1.
    */
    for (i=n/2-1 ; i>=0 ; i--)
        if(_siftup((PyListObject *)heap, i) == -1)
            return NULL;
    Py_INCREF(Py_None);
    return Py_None;
}
Пример #6
0
static PyObject *
nlargest(PyObject *self, PyObject *args)
{
    PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
    Py_ssize_t i, n;
    int cmp;

    if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable))
        return NULL;

    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;

    heap = PyList_New(0);
    if (heap == NULL)
        goto fail;

    for (i=0 ; i<n ; i++ ){
        elem = PyIter_Next(it);
        if (elem == NULL) {
            if (PyErr_Occurred())
                goto fail;
            else
                goto sortit;
        }
        if (PyList_Append(heap, elem) == -1) {
            Py_DECREF(elem);
            goto fail;
        }
        Py_DECREF(elem);
    }
    if (PyList_GET_SIZE(heap) == 0)
        goto sortit;

    for (i=n/2-1 ; i>=0 ; i--)
        if(_siftup((PyListObject *)heap, i) == -1)
            goto fail;

    sol = PyList_GET_ITEM(heap, 0);
    while (1) {
        elem = PyIter_Next(it);
        if (elem == NULL) {
            if (PyErr_Occurred())
                goto fail;
            else
                goto sortit;
        }
        cmp = PyObject_RichCompareBool(sol, elem, Py_LT);
        if (cmp == -1) {
            Py_DECREF(elem);
            goto fail;
        }
        if (cmp == 0) {
            Py_DECREF(elem);
            continue;
        }
        oldelem = PyList_GET_ITEM(heap, 0);
        PyList_SET_ITEM(heap, 0, elem);
        Py_DECREF(oldelem);
        if (_siftup((PyListObject *)heap, 0) == -1)
            goto fail;
        sol = PyList_GET_ITEM(heap, 0);
    }
sortit:
    if (PyList_Sort(heap) == -1)
        goto fail;
    if (PyList_Reverse(heap) == -1)
        goto fail;
    Py_DECREF(it);
    return heap;

fail:
    Py_DECREF(it);
    Py_XDECREF(heap);
    return NULL;
}
Пример #7
0
void _heapify(Heap * self) {
    int i;
    for (i = self->length / 2 - 1; i >=0; i--) {
        _siftup(self, i);
    }
}