From: Raymond Hettinger Date: Mon, 19 Feb 2007 05:28:28 +0000 (+0000) Subject: Add tie-breaker count to preserve sort stability. X-Git-Tag: v2.6a1~2163 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=54da9819cc74fe6091d090d12753116cfb6c6c62;p=thirdparty%2FPython%2Fcpython.git Add tie-breaker count to preserve sort stability. --- diff --git a/Lib/heapq.py b/Lib/heapq.py index b56d0f96bfd5..4c11eb6e8740 100644 --- a/Lib/heapq.py +++ b/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