summaryrefslogtreecommitdiff
path: root/Lib/heapq.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-05-26 00:58:56 -0700
committerRaymond Hettinger <python@rcn.com>2014-05-26 00:58:56 -0700
commit41331e8193041f5de6134e82c7d728df0111c537 (patch)
tree7c2e1f159cb5e492dae05d17ec07a79c27576017 /Lib/heapq.py
parent79cae680a3c84477fdcadeed14f8392842d93c01 (diff)
downloadcpython-git-41331e8193041f5de6134e82c7d728df0111c537.tar.gz
Minor clean-ups for heapq.
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r--Lib/heapq.py22
1 files changed, 11 insertions, 11 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index 41626c56a7..121a9bb437 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -127,8 +127,6 @@ From all times, sorting has always been a Great Art! :-)
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
'nlargest', 'nsmallest', 'heappushpop']
-from itertools import islice, count
-
def heappush(heap, item):
"""Push item onto heap, maintaining the heap invariant."""
heap.append(item)
@@ -378,8 +376,10 @@ def merge(*iterables):
# 2 n - k compare remaining elements to top of heap
# 3 k * (1 + lg2(k)) * ln(n/k) replace the topmost value on the heap
# 4 k * lg2(k) - (k/2) final sort of the k most extreme values
+#
# Combining and simplifying for a rough estimate gives:
-# comparisons = n + k * (1 + log(n/k)) * (1 + log(k, 2))
+#
+# comparisons = n + k * (log(k, 2) * log(n/k)) + log(k, 2) + log(n/k))
#
# Computing the number of comparisons for step 3:
# -----------------------------------------------
@@ -391,12 +391,12 @@ def merge(*iterables):
# * The probabilty times the cost gives:
# (k/i) * (1 + log(k, 2))
# * Summing across the remaining n-k elements gives:
-# sum((k/i) * (1 + log(k, 2)) for xrange(k+1, n+1))
+# sum((k/i) * (1 + log(k, 2)) for i in range(k+1, n+1))
# * This reduces to:
# (H(n) - H(k)) * k * (1 + log(k, 2))
# * Where H(n) is the n-th harmonic number estimated by:
# gamma = 0.5772156649
-# H(n) = log(n, e) + gamma + 1.0 / (2.0 * n)
+# H(n) = log(n, e) + gamma + 1 / (2 * n)
# http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence
# * Substituting the H(n) formula:
# comparisons = k * (1 + log(k, 2)) * (log(n/k, e) + (1/n - 1/k) / 2)
@@ -445,12 +445,12 @@ def nsmallest(n, iterable, key=None):
# When key is none, use simpler decoration
if key is None:
it = iter(iterable)
- result = list(islice(zip(it, count()), n))
+ result = [(elem, i) for i, elem in zip(range(n), it)]
if not result:
return result
_heapify_max(result)
- order = n
top = result[0][0]
+ order = n
_heapreplace = _heapreplace_max
for elem in it:
if elem < top:
@@ -466,8 +466,8 @@ def nsmallest(n, iterable, key=None):
if not result:
return result
_heapify_max(result)
- order = n
top = result[0][0]
+ order = n
_heapreplace = _heapreplace_max
for elem in it:
k = key(elem)
@@ -506,12 +506,12 @@ def nlargest(n, iterable, key=None):
# When key is none, use simpler decoration
if key is None:
it = iter(iterable)
- result = list(islice(zip(it, count(0, -1)), n))
+ result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)]
if not result:
return result
heapify(result)
- order = -n
top = result[0][0]
+ order = -n
_heapreplace = heapreplace
for elem in it:
if top < elem:
@@ -527,8 +527,8 @@ def nlargest(n, iterable, key=None):
if not result:
return result
heapify(result)
- order = -n
top = result[0][0]
+ order = -n
_heapreplace = heapreplace
for elem in it:
k = key(elem)