[Python-checkins] python/dist/src/Lib/test test_heapq.py,1.4,1.5

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 02 Aug 2002 19:11:28 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv18315/python/lib/test

Modified Files:
	test_heapq.py 
Log Message:
Minor fiddling, including a simple class to implement a heap iterator
in the test file.  I have docs for heapq.heapify ready to check in, but
Jack appears to have left behind a stale lock in the Doc/lib directory.


Index: test_heapq.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_heapq.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** test_heapq.py	2 Aug 2002 21:48:06 -0000	1.4
--- test_heapq.py	3 Aug 2002 02:11:26 -0000	1.5
***************
*** 13,16 ****
--- 13,30 ----
              verify(heap[parentpos] <= item)
  
+ # An iterator returning a heap's elements, smallest-first.
+ class heapiter(object):
+     def __init__(self, heap):
+         self.heap = heap
+ 
+     def next(self):
+         try:
+             return heappop(self.heap)
+         except IndexError:
+             raise StopIteration
+ 
+     def __iter__(self):
+         return self
+ 
  def test_main():
      # 1) Push 100 random numbers and pop them off, verifying all's OK.
***************
*** 48,52 ****
      # 5) Less-naive "N-best" algorithm, much faster (if len(data) is big
      #    enough <wink>) than sorting all of data.  However, if we had a max
!     #    heap instead of a min heap, it would go much faster still via
      #    heapify'ing all of data (linear time), then doing 10 heappops
      #    (10 log-time steps).
--- 62,66 ----
      # 5) Less-naive "N-best" algorithm, much faster (if len(data) is big
      #    enough <wink>) than sorting all of data.  However, if we had a max
!     #    heap instead of a min heap, it could go faster still via
      #    heapify'ing all of data (linear time), then doing 10 heappops
      #    (10 log-time steps).
***************
*** 54,62 ****
      heapify(heap)
      for item in data[10:]:
!         if item > heap[0]:  # this gets rarer and rarer the longer we run
              heappush(heap, item)
!             heappop(heap)
!     heap.sort()
!     vereq(heap, data_sorted[-10:])
      # Make user happy
      if verbose:
--- 68,75 ----
      heapify(heap)
      for item in data[10:]:
!         if item > heap[0]:  # this gets rarer the longer we run
!             heappop(heap)       # we know heap[0] isn't in best 10 anymore
              heappush(heap, item)
!     vereq(list(heapiter(heap)), data_sorted[-10:])
      # Make user happy
      if verbose: