Skip to content

Commit beaf915

Browse files
Add C imp
1 parent eccf484 commit beaf915

File tree

4 files changed

+119
-54
lines changed

4 files changed

+119
-54
lines changed

Lib/heapq.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,18 +595,6 @@ def nlargest(n, iterable, key=None):
595595
from _heapq import *
596596
except ImportError:
597597
pass
598-
try:
599-
from _heapq import heapreplace_max
600-
except ImportError:
601-
pass
602-
try:
603-
from _heapq import heapify_max
604-
except ImportError:
605-
pass
606-
try:
607-
from _heapq import heappop_max
608-
except ImportError:
609-
pass
610598

611599

612600
if __name__ == "__main__":

Lib/test/test_heapq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
1515
# _heapq is imported, so check them there
1616
func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace',
17-
'heappop_max', 'heapreplace_max', 'heapify_max']
17+
'heappop_max', 'heapreplace_max', 'heapify_max', 'heappushpop_max',]
1818

1919
class TestModules(TestCase):
2020
def test_py_functions(self):
@@ -23,7 +23,7 @@ def test_py_functions(self):
2323

2424
@skipUnless(c_heapq, 'requires _heapq')
2525
def test_c_functions(self):
26-
for fname in ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace']:
26+
for fname in func_names:
2727
self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
2828

2929

Modules/_heapqmodule.c

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
482482

483483

484484
/*[clinic input]
485-
_heapq._heappop_max
485+
_heapq.heappop_max
486486
487487
heap: object(subclass_of='&PyList_Type')
488488
/
@@ -491,14 +491,14 @@ Maxheap variant of heappop.
491491
[clinic start generated code]*/
492492

493493
static PyObject *
494-
_heapq__heappop_max_impl(PyObject *module, PyObject *heap)
495-
/*[clinic end generated code: output=9e77aadd4e6a8760 input=362c06e1c7484793]*/
494+
_heapq_heappop_max_impl(PyObject *module, PyObject *heap)
495+
/*[clinic end generated code: output=2f051195ab404b77 input=e62b14016a5a26de]*/
496496
{
497497
return heappop_internal(heap, siftup_max);
498498
}
499499

500500
/*[clinic input]
501-
_heapq._heapreplace_max
501+
_heapq.heapreplace_max
502502
503503
heap: object(subclass_of='&PyList_Type')
504504
item: object
@@ -508,15 +508,14 @@ Maxheap variant of heapreplace.
508508
[clinic start generated code]*/
509509

510510
static PyObject *
511-
_heapq__heapreplace_max_impl(PyObject *module, PyObject *heap,
512-
PyObject *item)
513-
/*[clinic end generated code: output=8ad7545e4a5e8adb input=f2dd27cbadb948d7]*/
511+
_heapq_heapreplace_max_impl(PyObject *module, PyObject *heap, PyObject *item)
512+
/*[clinic end generated code: output=8770778b5a9cbe9b input=21a3d28d757c881c]*/
514513
{
515514
return heapreplace_internal(heap, item, siftup_max);
516515
}
517516

518517
/*[clinic input]
519-
_heapq._heapify_max
518+
_heapq.heapify_max
520519
521520
heap: object(subclass_of='&PyList_Type')
522521
/
@@ -525,21 +524,63 @@ Maxheap variant of heapify.
525524
[clinic start generated code]*/
526525

527526
static PyObject *
528-
_heapq__heapify_max_impl(PyObject *module, PyObject *heap)
529-
/*[clinic end generated code: output=2cb028beb4a8b65e input=c1f765ee69f124b8]*/
527+
_heapq_heapify_max_impl(PyObject *module, PyObject *heap)
528+
/*[clinic end generated code: output=8401af3856529807 input=edda4255728c431e]*/
530529
{
531530
return heapify_internal(heap, siftup_max);
532531
}
533532

533+
/*[clinic input]
534+
_heapq.heappushpop_max
535+
536+
heap: object(subclass_of='&PyList_Type')
537+
item: object
538+
/
539+
540+
Maxheap variant of heappushpop.
541+
542+
The combined action runs more efficiently than heappush_max() followed by
543+
a separate call to heappop_max().
544+
[clinic start generated code]*/
545+
546+
static PyObject *
547+
_heapq_heappushpop_max_impl(PyObject *module, PyObject *heap, PyObject *item)
548+
/*[clinic end generated code: output=ff0019f0941aca0d input=525a843013cbd6c0]*/
549+
{
550+
PyObject *returnitem;
551+
int cmp;
552+
if (PyList_GET_SIZE(heap) == 0) {
553+
return Py_NewRef(item);
554+
}
555+
PyObject *top = PyList_GET_ITEM(heap, 0);
556+
Py_INCREF(top);
557+
cmp = PyObject_RichCompareBool(top, item, Py_LT);
558+
Py_DECREF(top);
559+
if (cmp < 0)
560+
return NULL;
561+
if (cmp == 0) {
562+
return Py_NewRef(item);
563+
}
564+
returnitem = PyList_GET_ITEM(heap, 0);
565+
PyList_SET_ITEM(heap, 0, Py_NewRef(item));
566+
567+
if (siftup_max((PyListObject *)heap, 0)) {
568+
Py_DECREF(returnitem);
569+
return NULL;
570+
}
571+
return Py_NewRef(returnitem);
572+
}
573+
534574
static PyMethodDef heapq_methods[] = {
535575
_HEAPQ_HEAPPUSH_METHODDEF
536576
_HEAPQ_HEAPPUSHPOP_METHODDEF
537577
_HEAPQ_HEAPPOP_METHODDEF
538578
_HEAPQ_HEAPREPLACE_METHODDEF
539579
_HEAPQ_HEAPIFY_METHODDEF
540-
_HEAPQ__HEAPPOP_MAX_METHODDEF
541-
_HEAPQ__HEAPIFY_MAX_METHODDEF
542-
_HEAPQ__HEAPREPLACE_MAX_METHODDEF
580+
_HEAPQ_HEAPPOP_MAX_METHODDEF
581+
_HEAPQ_HEAPIFY_MAX_METHODDEF
582+
_HEAPQ_HEAPREPLACE_MAX_METHODDEF
583+
_HEAPQ_HEAPPUSHPOP_MAX_METHODDEF
543584
{NULL, NULL} /* sentinel */
544585
};
545586

Modules/clinic/_heapqmodule.c.h

Lines changed: 63 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)