|
131 | 131 | (with-slots (left right) rope
|
132 | 132 | (rotate-left (concat-rope* left (rotate-right right)))))
|
133 | 133 |
|
134 |
| -;; (defun balance-children (rope) |
135 |
| -;; (with-slots (left right) rope |
136 |
| -;; (concat-rope* (balance-rope left) |
137 |
| -;; (balance-rope right)))) |
138 |
| - |
139 | 134 | (defgeneric balance-rope (rope)
|
140 | 135 | (:method ((rope leaf))
|
141 | 136 | rope)
|
|
183 | 178 | (merge-leaves leaves mid end)))))))
|
184 | 179 |
|
185 | 180 | (defun rebuild-rope (rope)
|
186 |
| - "Balance a rope by reconstructing it from the bottom up." |
| 181 | + "Reconstruct a rope from the bottom up. |
| 182 | +Doing this occasionally can reduce the number of leaves in a rope, |
| 183 | +but it is expensive - O(n)." |
187 | 184 | (let ((leaves (normalize-leaves (collect-rope rope))))
|
188 | 185 | (merge-leaves leaves 0 (length leaves))))
|
189 | 186 |
|
|
219 | 216 | (with-slots (left right) rope
|
220 | 217 | (concat-rope* left (append-rope right source)))))
|
221 | 218 |
|
222 |
| -;; (defgeneric prepend-rope (rope source) |
223 |
| -;; (:documentation "Return a new rope with a string or rope inserted at the beginning of a rope.") |
224 |
| -;; (:method (rope (source rope)) |
225 |
| -;; (concat-rope source rope)) |
226 |
| -;; (:method (rope (source t)) |
227 |
| -;; (concat-rope (make-rope source) rope))) |
228 |
| - |
229 |
| -;; (defgeneric append-rope (rope source) |
230 |
| -;; (:documentation "Return a new rope with a string or rope inserted at the end of a rope.") |
231 |
| -;; (:method (rope (source rope)) |
232 |
| -;; (concat-rope rope source)) |
233 |
| -;; (:method (rope (source t)) |
234 |
| -;; (concat-rope rope (make-rope source)))) |
235 |
| - |
236 | 219 | (defun insert-rope (rope index str)
|
237 | 220 | "Return a new rope with a string or rope inserted at the specified index of a rope."
|
238 | 221 | (cond ((= index 0) (prepend-rope rope str))
|
|
0 commit comments