Always grow string by fixed ratio to guarantee constant amortized cost
authorChris Hanson <org/chris-hanson/cph>
Tue, 19 Jan 1993 05:33:49 +0000 (05:33 +0000)
committerChris Hanson <org/chris-hanson/cph>
Tue, 19 Jan 1993 05:33:49 +0000 (05:33 +0000)
of insertion.

v7/src/runtime/strout.scm

index 89be585c39e855c03fb6b0df3c7887243f668c39..92fa88ec4977c30df5cb985c0ad24262c8021e19 100644 (file)
@@ -1,8 +1,8 @@
 #| -*-Scheme-*-
 
-$Id: strout.scm,v 14.6 1993/01/18 16:50:09 gjr Exp $
+$Id: strout.scm,v 14.7 1993/01/19 05:33:49 cph Exp $
 
-Copyright (c) 1988-1993 Massachusetts Institute of Technology
+Copyright (c) 1988-93 Massachusetts Institute of Technology
 
 This material was developed by the Scheme project at the Massachusetts
 Institute of Technology, Department of Electrical Engineering and
@@ -66,14 +66,15 @@ MIT in each case. |#
   accumulator
   counter)
 
-(define (grow-accumulator! state min-n*)
+(define (grow-accumulator! state min-size)
   (let* ((old (output-string-state/accumulator state))
         (n (string-length old))
-        (n* (+ n n))
-        (new (make-string
-              (if (< n* min-n*)
-                  min-n*
-                  n*))))
+        (new
+         (make-string
+          (let loop ((n (fix:+ n n)))
+            (if (fix:>= n min-size)
+                n
+                (loop (fix:+ n n)))))))
     (substring-move-left! old 0 n new 0)
     (set-output-string-state/accumulator! state new)))