-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree-common.lisp
410 lines (309 loc) · 13.4 KB
/
tree-common.lisp
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
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :tree)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tree Common Interface Layer, depends on only the public node-instance-access
;; api: node/k, node/v, node/l, node/r, node/x, node/kv, node/lr, node/kvlr,
;; node/kvlrx, node/constituents, node/values, kv, lr, kvlr, kvlrx.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deflayer balanced)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Degenerate Instance API
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun empty ()
"a value which satisfies {defun tree::empty?}"
(leaf))
(defun node/empty? (node)
;; this probably doesnt need to exist
(null node))
(define-layered-function empty? (thing)
(:documentation "returns t if THING contains no associations")
(:method ((thing null)) t)
(:method ((thing t)) nil))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Node Primatives
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun node/call (node fn)
"apply FN to the destructured constituent values of
NODE. FN is a function taking four parameters: K, V, L, and R,
where K is the key of NODE, V is the value of NODE, L is the left
subtree of NODE, and R is the right subtree of NODE."
(apply fn (node/kvlr node)))
(define-layered-function node/size (node)
(:documentation "returns the number of associations in tree rooted at node"))
(define-layered-method node/size :in-layer t ((node null))
0)
(define-layered-method node/size :in-layer t (node)
(+ 1 (node/size (node/l node)) (node/size (node/r node))))
(defun node/weight (node)
"returns node weight as appropriate for rotation
calculations using the 'revised non-variant algorithm' for weight
balanced binary tree. For non-weight-balanced trees, this is not a
useful statistic"
(+ (node/size node) 1))
(define-layered-function node/height (node)
(:documentation "The distance from NODE to its furthest leaf subnode"))
(define-layered-method node/height :in-layer t ((node null))
0)
(define-layered-method node/height :in-layer t ((node t))
(+ 1 (max (node/height (node/l node)) (node/height (node/r node)))))
(defun node/cons-enum (node &optional enum)
"efficient mechanism to accomplish partial enumeration of
tree-structure into a consp representation without incurring the
overhead of operating over the entire tree. Used internally for
implementation of higher-level collection api routines"
(cond
((empty? node) enum)
(t
(kvlr (k v l r) node
(node/cons-enum l (list (cons k v) r enum))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Construction and Rotation Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-layered-function node/create (k v l r)
(:documentation "Join left and right subtrees at root k/v. Assumes
all keys in l < k < all keys in r."))
(defun node/singleton (k &optional (v (unbound)))
"create and return a newly allocated weight balanced
tree containing a single association, that value V with key K."
(node/create k v (leaf) (leaf)))
(define-layered-function node/join (k v l r)
(:documentation "Join left and right subtrees at root k/v,
performing a rotation operation to balance the resulting tree,
if needed. Assumes all keys in l < k < all keys in r, and the
relative balance of the left and right subtrees is such that no
more than one rotation operation will be required to restore balance
for the two provided subtrees, l and r"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Ordering Relation Independent Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun node/least (node)
"Return the node containing the minimum key of the tree rooted at NODE"
(cond
((empty? node) (error "least: empty tree"))
((empty? (node/l node)) (return-from node/least node))
(t
(node/least (node/l node)))))
(defun node/greatest (node)
"Return the node containing the minimum key of the tree rooted at NODE"
(cond
((empty? node) (error "greatest: empty tree"))
((empty? (node/r node)) (return-from node/greatest node))
(t
(node/greatest (node/r node)))))
(defun node/remove-least (node)
"Return a tree the same as the one rooted at NODE,
with the node containing the minimum key removed. See {defun
tree::node/least}"
(cond
((empty? node) (error "remove-least: empty tree"))
((empty? (node/l node)) (node/r node))
(t
(node/join (node/k node) (node/v node)
(node/remove-least (node/l node)) (node/r node)))))
(defun node/remove-greatest (node)
"Return a tree the same as the one rooted at NODE,
with the node containing the maximum key removed. See {defun
tree::node/greatest}"
(cond
((empty? node) (error "remove-greatest: empty tree"))
((empty? (node/r node)) (node/l node))
(t
(node/join (node/k node) (node/v node)
(node/l node) (node/remove-greatest (node/r node))))))
(defun node/concat2 (node1 node2)
"Join two trees, the left rooted at NODE1, and the right at NODE2,
performing a single balancing operation on the resulting tree, if
needed. Assumes all keys in NODE1 are smaller than all keys in
NODE2, and the relative balance of NODE1 and NODE2 is such that no
more than one rotation operation will be required to balance the
resulting tree"
(cond
((empty? node1) node2)
((empty? node2) node1)
(t
(kv (k v) (node/least node2)
(node/join k v node1 (node/remove-least node2))))))
(defun node/inorder-fold (fn base node)
(labels ((the-fn (k &optional v a)
(funcall (alexandria:ensure-function fn) k v a))
(fold (base node)
(if (empty? node) base
(kvlr (k v l r) node
(fold (the-fn k v (fold base r)) l)))))
(fold base node)))
(defun node/reverse-fold (fn base node)
(labels ((the-fn (k &optional v a)
(funcall (alexandria:ensure-function fn) k v a))
(fold (base node)
(if (empty? node) base
(kvlr (k v l r) node
(fold (the-fn k v (fold base l)) r)))))
(fold base node)))
(defun node/iter (node fn)
"For the side-effect, apply FN to each node of the tree rooted at NODE"
(if (empty? node) nil
(kvlr (k v l r) node
(node/iter l fn)
(funcall (alexandria:ensure-function fn) k v)
(node/iter r fn))))
(defun node/for-all (node predicate fn)
"For the side-effect, apply FN to each node of the tree rooted at
NODE for which the predicate function returns a non-nil value"
(if (empty? node) nil
(kvlr (k v l r) node
(node/for-all l predicate fn)
(when (funcall predicate k)
(funcall (alexandria:ensure-function fn) k v))
(node/for-all r predicate fn))))
(define-layered-function node/at-index (node index &optional caller)
(:documentation ""))
(define-layered-function node/rank (k node &optional rank)
(:documentation ""))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Ordering Relation Dependent Operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun node/find (k node)
"find k (if exists) in only d comparisons (d is depth of tree)
rather than the traditional compare/low compare/high which takes on
avg (* 1.5 (- d 1))"
(labels ((recur (this best)
(cond
((empty? this) (return-from recur best))
((ORD:|COMPARE<| k (node/k this)) (recur (node/l this) best))
(t (recur (node/r this) this)))))
(let ((best (recur node nil)))
(when best
(unless (ORD:|COMPARE<| (node/k best) k)
(return-from node/find best))))))
(defun node/add (node k &optional (v (unbound)))
(if (empty? node)
(node/singleton k v)
(node/call node
(lambda (key val l r)
(cond
((ORD:|COMPARE<| k key) (node/join key val (node/add l k v) r))
((ORD:|COMPARE<| key k) (node/join key val l (node/add r k v)))
(t
(node/create key v l r)))))))
(defun node/remove (node k)
(if (empty? node)
(leaf)
(node/call node
(lambda (key val l r)
(cond
((ORD:|COMPARE<| k key) (node/join key val (node/remove l k) r))
((ORD:|COMPARE<| key k) (node/join key val l (node/remove k r)))
(t
(node/concat2 l r)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Full Concatentate Operations (Regardless of Weight)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-layered-function node/concat3 (k v l r)
(:documentation ""))
(define-layered-function node/concat (node1 node2)
(:documentation ""))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set Operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun node/split-lesser (node k)
(cond
((empty? node) (empty))
((ORD:|COMPARE<| k (node/k node)) (node/split-lesser (node/l node) k))
((ORD:|COMPARE>| k (node/k node)) (node/concat3
(node/k node)
(node/v node)
(node/l node)
(node/split-lesser (node/r node) k)))
(t (node/l node))))
(defun node/split-greater (node k)
(cond
((empty? node) (empty))
((ORD:|COMPARE<| (node/k node) k) (node/split-greater (node/r node) k))
((ORD:|COMPARE>| (node/k node) k) (node/concat3
(node/k node)
(node/v node)
(node/split-greater (node/l node) k)
(node/r node)))
(t (node/r node))))
(defun node/split (node k)
"returns a triple (l present r) where: l is the set of elements of
s that are < k, r is the set of elements of s that are > k, present
is false if s contains no element equal to k, or
(k . v) if s contains an element with key equal to k"
(cond
((empty? node) (list nil nil nil))
(t (kvlr (ak v l r) node
(let ((c (ord:compare k ak)))
(cond
((zerop c) (return-from node/split
(list l (cons k v) r)))
((minusp c) (destructuring-bind (ll pres rl) (node/split l k)
(list ll pres (node/concat3 ak v rl r))))
((plusp c) (destructuring-bind (lr pres rr) (node/split r k)
(list (node/concat3 ak v l lr) pres rr)))))))))
(defun node/union (node1 node2)
(cond
((empty? node1) node2)
((empty? node2) node1)
(t
(kvlr (ak av l r) node2
(let ((l1 (node/split-lesser node1 ak))
(r1 (node/split-greater node1 ak)))
(node/concat3 ak av
(node/union l1 l)
(node/union r1 r)))))))
(deftype merge-direction ()
""
`(member :left :right))
(defun node/union-merge (node1 node2 &optional merge)
(let ((do-merge-values
(case merge
(:left #'first)
(:right #'second)
(t (error "invalid merge type ~S, must be :left or :right" merge)))))
(cond
((empty? node1) node2)
((empty? node2) node1)
(t
(kvlr (ak av l r) node2
(let* ((node1 (node/find ak node1))
(l1 (node/split-lesser node1 ak))
(r1 (node/split-greater node1 ak))
(val (if node1
(funcall do-merge-values (list (node/v node1) av))
av)))
(node/concat3 ak val
(node/union-merge l1 l merge)
(node/union-merge r1 r merge))))))))
(defun node/intersection (node1 node2)
(cond
((empty? node1) (empty))
((empty? node2) (empty))
(t
(kvlr (ak av l r) node2
(let ((l1 (node/split-lesser node1 ak))
(r1 (node/split-greater node1 ak)))
(if (node/find ak node1)
(node/concat3 ak av
(node/intersection l1 l)
(node/intersection r1 r))
(node/concat
(node/intersection l1 l)
(node/intersection r1 r))))))))
(defun node/difference (node1 node2)
(cond
((empty? node1) (empty))
((empty? node2) node1)
(t
(kvlr (ak av l r) node2
(declare (ignore av))
(let ((l1 (node/split-lesser node1 ak))
(r1 (node/split-greater node1 ak)))
(node/concat
(node/difference l1 l)
(node/difference r1 r)))))))
(define-layered-function node/subset? (node1 node2))
(define-layered-function node/from (thing) )
(define-layered-function node/member? (node key) )
(define-layered-function node/contains? (key node) )