Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eccf484
Initial addition
StanFromIreland Mar 1, 2025
beaf915
Add C imp
StanFromIreland Mar 1, 2025
c143ae2
Benedikts suggestions
StanFromIreland Mar 1, 2025
167525d
Benedikts suggestions
StanFromIreland Mar 1, 2025
1b0b6f3
Update Modules/_heapqmodule.c with Benedikts suggestion
StanFromIreland Mar 1, 2025
fc46707
Fix mistake (extra underscores)
StanFromIreland Mar 1, 2025
f4fd94a
Benedikt's requested changes
StanFromIreland Mar 1, 2025
cebbc88
Missed one of Benedikt's requested changes
StanFromIreland Mar 1, 2025
3cde6c6
Benedikts suggestion
StanFromIreland Mar 1, 2025
5d2d387
Benedikts Suggestions
StanFromIreland Mar 2, 2025
a499cd4
Fix doc warnings
StanFromIreland Mar 2, 2025
abe0a95
Improve entries
StanFromIreland Mar 2, 2025
3561206
Address some of Petr's suggestions
StanFromIreland Mar 10, 2025
8fd1a03
Clean up and add missing
StanFromIreland Mar 10, 2025
8ab97c2
Update Doc/library/heapq.rst
StanFromIreland Mar 17, 2025
623cae7
Merge branch 'main' into add-heapq-max
StanFromIreland Apr 25, 2025
81db251
Sort and add missing C implementation
StanFromIreland May 2, 2025
38cbf13
Petr's list suggestion
StanFromIreland May 2, 2025
b6f4db4
heappushpop_max fixup
StanFromIreland May 2, 2025
61c9285
Improve test
StanFromIreland May 4, 2025
ebe00dc
Switch to <
StanFromIreland May 5, 2025
bc0dd66
Clean up test
StanFromIreland May 5, 2025
988b2d3
Reword the docs
encukou May 5, 2025
6efd70c
Add max-heap variants for the other tests
encukou May 5, 2025
4ba533b
Apply suggestions from code review
encukou May 5, 2025
160bc35
Merge pull request #1 from encukou/add-heapq-max
StanFromIreland May 5, 2025
742c46c
final touchups
StanFromIreland May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def heappush_max(heap, item):

def heappushpop_max(heap, item):
"""Maxheap fast version of a heappush followed by a heappop."""
if heap and heap[0] < item:
if heap and heap[0] > item:
item, heap[0] = heap[0], item
_siftup_max(heap, 0)
return item
Expand Down
11 changes: 7 additions & 4 deletions Lib/test/test_heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ def test_heappushpop_max(self):

h = [10]
x = self.module.heappushpop_max(h, 11)
self.assertTupleEqual((h, x), ([11], 10))
self.assertTupleEqual((h, x), ([10], 11))

h = [10]
x = self.module.heappushpop_max(h, 9)
self.assertTupleEqual((h, x), ([10], 9))
self.assertTupleEqual((h, x), ([9], 10))

def test_heappop_max(self):
# heapop_max has an optimization for one-item lists which isn't
Expand Down Expand Up @@ -515,13 +515,16 @@ class EvilClass(int):
def __lt__(self, o):
heap.clear()
return NotImplemented
def __gt__(self, o):
heap.clear()
return NotImplemented

heap = []
self.module.heappush(heap, EvilClass(0))
self.assertRaises(IndexError, self.module.heappushpop, heap, 1)
heap = []
self.module.heappush_max(heap, EvilClass(0))
self.assertRaises(IndexError, self.module.heappushpop_max, heap, 1)
self.module.heappush_max(heap, EvilClass(1))
self.assertRaises(IndexError, self.module.heappushpop_max, heap, 0)

def test_comparison_operator_modifiying_heap_two_heaps(self):

Expand Down
2 changes: 1 addition & 1 deletion Modules/_heapqmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ _heapq_heappushpop_max_impl(PyObject *module, PyObject *heap, PyObject *item)

PyObject *top = PyList_GET_ITEM(heap, 0);
Py_INCREF(top);
cmp = PyObject_RichCompareBool(top, item, Py_LT);
cmp = PyObject_RichCompareBool(top, item, Py_GT);
Py_DECREF(top);
if (cmp < 0) {
return NULL;
Expand Down
Loading