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

Tim Peters tim.one@comcast.net
Wed, 1 May 2002 22:05:29 -0400


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

Modified Files:
      Tag: TextIndexDS9-branch
	INBest.py NBest.py 
Log Message:
Give an NBest object a batch-processing "addmany" method.  It's quicker.
Alas, the splitter stuff won't build on Win98 due to ExtensionBuilder.py
creating command lines longer than this OS can handle, so I didn't change
our *use* of NBest to match (can't test it here).  Will do tomorrow from
a Win2K box.


=== Products/ZCTextIndex/INBest.py 1.1.2.1 => 1.1.2.2 ===
         a number, and larger numbers are considered better.
         """
+    
+    def addmany(sequence):
+        """Like "for item, score in sequence: self.add(item, score)".
+
+        This is simply faster than calling add() len(seq) times.
+        """
+
 
     def getbest():
         """Return the (at most) N best-scoring items as a sequence.


=== Products/ZCTextIndex/NBest.py 1.1.2.1 => 1.1.2.2 ===
 
     def add(self, item, score):
-        scores = self.scores
-        # When we're in steady-state, the usual case is that we're filled
-        # to capacity, and that an incoming item is worse than any of
-        # the best-seen so far.
-        if len(scores) >= self._capacity and score <= scores[0]:
-            return
-        i = bisect(scores, score)
-        scores.insert(i, score)
-        self.items.insert(i, item)
-        if len(scores) > self._capacity:
-            del scores[0]
-            del self.items[0]
+        self.addmany([(item, score)])
+
+    def addmany(self, sequence):
+        scores, items, capacity = self.scores, self.items, self._capacity
+        n = len(scores)
+        for item, score in sequence:
+            # When we're in steady-state, the usual case is that we're filled
+            # to capacity, and that an incoming item is worse than any of
+            # the best-seen so far.
+            if n >= capacity and score <= scores[0]:
+                continue
+            i = bisect(scores, score)
+            scores.insert(i, score)
+            items.insert(i, item)
+            if n == capacity:
+                del scores[0]
+                del items[0]
+            else:
+                n += 1
+        assert n == len(scores)
 
     def getbest(self):
         n = len(self.scores)