[pypy-svn] r49346 - pypy/dist/pypy/rlib

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Dec 4 17:38:00 CET 2007


Author: cfbolz
Date: Tue Dec  4 17:38:00 2007
New Revision: 49346

Modified:
   pypy/dist/pypy/rlib/rope.py
Log:
trying to give the searching of single char strings an extremely good best case:
O(log(n)) if the char is there and O(1) if the char is not there. Worst case is
still O(n), of course.


Modified: pypy/dist/pypy/rlib/rope.py
==============================================================================
--- pypy/dist/pypy/rlib/rope.py	(original)
+++ pypy/dist/pypy/rlib/rope.py	Tue Dec  4 17:38:00 2007
@@ -683,24 +683,36 @@
     length = node.length()
     if stop == -1:
         stop = length
+    if start != 0 or stop != length:
+        newstart, newstop, node = find_straddling(node, start, stop)
+        offset = start - newstart
+        start = newstart
+        stop = newstop
     assert 0 <= start <= stop
     if isinstance(node, LiteralNode):
         pos = node.find_int(what, start, stop)
         if pos == -1:
             return pos
         return pos + offset
-    iter = FringeIterator(node)
-    newstart = iter._seekforward(start)
-    offset += start - newstart
-    start = newstart
+    if not node.can_contain_int(what):
+        return -1
+    # invariant: stack should only contain nodes that can contain the int what
+    stack = [node]
     #import pdb; pdb.set_trace()
     i = 0
-    while i < stop:
-        try:
-            fringenode = iter.next()
-        except StopIteration:
-            return -1
-        nodelength = fringenode.length()
+    while stack:
+        curr = stack.pop()
+        while isinstance(curr, BinaryConcatNode):
+            if curr.left.can_contain_int(what):
+                if curr.right.can_contain_int(what):
+                    stack.append(curr.right)
+                curr = curr.left
+            else:
+                i += curr.left.length()
+                # if left cannot contain what, then right must contain it
+                curr = curr.right
+        nodelength = curr.length()
+        fringenode = curr
         if i + nodelength <= start:
             i += nodelength
             continue



More information about the Pypy-commit mailing list