From: Raymond Hettinger Date: Sat, 12 Jun 2004 22:48:46 +0000 (+0000) Subject: Install C version of heapq.nlargest(). X-Git-Tag: v2.4a1~197 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c929766361b4e0ea5bf5dc625e326fb954956f0b;p=python Install C version of heapq.nlargest(). Maxheap version of heapq.smallest() is forthcoming. --- diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 7455fbc916..8fe742ff0e 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -216,6 +216,80 @@ heapify(PyObject *self, PyObject *heap) PyDoc_STRVAR(heapify_doc, "Transform list into a heap, in-place, in O(len(heap)) time."); +static PyObject * +nlargest(PyObject *self, PyObject *args) +{ + PyObject *heap=NULL, *elem, *rv, *iterable, *sol, *it, *oldelem; + int i, n; + + if (!PyArg_ParseTuple(args, "Oi:nlargest", &iterable, &n)) + return NULL; + + it = PyObject_GetIter(iterable); + if (it == NULL) + return NULL; + + heap = PyList_New(0); + if (it == NULL) + goto fail; + + for (i=0 ; i