This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: heapq API
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger
Priority: normal Keywords:

Created on 2008-03-11 21:00 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg63465 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-11 21:00
The heapreplace() function has an irritating API.  Since the replacement
is unconditional, it usually obligates the caller to examine the top of
the heap to see if it is smaller or larger than the candidate
replacement item.  Typical calls look like this:

    if item > heap[0]:
        item = heapreplace(heap, item)

Instead of the current design "x=heappop(h); heappush(h, item); return
x", it would be better to have a function equivalent to
"heappush(h,item); return heappop(h)".  The above fragment then
simplifies to:

    item = heappushpop(heap, item)

I propose to add heappushpop() to Py2.6 and to remove heapreplace() from
Py3.0:

    def heappushpop(heap, item):
        """Fast version of a heappush followed by a heappop."""
        if item > heap[0]:
            item, heap[0] = heap[0], item
            _siftup(heap, 0)
        return item
msg63509 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-13 19:04
See r61369.
History
Date User Action Args
2022-04-11 14:56:31adminsetgithub: 46527
2008-03-13 19:04:40rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg63509
2008-03-11 21:01:38rhettingersetversions: + Python 3.0, - Python 2.5
2008-03-11 21:00:31rhettingercreate