mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +00:00
Issue 2274: Add heapq.heappushpop().
This commit is contained in:
parent
431f029486
commit
53bdf09343
5 changed files with 95 additions and 2 deletions
11
Lib/heapq.py
11
Lib/heapq.py
|
@ -127,7 +127,7 @@ From all times, sorting has always been a Great Art! :-)
|
|||
"""
|
||||
|
||||
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
|
||||
'nlargest', 'nsmallest']
|
||||
'nlargest', 'nsmallest', 'heappushpop']
|
||||
|
||||
from itertools import islice, repeat, count, imap, izip, tee
|
||||
from operator import itemgetter, neg
|
||||
|
@ -165,6 +165,13 @@ def heapreplace(heap, item):
|
|||
_siftup(heap, 0)
|
||||
return returnitem
|
||||
|
||||
def heappushpop(heap, item):
|
||||
"""Fast version of a heappush followed by a heappop."""
|
||||
if heap and item > heap[0]:
|
||||
item, heap[0] = heap[0], item
|
||||
_siftup(heap, 0)
|
||||
return item
|
||||
|
||||
def heapify(x):
|
||||
"""Transform list into a heap, in-place, in O(len(heap)) time."""
|
||||
n = len(x)
|
||||
|
@ -304,7 +311,7 @@ def _siftup(heap, pos):
|
|||
|
||||
# If available, use C implementation
|
||||
try:
|
||||
from _heapq import heappush, heappop, heapify, heapreplace, nlargest, nsmallest
|
||||
from _heapq import heappush, heappop, heapify, heapreplace, nlargest, nsmallest, heappushpop
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue