[pypy-svn] r39839 - in pypy/branch/rope-branch/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sat Mar 3 19:09:27 CET 2007


Author: arigo
Date: Sat Mar  3 19:09:27 2007
New Revision: 39839

Modified:
   pypy/branch/rope-branch/pypy/objspace/std/rope.py
   pypy/branch/rope-branch/pypy/objspace/std/test/test_rope.py
Log:
(cfbolz, arigo)

Test and temporary fix for it.  We're going to hack more on it but it
passes the tests now.



Modified: pypy/branch/rope-branch/pypy/objspace/std/rope.py
==============================================================================
--- pypy/branch/rope-branch/pypy/objspace/std/rope.py	(original)
+++ pypy/branch/rope-branch/pypy/objspace/std/rope.py	Sat Mar  3 19:09:27 2007
@@ -322,42 +322,51 @@
     return rebalance(nodelist, length)
 
 def rebalance(nodelist, sizehint=-1):
-    if not nodelist:
-        return LiteralStringNode("")
     nodelist.reverse()
     if sizehint < 0:
         sizehint = 0
         for node in nodelist:
             sizehint += node.length()
+    if sizehint == 0:
+        return LiteralStringNode("")
+
+    # this code is based on the Fibonacci identity:
+    #   sum(fib(i) for i in range(n+1)) == fib(n+2)
     l = [None] * (find_fib_index(sizehint) + 2)
     stack = nodelist
     i = 0
     curr = None
     while stack:
         curr = stack.pop()
-        while 1:
-            if isinstance(curr, BinaryConcatNode) and not curr.balanced:
-                stack.append(curr.right)
-                curr = curr.left
+        while isinstance(curr, BinaryConcatNode) and not curr.balanced:
+            stack.append(curr.right)
+            curr = curr.left
+
+        currlen = curr.length()
+        if currlen == 0:
+            continue
+        i = 0
+        a, b = 1, 2
+        while not (currlen < b and l[i] is None):
+            if l[i] is not None:
+                curr = concatenate(l[i], curr)
+                l[i] = None
+                currlen = curr.length()
             else:
-                i = orig_i = find_fib_index(curr.length())
-                index = 0
-                added = False
-                while index <= i:
-                    if l[index] is not None:
-                        curr = concatenate(l[index], curr)
-                        l[index] = None
-                        if index >= orig_i or not added:
-                            i += 1
-                            added = True
-                    index += 1
-                if i == len(l):
-                    return curr
-                l[i] = curr
-                break
-    for index in range(i + 1, len(l)):
+                i += 1
+                a, b = b, a+b
+        if i == len(l):
+            return curr
+        l[i] = curr
+
+    #for index in range(i + 1, len(l)):
+    curr = None
+    for index in range(len(l)):
         if l[index] is not None:
-            curr = BinaryConcatNode(l[index], curr)
+            if curr is None:
+                curr = l[index]
+            else:
+                curr = BinaryConcatNode(l[index], curr)
     assert curr is not None
     curr.check_balanced()
     return curr

Modified: pypy/branch/rope-branch/pypy/objspace/std/test/test_rope.py
==============================================================================
--- pypy/branch/rope-branch/pypy/objspace/std/test/test_rope.py	(original)
+++ pypy/branch/rope-branch/pypy/objspace/std/test/test_rope.py	Sat Mar  3 19:09:27 2007
@@ -181,6 +181,13 @@
         for i in range(node.length()):
             assert result1[i] == result2[i]
 
+    strings = ['', '<',
+               '/home/arigo/svn/pypy/branch/rope-branch/py/code/source.py',
+               ':', '213', '>']
+    l = [LiteralStringNode(s) for s in strings]
+    node = join(LiteralStringNode(""), l)
+    assert node.flatten() == ''.join(strings)
+
 def test_join_random():
     l, strs = zip(*[make_random_string(10 * i) for i in range(1, 5)])
     l = list(l)



More information about the Pypy-commit mailing list