Commit 54da9819 authored by Raymond Hettinger's avatar Raymond Hettinger

Add tie-breaker count to preserve sort stability.

parent 00166c55
...@@ -323,10 +323,10 @@ def merge(*iterables): ...@@ -323,10 +323,10 @@ def merge(*iterables):
h = [] h = []
h_append = h.append h_append = h.append
for it in map(iter, iterables): for itnum, it in enumerate(map(iter, iterables)):
try: try:
next = it.next next = it.next
h_append([next(), next]) h_append([next(), itnum, next])
except _StopIteration: except _StopIteration:
pass pass
heapify(h) heapify(h)
...@@ -334,12 +334,12 @@ def merge(*iterables): ...@@ -334,12 +334,12 @@ def merge(*iterables):
while 1: while 1:
try: try:
while 1: 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 yield v
s[0] = next() # raises StopIteration when exhausted s[0] = next() # raises StopIteration when exhausted
siftup(h, 0) # restore heap condition siftup(h, 0) # restore heap condition
except _StopIteration: except _StopIteration:
_heappop(h) # remove empty iterator _heappop(h) # remove empty iterator
except IndexError: except IndexError:
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment