|
22 | 22 | {:node root
|
23 | 23 | :position [1 1]
|
24 | 24 | :parent nil
|
25 |
| - :path nil}) |
| 25 | + :left [] |
| 26 | + :right '()}) |
26 | 27 |
|
27 | 28 | (defn node
|
28 | 29 | "Returns the node at loc"
|
|
55 | 56 | (defn lefts
|
56 | 57 | "Returns a seq of the left siblings of this loc"
|
57 | 58 | [loc]
|
58 |
| - (seq (:l (:path loc)))) |
| 59 | + (seq (:left loc))) |
59 | 60 |
|
60 | 61 | (defn down
|
61 | 62 | "Returns the loc of the leftmost child of the node at this loc, or
|
|
68 | 69 | {:node c
|
69 | 70 | :position [row (+ col (node/leader-length node))]
|
70 | 71 | :parent loc
|
71 |
| - :path {:l [] |
72 |
| - :ppath path |
73 |
| - :r cnext}})))) |
| 72 | + :left [] |
| 73 | + :right cnext})))) |
74 | 74 |
|
75 | 75 | (defn up
|
76 | 76 | "Returns the loc of the parent of the node at this loc, or nil if at
|
77 | 77 | the top"
|
78 | 78 | [loc]
|
79 |
| - (let [{:keys [node parent position changed?] {:keys [l ppath r]} :path} loc] |
| 79 | + (let [{:keys [node parent left right changed?]} loc] |
80 | 80 | (when parent
|
81 | 81 | (if changed?
|
82 | 82 | (assoc parent
|
83 | 83 | :changed? true
|
84 |
| - :node (make-node loc (:node parent) (concat l (cons node r))) |
85 |
| - :path ppath) |
| 84 | + :node (make-node loc (:node parent) (concat left (cons node right)))) |
86 | 85 | parent))))
|
87 | 86 |
|
88 | 87 | (defn root
|
|
98 | 97 | (defn right
|
99 | 98 | "Returns the loc of the right sibling of the node at this loc, or nil"
|
100 | 99 | [loc]
|
101 |
| - (let [{:keys [node parent position] {l :l [r & rnext :as rs] :r :as path} :path} loc] |
102 |
| - (when (and path rs) |
| 100 | + (let [{:keys [node parent position left] [r & rnext :as right] :right} loc] |
| 101 | + (when (and parent right) |
103 | 102 | (assoc loc
|
104 | 103 | :node r
|
105 |
| - :path (assoc path :l (conj l node) :r rnext) |
| 104 | + :left (conj left node) |
| 105 | + :right rnext |
106 | 106 | :position (node/+extent position (node/extent node))))))
|
107 | 107 |
|
108 | 108 | (defn rightmost
|
|
115 | 115 | (defn left
|
116 | 116 | "Returns the loc of the left sibling of the node at this loc, or nil"
|
117 | 117 | [loc]
|
118 |
| - (let [{:keys [node parent position] {:keys [l r] :as path} :path} loc] |
119 |
| - (when (and path (seq l)) |
| 118 | + (let [{:keys [node parent left right]} loc] |
| 119 | + (when (and parent (seq left)) |
120 | 120 | (assoc loc
|
121 |
| - :node (peek l) |
122 |
| - :path (assoc path :l (pop l) :r (cons node r)))))) |
| 121 | + :node (peek left) |
| 122 | + :left (pop left) |
| 123 | + :right (cons node right))))) |
123 | 124 |
|
124 | 125 | (defn leftmost
|
125 | 126 | "Returns the loc of the leftmost sibling of the node at this loc, or self"
|
126 | 127 | [loc]
|
127 |
| - (let [{:keys [node parent position] {:keys [l r] :as path} :path} loc] |
128 |
| - (if (and path (seq l)) |
| 128 | + (let [{:keys [node parent left right]} loc] |
| 129 | + (if (and parent (seq left)) |
129 | 130 | (assoc loc
|
130 |
| - :node (first l) |
131 |
| - :path (assoc path :l [] :r (concat (rest l) [node] r))) |
| 131 | + :node (first left) |
| 132 | + :left [] |
| 133 | + :right (concat (rest left) [node] right)) |
132 | 134 | loc)))
|
133 | 135 |
|
134 | 136 | (defn insert-left
|
135 | 137 | "Inserts the item as the left sibling of the node at this loc,
|
136 | 138 | without moving"
|
137 | 139 | [loc item]
|
138 |
| - (let [{:keys [parent position] {:keys [l] :as path} :path} loc] |
139 |
| - (if (nil? path) |
| 140 | + (let [{:keys [parent left]} loc] |
| 141 | + (if-not parent |
140 | 142 | (throw (new Exception "Insert at top"))
|
141 | 143 | (assoc loc
|
142 | 144 | :changed? true
|
143 |
| - :path (assoc path :l (conj l item)))))) |
| 145 | + :left (conj left item))))) |
144 | 146 |
|
145 | 147 | (defn insert-right
|
146 | 148 | "Inserts the item as the right sibling of the node at this loc,
|
147 | 149 | without moving"
|
148 | 150 | [loc item]
|
149 |
| - (let [{:keys [node parent position] {:keys [r] :as path} :path} loc] |
150 |
| - (if (nil? path) |
| 151 | + (let [{:keys [node parent right]} loc] |
| 152 | + (if-not parent |
151 | 153 | (throw (new Exception "Insert at top"))
|
152 | 154 | (assoc loc
|
153 | 155 | :changed? true
|
154 |
| - :path (assoc path :r (cons item r)))))) |
| 156 | + :right (cons item right))))) |
155 | 157 |
|
156 | 158 | (defn replace
|
157 | 159 | "Replaces the node at this loc, without moving"
|
|
210 | 212 | "Removes the node at loc, returning the loc that would have preceded
|
211 | 213 | it in a depth-first walk."
|
212 | 214 | [loc]
|
213 |
| - (let [{:keys [node parent position] {:keys [l ppath r] :as path} :path} loc] |
214 |
| - (if (nil? path) |
| 215 | + (let [{:keys [node parent left right]} loc] |
| 216 | + (if-not parent |
215 | 217 | (throw (new Exception "Remove at top"))
|
216 |
| - (if (pos? (count l)) |
| 218 | + (if (seq left) |
217 | 219 | (loop [loc (assoc loc
|
218 | 220 | :changed? true
|
219 |
| - :node (peek l) |
220 |
| - :path (assoc path :l (pop l)))] |
| 221 | + :node (peek left) |
| 222 | + :left (pop left))] |
221 | 223 | (if-let [child (and (branch? loc) (down loc))]
|
222 | 224 | (recur (rightmost child))
|
223 | 225 | loc))
|
224 | 226 | (assoc parent
|
225 | 227 | :changed? true
|
226 |
| - :node (make-node loc (:node parent) r) |
227 |
| - :path ppath |
228 |
| - :parent (:parent parent)))))) |
| 228 | + :node (make-node loc (:node parent) right)))))) |
0 commit comments