New submission from Raymond Hettinger <[EMAIL PROTECTED]>:

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 <[EMAIL PROTECTED]>
<http://bugs.python.org/issue2274>
__________________________________
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to