I was reading some of the Python standard library code and one piece stood out for me in the heapq.py module.
Merge sorted iterables using min heap:
def merge(*iterables):
'''Merge multiple sorted inputs into a single sorted output.
>>> list(merge([1,3,5,7], [0,2,4,8], [5,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_append = h.append
for itnum, it in enumerate(map(iter, iterables)):
try:
next = it.next
h_append([next(), itnum, next])
except _StopIteration:
pass
heapify(h)
while _len(h) > 1:
try:
while 1:
v, itnum, next = s = h[0]
yield v
s[0] = next() # raises StopIteration when exhausted
_heapreplace(h, s) # restore heap condition
except _StopIteration:
_heappop(h) # remove empty iterator
if h:
# fast case when only a single iterator remains
v, itnum, next = h[0]
yield v
for v in next.__self__:
yield v
If I'm not wrong, this is written by Raymond Hettinger and it's always a pleasure to read/use his code.
Merge sorted iterables using min heap:
If I'm not wrong, this is written by Raymond Hettinger and it's always a pleasure to read/use his code.