[Zope-CVS] CVS: Products/ZCTextIndex - INBest.py: NBest.py:

Tim Peters tim.one@comcast.net
Sun, 5 May 2002 21:46:26 -0400

Update of /cvs-repository/Products/ZCTextIndex
In directory cvs.zope.org:/tmp/cvs-serv31723

Modified Files:
      Tag: TextIndexDS9-branch
	INBest.py NBest.py 
Log Message:
New method NBest.pop_smallest().  This is key to doing multiple unions
efficiently (which we aren't doing yet).

NBest.getbest():  Rewrite in the obvious way.  When I wrote this, I
believe I was under the illusion that zip() wasn't available in 2.1.

=== Products/ZCTextIndex/INBest.py => ===
     def getbest():
         """Return the (at most) N best-scoring items as a sequence.
         The return value is a sequence of 2-tuples, (item, score), with
         the largest score first.  If .add() has been called fewer than
         N times, this sequence will contain fewer than N pairs.
+        """
+    def pop_smallest():
+        """Return and remove the (item, score) pair with lowest score.
+        If len(self) is 0, raise IndexError.
+        To be cleaer, this is the lowest score among the N best-scoring
+        seen so far.  This is most useful if the capacity of the NBest
+        object is never exceeded, in which case  pop_smallest() allows
+        using the object as an ordinary smallest-in-first-out priority
+        queue.
     def __len__():

=== Products/ZCTextIndex/NBest.py => ===
     def getbest(self):
-        n = len(self.scores)
-        return [(self.items[i], self.scores[i])
-                    for i in range(n-1, -1, -1)]
+        result = zip(self.items, self.scores)
+        result.reverse()
+        return result
+    def pop_smallest(self):
+        if self.scores:
+            result = (self.items[0], self.scores[0])
+            del self.items[0], self.scores[0]
+            return result
+        raise IndexError("pop_smallest() called on empty NBest object")