Minor clean-ups for heapq.merge().

This commit is contained in:
Raymond Hettinger 2014-05-26 17:08:27 -07:00
parent 9f4c963e7a
commit ab09c1319b
1 changed files with 13 additions and 14 deletions

View File

@ -322,32 +322,31 @@ def merge(*iterables):
[0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25] [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
''' '''
_heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
_len = len
h = [] h = []
h_append = h.append h_append = h.append
for itnum, it in enumerate(map(iter, iterables)): for order, it in enumerate(map(iter, iterables)):
try: try:
next = it.__next__ next = it.__next__
h_append([next(), itnum, next]) h_append([next(), order, next])
except _StopIteration: except StopIteration:
pass pass
heapify(h) heapify(h)
while _len(h) > 1: _heapreplace = heapreplace
while len(h) > 1:
try: try:
while True: while True:
v, itnum, next = s = h[0] value, order, next = s = h[0]
yield v yield value
s[0] = next() # raises StopIteration when exhausted s[0] = next() # raises StopIteration when exhausted
_heapreplace(h, s) # restore heap condition _heapreplace(h, s) # restore heap condition
except _StopIteration: except StopIteration:
_heappop(h) # remove empty iterator heappop(h) # remove empty iterator
if h: if h:
# fast case when only a single iterator remains # fast case when only a single iterator remains
v, itnum, next = h[0] value, order, next = h[0]
yield v yield value
yield from next.__self__ yield from next.__self__