mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Add tie-breaker count to preserve sort stability.
This commit is contained in:
parent
00166c5532
commit
54da9819cc
1 changed files with 6 additions and 6 deletions
12
Lib/heapq.py
12
Lib/heapq.py
|
@ -323,10 +323,10 @@ def merge(*iterables):
|
|||
|
||||
h = []
|
||||
h_append = h.append
|
||||
for it in map(iter, iterables):
|
||||
for itnum, it in enumerate(map(iter, iterables)):
|
||||
try:
|
||||
next = it.next
|
||||
h_append([next(), next])
|
||||
h_append([next(), itnum, next])
|
||||
except _StopIteration:
|
||||
pass
|
||||
heapify(h)
|
||||
|
@ -334,12 +334,12 @@ def merge(*iterables):
|
|||
while 1:
|
||||
try:
|
||||
while 1:
|
||||
v, next = s = h[0] # raises IndexError when h is empty
|
||||
v, itnum, next = s = h[0] # raises IndexError when h is empty
|
||||
yield v
|
||||
s[0] = next() # raises StopIteration when exhausted
|
||||
siftup(h, 0) # restore heap condition
|
||||
s[0] = next() # raises StopIteration when exhausted
|
||||
siftup(h, 0) # restore heap condition
|
||||
except _StopIteration:
|
||||
_heappop(h) # remove empty iterator
|
||||
_heappop(h) # remove empty iterator
|
||||
except IndexError:
|
||||
return
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue