-
Notifications
You must be signed in to change notification settings - Fork 0
/
rktree.cljc
506 lines (336 loc) · 10.5 KB
/
rktree.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
(ns dvlopt.rktree
"README describes what ranked trees are.
This namespace provides recognizable functions for manipulating them."
{:author "Adam Helinski"}
(:require [clojure.core :as clj]
[dvlopt.void :as void])
;;
;; <!> Attention <!>
;;
;; Code is confusing if one does not remember this.
;;
(:refer-clojure :exclude [#?@(:cljs [-assoc
-pop])
assoc
dissoc
get
pop
update]))
;;;;;;;;;; Gathering all declarations
(declare update)
;;;;;;;;;; Creating new trees
(defn tree
"Simply provides a sorted map, reminding that a ranked tree always start with at least one sorted map.
Should be used just in case the implementation changes and uses something else than Clojure's sorted maps."
[]
(sorted-map))
;;;;;;;;;; Operations with ranks
(defn r+
"Sums the given rank collections which might be of different length.
```clojure
(r+ [0 5]
[10 10 10])
[10 15 10]
```"
;; TODO. Optimize for endings in 0's ?
[ranks-1 ranks-2]
(let [n-1 (count ranks-1)
n-2 (count ranks-2)
[base
[front
rear]] (if (<= n-1
n-2)
[ranks-1
(split-at n-1
ranks-2)]
[ranks-2
(split-at n-2
ranks-1)])]
(into (mapv +
base
front)
rear)))
;;;;;;;;;; Manipulating trees
(defn- -enforce-ranks
;; At least one rank must be specified, there is no concept of ranked trees without
;; sorted maps surrounding the root.
[ranks]
(if (seq ranks)
ranks
(throw (ex-info "Ranks cannot be empty"
{::ranks ranks}))))
(defn- -assoc-in
;; Returns `v` when there is no path.
([path v]
(-assoc-in {}
path
v))
([hmap path v]
(if (empty? path)
v
(assoc-in hmap
path
v))))
(defn- -assoc-leaf
;; Creates required sorted maps following ranks then associates the leaf.
[[r & rs] path v]
(sorted-map r (if rs
(-assoc-leaf rs
path
v)
(-assoc-in path
v))))
(defn- -bubbling-assoc-leaf
;; Like [[-assoc-leaf]], but incorporates a "bubbling" node which needs to be re-prioritized.
[[r & rs] path v bubbling-node]
(if (zero? r)
(if rs
(sorted-map 0 (-bubbling-assoc-leaf rs
path
v
bubbling-node))
(-assoc-in bubbling-node
path
v))
(if rs
(sorted-map 0 bubbling-node
r (-assoc-leaf rs
path
v))
(sorted-map 0 bubbling-node
r (-assoc-in path
v)))))
(defn- -assoc
;; Cf. [[assoc]]
[tree [r & rs] path v]
(clj/update tree
r
(fn at-rank [node]
(cond
(nil? node) (if rs
(-assoc-leaf rs
path
v)
(-assoc-in path
v))
(sorted? node) (if rs
(-assoc node
rs
path
v)
(-assoc node
[0]
path
v))
:else (if rs
(if (some? node)
(-bubbling-assoc-leaf rs
path
v
node)
(-assoc-leaf rs
path
v))
(-assoc-in node
path
v))))))
(defn assoc
"Associates `v` at `path` which is then prioritized using `ranks`.
When `path` is not provided, associates directly after the ranks.
Cf. Namespace description for an example."
([tree ranks v]
(assoc tree
ranks
nil
v))
([tree ranks path v]
(-assoc tree
(-enforce-ranks ranks)
path
v)))
(defn dissoc
"Dissociates a value located at `path` and prioritized by `ranks`.
When `path` is not provided, dissociates everything for the given `ranks`."
([tree ranks]
(dissoc tree
ranks
nil))
([tree ranks path]
(update tree
ranks
nil
(fn dissoc-in [node]
(when (seq path)
(not-empty (void/dissoc-in node
path)))))))
(defn- -get
;; Cf. [[get]]
[tree [r & rs] path not-found]
(let [node (clj/get tree
r)]
(if (sorted? node)
(if rs
(recur node
rs
path
not-found)
(recur node
[0]
path
not-found))
(if rs
not-found
(get-in node
path
not-found)))))
(defn get
"Find a value located at `path` that has been prioritized with `ranks`.
When path is not provided, returns everything at the given `ranks`."
([tree ranks]
(get tree
ranks
nil))
([tree ranks path]
(get tree
ranks
path
nil))
([tree ranks path not-found]
(-get tree
(-enforce-ranks ranks)
path
not-found)))
(defn- -pop
;; Cf. [[pop]]
[node ranks]
(if (and (map? node)
(sorted? node))
(if-some [[k
node-next] (first node)]
(clj/update (-pop node-next
(conj ranks
k))
0
(fn rebuild-tree [subtree]
(if subtree
(clj/assoc node
k
subtree)
(not-empty (clj/dissoc node
k)))))
[nil
ranks
nil])
[nil
ranks
node]))
(defn pop
"Respecting the ordering of ranks, returns a vector such as:
| i | meaning |
|---|---|
| 0 | tree after popping |
| 1 | ranks that led to node |
| 2 | popped unsorted node at ranks |
There might be no node to pop, and after popping a empty tree is returned as nil."
[tree]
(-pop tree
[]))
(defn- -walk-unsorted
;; Cf. [[-pop-walk]]
[acc ranks path node f]
(if (map? node)
(reduce-kv (fn deeper [acc-2 k node-next]
(-walk-unsorted acc-2
ranks
(conj path
k)
node-next
f))
acc
node)
(f acc
ranks
path
node)))
(defn pop-walk
"Pops the given ranked `tree` and then walks the unsorted node in a depth-first manner by applying:
```clojure
(f acc
ranks
path
leaf)
```
After popping but before walking the unsorted node, the popped tree is transformed into `acc`
by calling `popped-tree->acc` (defaulting to Clojure's `identity`).
See also [[pop]]."
([tree f]
(pop-walk tree
identity
f))
([tree popped-tree->acc f]
(if (sorted? tree)
(let [[tree-2
ranks
node] (pop tree)]
(if (nil? node)
tree-2
(-walk-unsorted (popped-tree->acc tree-2)
ranks
[]
node
f)))
(-walk-unsorted (popped-tree->acc nil)
[]
[]
tree
f))))
(defn- -update
;; Cf. [[update]]
[tree [r & rs :as ranks] path f]
(void/update tree
r
(fn at-rank [node]
(if node
(if rs
(if (sorted? node)
(not-empty (-update node
rs
path
f))
(if-some [leaf (f nil)]
(-bubbling-assoc-leaf rs
path
leaf
node)
node))
(if (sorted? node)
(not-empty (-update node
[0]
path
f))
(if (seq path)
(not-empty (void/update-in node
path
f))
(f node))))
(when-some [v (f nil)]
(if rs
(-assoc-leaf rs
path
v)
(-assoc-in path
v)))))))
(defn update
"Update the value located at `path` and prioritized by `ranks`
When `path` is not provided, updates whatever there is at the given `ranks`.
If the result of applying `f` is nil, the value is removed."
([tree ranks f]
(update tree
ranks
nil
f))
([tree ranks path f]
(-update tree
(-enforce-ranks ranks)
path
f)))