[issue2274] heapq API

Raymond Hettinger report at bugs.python.org
Tue Mar 11 22:00:34 CET 2008


New submission from Raymond Hettinger <rhettinger at users.sourceforge.net>:

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

----------
components: Library (Lib)
messages: 63465
nosy: rhettinger
severity: normal
status: open
title: heapq API
type: feature request
versions: Python 2.5, Python 2.6

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2274>
__________________________________


More information about the Python-bugs-list mailing list