From c929766361b4e0ea5bf5dc625e326fb954956f0b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 12 Jun 2004 22:48:46 +0000 Subject: Install C version of heapq.nlargest(). Maxheap version of heapq.smallest() is forthcoming. --- Modules/_heapqmodule.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 7455fbc..8fe742f 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