|
35 | 35 | ;; Utils ;;
|
36 | 36 | ;;-------;;
|
37 | 37 |
|
| 38 | +(defun branch-weight (branch) |
| 39 | + (rope-length (branch-left branch))) |
| 40 | + |
| 41 | +(defun leaf-short-p (leaf) |
| 42 | + (>= *short-leaf* (rope-length leaf))) |
| 43 | + |
| 44 | +(defun strcat (a b) |
| 45 | + (concatenate 'string a b)) |
| 46 | + |
| 47 | +(defun make-leaf (string &optional length) |
| 48 | + (make-instance 'leaf :string string :length (or length (length string)))) |
| 49 | + |
38 | 50 | (defgeneric make-rope (source)
|
39 | 51 | (:documentation "Create a new rope from a string, stream, or pathname.")
|
40 | 52 | (:method ((source rope))
|
|
43 | 55 | (labels ((read-leaves (&optional acc)
|
44 | 56 | (let* ((string (make-string *long-leaf*))
|
45 | 57 | (length (read-sequence string source))
|
46 |
| - (leaf (make-instance 'leaf :length length :string (subseq string 0 length)))) |
| 58 | + (leaf (make-leaf (subseq string 0 length) length))) |
47 | 59 | (if (= *long-leaf* length)
|
48 | 60 | (read-leaves (cons leaf acc))
|
49 | 61 | (cons leaf acc)))))
|
|
57 | 69 | (if (<= *long-leaf* length)
|
58 | 70 | (concat-rope (make-rope (subseq source 0 (round length 2)))
|
59 | 71 | (make-rope (subseq source (round length 2))))
|
60 |
| - (make-instance 'leaf :length length :string source))))) |
61 |
| - |
62 |
| -(defgeneric rope-weight (rope) |
63 |
| - (:method ((rope leaf)) |
64 |
| - (rope-length rope)) |
65 |
| - (:method ((rope branch)) |
66 |
| - (rope-length (branch-left rope)))) |
67 |
| - |
68 |
| -(defun leaf-short-p (leaf) |
69 |
| - (>= *short-leaf* (rope-length leaf))) |
70 |
| - |
71 |
| -(defun strcat (a b) |
72 |
| - (concatenate 'string a b)) |
| 72 | + (make-leaf source length))))) |
73 | 73 |
|
74 | 74 | ;;-----------;;
|
75 | 75 | ;; Iteration ;;
|
|
175 | 175 | (:method ((rope leaf) index)
|
176 | 176 | (char (leaf-string rope) index))
|
177 | 177 | (:method ((rope branch) index)
|
178 |
| - (let ((weight (rope-weight rope))) |
| 178 | + (let ((weight (branch-weight rope))) |
179 | 179 | (if (< index weight)
|
180 | 180 | (index-rope (branch-left rope) index)
|
181 | 181 | (index-rope (branch-right rope) (- index weight))))))
|
|
212 | 212 | (make-rope (subseq (leaf-string rope) index))))
|
213 | 213 | (:method ((rope branch) index)
|
214 | 214 | (with-slots (left right) rope
|
215 |
| - (let ((weight (rope-weight rope))) |
| 215 | + (let ((weight (branch-weight rope))) |
216 | 216 | (cond ((= index weight)
|
217 | 217 | (values left right))
|
218 | 218 | ((< index weight)
|
|
0 commit comments