|
56 | 56 | (defn lefts
|
57 | 57 | "Returns a seq of the left siblings of this loc"
|
58 | 58 | [loc]
|
59 |
| - (seq (:left loc))) |
| 59 | + (map first (:left loc))) |
60 | 60 |
|
61 | 61 | (defn down
|
62 | 62 | "Returns the loc of the leftmost child of the node at this loc, or
|
|
81 | 81 | (if changed?
|
82 | 82 | (assoc parent
|
83 | 83 | :changed? true
|
84 |
| - :node (make-node loc (:node parent) (concat left (cons node right)))) |
| 84 | + :node (make-node loc |
| 85 | + (:node parent) |
| 86 | + (concat (map first left) (cons node right)))) |
85 | 87 | parent))))
|
86 | 88 |
|
87 | 89 | (defn root
|
|
101 | 103 | (when (and parent right)
|
102 | 104 | (assoc loc
|
103 | 105 | :node r
|
104 |
| - :left (conj left node) |
| 106 | + :left (conj left [node position]) |
105 | 107 | :right rnext
|
106 | 108 | :position (node/+extent position (node/extent node))))))
|
107 | 109 |
|
|
117 | 119 | [loc]
|
118 | 120 | (let [{:keys [node parent left right]} loc]
|
119 | 121 | (when (and parent (seq left))
|
120 |
| - (assoc loc |
121 |
| - :node (peek left) |
122 |
| - :left (pop left) |
123 |
| - :right (cons node right))))) |
| 122 | + (let [[lnode lpos] (peek left)] |
| 123 | + (assoc loc |
| 124 | + :node lnode |
| 125 | + :position lpos |
| 126 | + :left (pop left) |
| 127 | + :right (cons node right)))))) |
124 | 128 |
|
125 | 129 | (defn leftmost
|
126 | 130 | "Returns the loc of the leftmost sibling of the node at this loc, or self"
|
127 | 131 | [loc]
|
128 | 132 | (let [{:keys [node parent left right]} loc]
|
129 | 133 | (if (and parent (seq left))
|
130 | 134 | (assoc loc
|
131 |
| - :node (first left) |
| 135 | + :node (ffirst left) |
132 | 136 | :left []
|
133 |
| - :right (concat (rest left) [node] right)) |
| 137 | + :right (concat (map first (rest left)) [node] right)) |
134 | 138 | loc)))
|
135 | 139 |
|
136 | 140 | (defn insert-left
|
137 | 141 | "Inserts the item as the left sibling of the node at this loc,
|
138 | 142 | without moving"
|
139 | 143 | [loc item]
|
140 |
| - (let [{:keys [parent left]} loc] |
141 |
| - (if-not parent |
142 |
| - (throw (new Exception "Insert at top")) |
143 |
| - (assoc loc |
144 |
| - :changed? true |
145 |
| - :left (conj left item))))) |
| 144 | + (let [{:keys [parent position left]} loc] |
| 145 | + (if-not parent |
| 146 | + (throw (new Exception "Insert at top")) |
| 147 | + (assoc loc |
| 148 | + :changed? true |
| 149 | + :left (conj left [item position]))))) |
146 | 150 |
|
147 | 151 | (defn insert-right
|
148 | 152 | "Inserts the item as the right sibling of the node at this loc,
|
|
216 | 220 | (if-not parent
|
217 | 221 | (throw (new Exception "Remove at top"))
|
218 | 222 | (if (seq left)
|
219 |
| - (loop [loc (assoc loc |
220 |
| - :changed? true |
221 |
| - :node (peek left) |
222 |
| - :left (pop left))] |
| 223 | + (loop [loc (let [[lnode lpos] (peek left)] |
| 224 | + (assoc loc |
| 225 | + :changed? true |
| 226 | + :position lpos |
| 227 | + :node lnode |
| 228 | + :left (pop left)))] |
223 | 229 | (if-let [child (and (branch? loc) (down loc))]
|
224 | 230 | (recur (rightmost child))
|
225 | 231 | loc))
|
|
0 commit comments