diff options
| author | Raymond Hettinger <python@rcn.com> | 2008-05-31 03:24:31 (GMT) |
|---|---|---|
| committer | Raymond Hettinger <python@rcn.com> | 2008-05-31 03:24:31 (GMT) |
| commit | 6d7702ecd11e067f72029244b3f7aa8baa3ed2fc (patch) | |
| tree | 7b20e6e1287adeb64fdd4e2c2033d9a8bcf3cb92 /Lib/heapq.py | |
| parent | adff65bc3e49f58c637d7cb5b72b10268b4d7efd (diff) | |
| download | cpython-6d7702ecd11e067f72029244b3f7aa8baa3ed2fc.zip cpython-6d7702ecd11e067f72029244b3f7aa8baa3ed2fc.tar.gz cpython-6d7702ecd11e067f72029244b3f7aa8baa3ed2fc.tar.bz2 | |
Implement heapq in terms of less-than (to match list.sort()).
Diffstat (limited to 'Lib/heapq.py')
| -rw-r--r-- | Lib/heapq.py | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py index 4af9af1..0a83b76 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -167,7 +167,7 @@ def heapreplace(heap, item): def heappushpop(heap, item): """Fast version of a heappush followed by a heappop.""" - if heap and item > heap[0]: + if heap and heap[0] < item: item, heap[0] = heap[0], item _siftup(heap, 0) return item @@ -240,10 +240,11 @@ def _siftdown(heap, startpos, pos): while pos > startpos: parentpos = (pos - 1) >> 1 parent = heap[parentpos] - if parent <= newitem: - break - heap[pos] = parent - pos = parentpos + if newitem < parent: + heap[pos] = parent + pos = parentpos + continue + break heap[pos] = newitem # The child indices of heap index pos are already heaps, and we want to make @@ -294,7 +295,7 @@ def _siftup(heap, pos): while childpos < endpos: # Set childpos to index of smaller child. rightpos = childpos + 1 - if rightpos < endpos and heap[rightpos] <= heap[childpos]: + if rightpos < endpos and not heap[childpos] < heap[rightpos]: childpos = rightpos # Move the smaller child up. heap[pos] = heap[childpos] |
