[POS-commit] Kiwi/Kiwi List.py,1.86,1.87 Delegates.py,1.124,1.125

Christian Robottom Reis kiko at async.com.br
Sat Oct 25 01:06:44 BRDT 2003


Update of /cvs/Kiwi/Kiwi
In directory anthem:/tmp/cvs-serv22532/Kiwi

Modified Files:
	List.py Delegates.py 
Log Message:
Rehacked the binary search/insertion function that Pinazo and I 
wrote to use the simplified algorithm that Neil Rashbrook implemented in
Mozilla's config.js at
http://lxr.mozilla.org/seamonkey/source/xpfe/global/resources/content/config.js#195

Also renamed the function to _locate_nearest_row().


Index: Delegates.py
===================================================================
RCS file: /cvs/Kiwi/Kiwi/Delegates.py,v
retrieving revision 1.124
retrieving revision 1.125
diff -u -d -r1.124 -r1.125
--- Delegates.py	16 Oct 2003 01:39:29 -0000	1.124
+++ Delegates.py	25 Oct 2003 00:06:42 -0000	1.125
@@ -668,7 +668,7 @@
         i = 0
 
         _get_text = self._get_instance_text
-        _get_row = clist._get_insert_sorted_row
+        _get_row = clist._locate_nearest_row
         _insert = clist._raw_insert
         _set_row_data = clist.set_row_data
         _o = clist._o

Index: List.py
===================================================================
RCS file: /cvs/Kiwi/Kiwi/List.py,v
retrieving revision 1.86
retrieving revision 1.87
diff -u -d -r1.86 -r1.87
--- List.py	24 Oct 2003 23:45:38 -0000	1.86
+++ List.py	25 Oct 2003 00:06:42 -0000	1.87
@@ -984,7 +984,7 @@
 column sorting. Look at enable_column_sort() for more details."""
         self.typelist = typelist
 
-    def _get_insert_sorted_row(self, text, column, decimal=None, convert=None):
+    def _locate_nearest_row(self, text, column, decimal=None, convert=None):
         """ Return the correct row to insert sorted in the clist. It
         uses a binary search to find the correct row number to insert. """
         if not self.rows:
@@ -1004,46 +1004,27 @@
         translate = string.translate
         # Slightly modified binary search to deal with the fact that the
         # list may not contain the element.
-        low = 0
-        high = self.rows - 1
-        while low <= high:
-            middle = (low + high) / 2
-
+        low = -1
+        high = self.rows
+        middle = (low + high) / 2
+        while middle > low:
             rowtext = _raw_get_text(self._o, middle, column)
             if decimal:
                 rowtext = translate(rowtext, decimal)
             value = cmp(convert(text or 0), convert(rowtext or 0)) * inc
 
-            # We're down to a window of one element, just decide where
-            # to put ours
-            if high - low == 0:
-                if value == 1:
-                    return middle + 1
-                return middle
-            # With a window of two, the decision is which of the two we
-            # want to be.
-            if high - low == 1:
-                # assert low == middle == high
-                if value == 1:
-                    # We haven't checked high yet, so spin once more
-                    low = high
-                    continue
-                else:
-                    # assert low == middle
-                    # We can be sure that low is the correct value,
-                    # since we've already compared lower with it
-                    return low
             # text is less than the rowtext
             if value == -1:
-                high = middle - 1
+                high = middle
             # text is more than the rowtext
             elif value == +1:
-                low = middle + 1
+                low = middle
             # text is equal to the rowtext
             else:
                 return middle
+            middle = (low + high) / 2
 
-        raise AssertionError, (text, rowtext, low, middle, high)
+        return high
             
     def insert_sorted(self, values, data=None):
         """
@@ -1067,7 +1048,7 @@
             decimal = self._decimal_hack[column]
         else:
             decimal = None
-        insert_row = self._get_insert_sorted_row(text, column, decimal)
+        insert_row = self._locate_nearest_row(text, column, decimal)
 
         # row is -1 if the row is to be inserted before position 0 or
         # the list is empty. insert() knows how to handle this properly,
@@ -1248,7 +1229,7 @@
         elif USE_MX and tp == DateTimeType:
             if self.date_formats.has_key(column):
                 format = self.date_formats[column]
-                return lambda x, format=format: strptime(x, format)
+                return lambda x, format=format: x and strptime(x, format) or ""
             else:
                 return DateTimeFrom # Slow but works with most dates
         return lambda x: x



More information about the POS-commit mailing list