Debian Codegraph @

%3a224/trie.scm

raw symbols SCIP
1 ;;; SPDX-FileCopyrightText: 2021 Wolfgang Corcoran-Mathe <[email protected]>
2 ;;;
3 ;;; SPDX-License-Identifier: MIT
4
5 ;;; This file implements integer maps as big-endian binary radix
6 ;;; trees (AKA Patricia tries), as described by Chris Okasaki and
7 ;;; Andrew Gill in "Fast Mergeable Integer Maps" (1998). Integers
8 ;;; in big-endian binary encoding are stored in a trie structure
9 ;;; which allows fast lookup, insertion, and set-theoretical
10 ;;; operations (union, intersection, etc.)
11 ;;;
12 ;;; A trie is represented by #f (the empty trie), a leaf, or a branch.
13 ;;;
14 ;;; Throughout this code, the empty trie (#f) is always returned
15 ;;; as an explicit value, not, e.g. as the default value of an
16 ;;; (and ...) expression, to clarify its use as a trie value.
17
18 ;;;; Utility
19
20 (define (swap-last-args proc)
21 (lambda (k x y) (proc k y x)))
22
23 (define the-empty-trie #f)
24
25 (define (trie-empty? t) (not t))
26
27 (define-record-type <leaf>
28 (leaf key value)
29 leaf?
30 (key leaf-key)
31 (value leaf-value))
32
33 (define-record-type <branch>
34 (raw-branch prefix branching-bit left right)
35 branch?
36 (prefix branch-prefix)
37 (branching-bit branch-branching-bit)
38 (left branch-left)
39 (right branch-right))
40
41 (define (valid-integer? x) (fixnum? x))
42
43 ;; Zero the bits of k at and below (BE) the set bit of m.
44 (define (mask k m)
45 (if (fx=? m fx-least)
46 0
47 (fxand k (fxxor (fxnot (fx- m 1)) m))))
48
49 ;; Does the m-masked prefix of k match p?
50 (define (match-prefix? k p m)
51 (fx=? (mask k m) p))
52
53 (define (branching-bit p1 m1 p2 m2)
54 (if (fxnegative? (fxxor p1 p2))
55 fx-least ; different signs
56 (highest-bit-mask (fxxor p1 p2) (fxmax 1 (fx* 2 (fxmax m1 m2))))))
57
58 ;; Two's-complement trick.
59 (define (lowest-set-bit b)
60 (fxand b (fxneg b)))
61
62 (define (highest-bit-mask k guess-m)
63 (let lp ((x (fxand k (fxnot (fx- guess-m 1)))))
64 (let ((m (lowest-set-bit x)))
65 (if (fx=? x m)
66 m
67 (lp (fx- x m))))))
68
69 (define (zero-bit? k m)
70 (fxzero? (fxand k m)))
71
72 ;; Insert the association (key, value) into trie, replacing any old
73 ;; association.
74 (define (trie-insert trie key value)
75 (trie-insert/combine trie key value (lambda (_k new _) new)))
76
77 ;; Insert the association (key, value) into trie, preserving any old
78 ;; association.
79 (define (trie-adjoin trie key value)
80 (trie-insert/combine trie key value (lambda (_k _new old) old)))
81
82 ;; Insert (key, value) into trie if key doesn't already have an
83 ;; association. If it does, add a new association for key and
84 ;; the result of calling combine on the key, new, and old values.
85 (define (trie-insert/combine trie key value combine)
86 (letrec
87 ((new-leaf (leaf key value))
88 (insert
89 (lambda (t)
90 (tmatch t
91 (empty (leaf key value))
92 ((leaf ,k ,v)
93 (if (fx=? key k)
94 (leaf k (combine k value v))
95 (trie-join key 0 new-leaf k 0 t)))
96 ((branch ,p ,m ,l ,r)
97 (if (match-prefix? key p m)
98 (if (zero-bit? key m)
99 (branch p m (insert l) r)
100 (branch p m l (insert r)))
101 (trie-join key 0 new-leaf p m t)))))))
102 (assume (valid-integer? key) "invalid key")
103 (insert trie)))
104
105 (define (trie-join prefix1 mask1 trie1 prefix2 mask2 trie2)
106 (let ((m (branching-bit prefix1 mask1 prefix2 mask2)))
107 (if (zero-bit? prefix1 m)
108 (branch (mask prefix1 m) m trie1 trie2)
109 (branch (mask prefix1 m) m trie2 trie1))))
110
111 ;; If (key, value) is an association in trie, then replace it
112 ;; with (key, (proc key value)). Otherwise, return a copy of trie.
113 (define (trie-adjust trie key proc)
114 (letrec
115 ((update
116 (lambda (t)
117 (tmatch t
118 (empty t)
119 ((leaf ,k ,v)
120 (if (fx=? key k) (leaf k (proc k v)) t))
121 ((branch ,p ,m ,l ,r)
122 (if (match-prefix? key p m)
123 (if (zero-bit? key m)
124 (branch p m (update l) r)
125 (branch p m l (update r)))
126 t))))))
127 (update trie)))
128
129 (define (trie-update trie key proc failure wrapper)
130 (letrec
131 ((update
132 (lambda (t build)
133 (tmatch t
134 (empty (failure))
135 ((leaf ,k ,v)
136 (if (fx=? key k)
137 (proc k
138 v
139 (lambda (v*) (wrapper (build (leaf k v*))))
140 (lambda () (wrapper (build the-empty-trie))))
141 (failure)))
142 ((branch ,p ,m ,l ,r)
143 (if (match-prefix? key p m)
144 (if (zero-bit? key m)
145 (update l (lambda (l*) (build (branch p m l* r))))
146 (update r (lambda (r*) (build (branch p m l r*)))))
147 (failure)))))))
148 (update trie values)))
149
150 (define (trie-alter trie key failure success wrapper)
151 (letrec
152 ((update
153 (lambda (t build)
154 (tmatch t
155 (empty
156 (failure (lambda (v)
157 (wrapper (build (leaf key v)))) ; insert
158 (lambda ()
159 (wrapper (build the-empty-trie))))) ; ignore
160 ((leaf ,k ,v)
161 (if (fx=? key k)
162 (success k
163 v
164 (lambda (v*) ; replace
165 (wrapper (build (leaf k v*))))
166 (lambda () ; delete
167 (wrapper (build the-empty-trie))))
168 (failure (lambda (u) ; insert
169 (wrapper
170 (build (trie-join key 0 (leaf key u) k 0 t))))
171 (lambda () ; ignore
172 (wrapper (build t))))))
173 ((branch ,p ,m ,l ,r)
174 (if (match-prefix? key p m)
175 (if (zero-bit? key m)
176 (update l (lambda (l*)
177 (build (branch p m l* r))))
178 (update r (lambda (r*)
179 (build (branch p m l r*)))))
180 (failure (lambda (v) ; insert
181 (wrapper
182 (build (trie-join key 0 (leaf key v) p m t))))
183 (lambda () ; ignore
184 (wrapper (build t))))))))))
185 (update trie values)))
186
187 ;; If `key' has an association in `trie', then call `success' with
188 ;; on the associated value. Otherwise, call `failure'.
189 (define (trie-assoc trie key failure success)
190 (letrec
191 ((search
192 (tmatch-lambda
193 ((leaf ,k ,v) (guard (fx=? k key)) (success v))
194 ((branch ,p ,m ,l ,r) (guard (match-prefix? key p m))
195 (if (zero-bit? key m) (search l) (search r)))
196 (else (failure)))))
197 (search trie)))
198
199 ;; If `key' has an association in `trie', then return the associated
200 ;; value. Otherwise, return `default'.
201 (define (trie-assoc/default trie key default)
202 (letrec
203 ((search
204 (tmatch-lambda
205 ((leaf ,k ,v) (guard (fx=? k key)) v)
206 ((branch ,p ,m ,l ,r) (guard (match-prefix? key p m))
207 (if (zero-bit? key m) (search l) (search r)))
208 (else default))))
209 (search trie)))
210
211 ;; Return the number of associations in trie.
212 (define (trie-size trie)
213 (if (trie-empty? trie)
214 0
215 (let lp ((n 0) (t trie) (kont values))
216 (cond ((leaf? t) (kont (+ n 1)))
217 (else (lp n
218 (branch-left t)
219 (lambda (m)
220 (lp m (branch-right t) kont))))))))
221
222 (define (trie-contains? trie key)
223 (letrec
224 ((search
225 (tmatch-lambda
226 ((leaf ,k ,v) (fx=? k key))
227 ((branch ,p ,m ,l ,r) (guard (match-prefix? key p m))
228 (if (zero-bit? key m) (search l) (search r)))
229 (else #f))))
230 (search trie)))
231
232 (define (trie-find pred trie failure success)
233 (letrec
234 ((search
235 (lambda (t kont)
236 (tmatch t
237 ((leaf ,k ,v) (guard (pred k v)) (success k v))
238 ((branch ? ? ,l ,r) (search l (lambda () (search r kont))))
239 (else (kont))))))
240 (tmatch trie
241 ((branch ? ,m ,l ,r) (guard (negative? m))
242 (search r (lambda () (search l failure))))
243 (else (search trie failure)))))
244
245 (define (branching-bit-higher? mask1 mask2)
246 (if (negative? (fxxor mask1 mask2)) ; signs differ
247 (negative? mask1)
248 (fx>? mask1 mask2)))
249
250 ;; Merge two tries. `combine' is used to merge duplicated mappings.
251 (define (trie-merge combine trie1 trie2)
252 (letrec
253 ((merge
254 (lambda (s t)
255 (cond ((trie-empty? s) t)
256 ((trie-empty? t) s)
257 ((leaf? s)
258 (trie-insert/combine t (leaf-key s) (leaf-value s) combine))
259 ((leaf? t)
260 (trie-insert/combine s
261 (leaf-key t)
262 (leaf-value t)
263 (swap-last-args combine)))
264 ((and (branch? s) (branch? t)) (merge-branches s t)))))
265 (merge-branches
266 (lambda (s t)
267 (tmatch s
268 ((branch ,p ,m ,s1 ,s2)
269 (tmatch t
270 ((branch ,q ,n ,t1 ,t2)
271 (cond ((and (fx=? m n) (fx=? p q))
272 (branch p m (merge s1 t1) (merge s2 t2)))
273 ((and (branching-bit-higher? m n)
274 (match-prefix? q p m))
275 (if (zero-bit? q m)
276 (branch p m (merge s1 t) s2)
277 (branch p m s1 (merge s2 t))))
278 ((and (branching-bit-higher? n m)
279 (match-prefix? p q n))
280 (if (zero-bit? p n)
281 (branch q n (merge s t1) t2)
282 (branch q n t1 (merge s t2))))
283 (else (trie-join p m s q n t))))))))))
284 (merge trie1 trie2)))
285
286 ;; Construct a branch only if the subtrees are non-empty.
287 (define (branch prefix mask trie1 trie2)
288 (cond ((not trie1) trie2)
289 ((not trie2) trie1)
290 (else (raw-branch prefix mask trie1 trie2))))
291
292 (define (trie-union s t)
293 (trie-merge trie-insert s t))
294
295 (define (trie-partition pred trie)
296 (letrec
297 ((part
298 (lambda (t)
299 (tmatch t
300 (empty (values the-empty-trie the-empty-trie))
301 ((leaf ,k ,v)
302 (if (pred k v)
303 (values t the-empty-trie)
304 (values the-empty-trie t)))
305 ((branch ,p ,m ,l ,r)
306 (let-values (((il ol) (part l))
307 ((ir or) (part r)))
308 (values (branch p m il ir) (branch p m ol or))))))))
309 (part trie)))
310
311 ;;;; Map and fold
312
313 (define (trie-map proc trie)
314 (letrec
315 ((tmap
316 (tmatch-lambda
317 (empty the-empty-trie)
318 ((leaf ,k ,v)
319 (leaf k (proc k v)))
320 ((branch ,p ,m ,l ,r)
321 (branch p m (tmap l) (tmap r))))))
322 (tmap trie)))
323
324 (define (trie-fold-left proc nil trie)
325 (if (trie-empty? trie)
326 nil
327 (let lp ((t trie) (b nil) (kont values))
328 (if (leaf? t)
329 (kont (proc (leaf-key t) (leaf-value t) b))
330 (lp (branch-left t)
331 b
332 (lambda (c)
333 (lp (branch-right t) c kont)))))))
334
335 (define (trie-fold-right proc nil trie)
336 (if (trie-empty? trie)
337 nil
338 (let lp ((t trie) (b nil) (kont values))
339 (if (leaf? t)
340 (kont (proc (leaf-key t) (leaf-value t) b))
341 (lp (branch-right t)
342 b
343 (lambda (c)
344 (lp (branch-left t) c kont)))))))
345
346 (define (trie-filter pred trie)
347 (letrec ((filter
348 (lambda (t)
349 (tmatch t
350 (empty the-empty-trie)
351 ((leaf ,k ,v) (guard (pred k v)) t)
352 ((leaf ? ?) the-empty-trie)
353 ((branch ,p ,m ,l ,r)
354 (branch p m (filter l) (filter r)))))))
355 (filter trie)))
356
357 (define (trie-min trie)
358 (letrec
359 ((search
360 (tmatch-lambda
361 ((leaf ,k ,v) (values k v))
362 ((branch ? ? ,l ?) (search l)))))
363 (tmatch trie
364 (empty (error 'trie-min "empty trie"))
365 ((leaf ,k ,v) (values k v))
366 ((branch ? ,m ,l ,r)
367 (if (fxnegative? m) (search r) (search l))))))
368
369 (define (trie-update-min trie success wrapper)
370 (letrec
371 ((update
372 (lambda (t build)
373 (tmatch t
374 (empty (wrapper (build the-empty-trie)))
375 ((leaf ,k ,v)
376 (success k
377 v
378 (lambda (v*)
379 (wrapper (build (leaf k v*))))
380 (lambda ()
381 (wrapper (build the-empty-trie)))))
382 ((branch ,p ,m ,l ,r)
383 (update l (lambda (l*)
384 (build (branch p m l* r)))))))))
385 (tmatch trie
386 ((branch ,p ,m ,l ,r)
387 (if (negative? m)
388 (update r (lambda (r*) (branch p m l r*)))
389 (update l (lambda (l*) (branch p m l* r)))))
390 (else (update trie values)))))
391
392 (define (trie-pop-min trie)
393 (letrec
394 ((pop
395 (tmatch-lambda
396 (empty (error 'trie-pop-min "empty trie"))
397 ((leaf ,k ,v) (values k v the-empty-trie))
398 ((branch ,p ,m ,l ,r)
399 (let-values (((k v l*) (pop l)))
400 (values k v (branch p m l* r)))))))
401 (tmatch trie
402 ((branch ,p ,m ,l ,r)
403 (if (fxnegative? m)
404 (let-values (((k v r*) (pop r)))
405 (values k v (branch p m l r*)))
406 (let-values (((k v l*) (pop l)))
407 (values k v (branch p m l* r)))))
408 (else (pop trie)))))
409
410 (define (trie-max trie)
411 (letrec
412 ((search
413 (tmatch-lambda
414 ((leaf ,k ,v) (values k v))
415 ((branch ? ? ? ,r) (search r)))))
416 (tmatch trie
417 (empty (error 'trie-max "empty trie"))
418 ((branch ? ,m ,l ,r)
419 (if (fxnegative? m) (search l) (search r)))
420 ((leaf ,k ,v) (values k v)))))
421
422 (define (trie-update-max trie success wrapper)
423 (letrec
424 ((update
425 (lambda (t build)
426 (tmatch t
427 (empty (wrapper (build the-empty-trie)))
428 ((leaf ,k ,v)
429 (success k
430 v
431 (lambda (v*)
432 (wrapper (build (leaf k v*))))
433 (lambda ()
434 (wrapper (build the-empty-trie)))))
435 ((branch ,p ,m ,l ,r)
436 (update r (lambda (r*)
437 (build (branch p m l r*)))))))))
438 (tmatch trie
439 ((branch ,p ,m ,l ,r)
440 (if (negative? m)
441 (update l (lambda (l*) (branch p m l* r)))
442 (update r (lambda (r*) (branch p m l r*)))))
443 (else (update trie values)))))
444
445 (define (trie-pop-max trie)
446 (letrec
447 ((pop
448 (tmatch-lambda
449 (empty (error 'trie-pop-max "empty trie"))
450 ((leaf ,k ,v) (values k v the-empty-trie))
451 ((branch ,p ,m ,l ,r)
452 (let-values (((k v r*) (pop r)))
453 (values k v (branch p m l r*)))))))
454 (tmatch trie
455 ((branch ,p ,m ,l ,r)
456 (if (fxnegative? m)
457 (let-values (((k v l*) (pop l)))
458 (values k v (branch p m l* r)))
459 (let-values (((k v r*) (pop r)))
460 (values k v (branch p m l r*)))))
461 (else (pop trie)))))
462
463 ;;;; Comparisons
464
465 (define (trie=? comp trie1 trie2)
466 (let loop ((s trie1) (t trie2))
467 (cond ((and (trie-empty? s) (trie-empty? t)) #t)
468 ((leaf? s)
469 (and (leaf? t)
470 (fx=? (leaf-key s) (leaf-key t))
471 (=? comp (leaf-value s) (leaf-value t))))
472 ((and (branch? s) (branch? t))
473 (tmatch s
474 ((branch ,p ,m ,l1 ,r1)
475 (tmatch t
476 ((branch ,q ,n ,l2 ,r2)
477 (and (fx=? m n)
478 (fx=? p q)
479 (loop l1 l2)
480 (loop r1 r2)))))))
481 (else #f))))
482
483 ;; Returns the symbol 'less' if trie1 is a proper subset of trie2,
484 ;; 'equal' if they are the same, and 'greater' otherwise. NB that
485 ;; disjoint mappings will compare as greater.
486 (define (trie-subset-compare comp trie1 trie2)
487 (letrec
488 ((compare
489 (lambda (s t)
490 (cond ((eqv? s t) 'equal)
491 ((trie-empty? s) 'less)
492 ((trie-empty? t) 'greater) ; disjoint
493 ((and (leaf? s) (leaf? t))
494 (if (and (fx=? (leaf-key s) (leaf-key t))
495 (=? comp (leaf-value s) (leaf-value t)))
496 'equal
497 'greater))
498 ((leaf? s) ; leaf / branch
499 (tmatch t
500 ((branch ,p ,m ,l ,r)
501 (let ((k (leaf-key s)))
502 (if (match-prefix? k p m)
503 (case (compare s (if (zero-bit? k m) l r))
504 ((greater) 'greater)
505 (else 'less)))))))
506 ((leaf? t) 'greater) ; branch / leaf
507 (else (compare-branches s t)))))
508 (compare-branches
509 (lambda (s t)
510 (tmatch s
511 ((branch ,p ,m ,sl ,sr)
512 (tmatch t
513 ((branch ,q ,n ,tl ,tr)
514 (cond ((branching-bit-higher? m n) 'greater)
515 ((branching-bit-higher? n m)
516 (if (match-prefix? p q n)
517 (let ((res (if (zero-bit? p n)
518 (compare s tl)
519 (compare s tr))))
520 (if (eqv? res 'greater) res 'less))
521 'greater))
522 ((fx=? p q) ; same prefix, compare subtrees
523 (let ((cl (compare sl tl)) (cr (compare sr tr)))
524 (cond ((or (eqv? cl 'greater) (eqv? cr 'greater))
525 'greater)
526 ((and (eqv? cl 'equal) (eqv? cr 'equal))
527 'equal)
528 (else 'less))))
529 (else 'greater))))))))) ; disjoint
530 (compare trie1 trie2)))
531
532 (define (trie-proper-subset? comp trie1 trie2)
533 (eqv? (trie-subset-compare comp trie1 trie2) 'less))
534
535 ;; Two tries are disjoint if they have no keys in common.
536 (define (trie-disjoint? trie1 trie2)
537 (letrec
538 ((disjoint?
539 (lambda (s t)
540 (or (trie-empty? s)
541 (trie-empty? t)
542 (cond ((leaf? s)
543 (let ((k (leaf-key s)))
544 (if (leaf? t)
545 (not (fx=? k (leaf-key t)))
546 (not (trie-contains? t k)))))
547 ((leaf? t) (not (trie-contains? s (leaf-key t))))
548 (else (branches-disjoint? s t))))))
549 (branches-disjoint?
550 (lambda (s t)
551 (tmatch s
552 ((branch ,p ,m ,sl ,sr)
553 (tmatch t
554 ((branch ,q ,n ,tl ,tr)
555 (cond ((and (fx=? m n) (fx=? p q))
556 (and (disjoint? sl tl) (disjoint? sr tr)))
557 ((and (branching-bit-higher? m n)
558 (match-prefix? q p m))
559 (if (zero-bit? q m)
560 (disjoint? sl t)
561 (disjoint? sr t)))
562 ((and (branching-bit-higher? n m)
563 (match-prefix? p q n))
564 (if (zero-bit? p n)
565 (disjoint? s tl)
566 (disjoint? s tr)))
567 (else #t))))))))) ; the prefixes disagree
568 (disjoint? trie1 trie2)))
569
570 (define (trie-delete trie key)
571 (letrec
572 ((update
573 (lambda (t)
574 (tmatch t
575 (empty the-empty-trie)
576 ((leaf ,k ?) (if (fx=? k key) the-empty-trie t))
577 ((branch ,p ,m ,l ,r) (guard (match-prefix? key p m))
578 (if (zero-bit? key m)
579 (branch p m (update l) r)
580 (branch p m l (update r))))
581 (else t))))) ; key doesn't occur in t
582 (update trie)))
583
584 (define (trie-intersection combine trie1 trie2)
585 (letrec
586 ((intersect
587 (lambda (s t)
588 (cond ((or (trie-empty? s) (trie-empty? t)) the-empty-trie)
589 ((and (leaf? s) (leaf? t))
590 (tmatch s
591 ((leaf ,sk ,sv)
592 (tmatch t
593 ((leaf ,tk ,tv) (guard (fx=? sk tk))
594 (leaf sk (combine sk sv tv)))
595 (else the-empty-trie)))))
596 ((leaf? s) (insert-leaf combine s t))
597 ((leaf? t) (insert-leaf (swap-last-args combine) t s))
598 (else (intersect-branches s t)))))
599 (insert-leaf
600 (lambda (comb lf t)
601 (tmatch lf
602 ((leaf ,k ,v)
603 (let lp ((t t))
604 (tmatch t
605 ((leaf ,tk ,tv) (guard (fx=? k tk))
606 (leaf k (comb k v tv)))
607 ((leaf ? ?) the-empty-trie)
608 ((branch ,p ,m ,l ,r)
609 (and (match-prefix? k p m)
610 (if (zero-bit? k m) (lp l) (lp r))))
611 (else the-empty-trie)))))))
612 (intersect-branches
613 (lambda (s t)
614 (tmatch s
615 ((branch ,p ,m ,sl ,sr)
616 (tmatch t
617 ((branch ,q ,n ,tl ,tr)
618 (cond ((branching-bit-higher? m n)
619 (and (match-prefix? q p m)
620 (if (zero-bit? q m)
621 (intersect sl t)
622 (intersect sr t))))
623 ((branching-bit-higher? n m)
624 (and (match-prefix? p q n)
625 (if (zero-bit? p n)
626 (intersect s tl)
627 (intersect s tr))))
628 ((fx=? p q)
629 (branch p m (intersect sl tl) (intersect sr tr)))
630 (else the-empty-trie)))))))))
631 (intersect trie1 trie2)))
632
633 (define (trie-difference trie1 trie2)
634 (letrec
635 ((difference
636 (lambda (s t)
637 (cond ((trie-empty? s) the-empty-trie)
638 ((trie-empty? t) s)
639 ((leaf? s)
640 (trie-assoc t
641 (leaf-key s)
642 (lambda () s)
643 (lambda (_) the-empty-trie)))
644 ((leaf? t) (trie-delete s (leaf-key t)))
645 (else (branch-difference s t)))))
646 (branch-difference
647 (lambda (s t)
648 (tmatch s
649 ((branch ,p ,m ,sl ,sr)
650 (tmatch t
651 ((branch ,q ,n ,tl ,tr)
652 (cond ((and (fx=? m n) (fx=? p q))
653 (branch p m (difference sl tl) (difference sr tr)))
654 ((and (branching-bit-higher? m n)
655 (match-prefix? q p m))
656 (if (zero-bit? q m)
657 (branch p m (difference sl t) sr)
658 (branch p m sl (difference sr t))))
659 ((and (branching-bit-higher? n m)
660 (match-prefix? p q n))
661 (if (zero-bit? p n)
662 (difference s tl)
663 (difference s tr)))
664 (else s)))))))))
665 (difference trie1 trie2)))
666
667 ;; Remove the assoc for key if it exists in trie; otherwise, add the
668 ;; assoc (key, value).
669 (define (%trie-insert-xor trie key value)
670 (trie-alter trie
671 key
672 (lambda (insert _ig) (insert value))
673 (lambda (_k _v _rep delete) (delete))
674 values))
675
676 (define (trie-xor trie1 trie2)
677 (letrec
678 ((merge
679 (lambda (s t)
680 (cond ((trie-empty? s) t)
681 ((trie-empty? t) s)
682 ((leaf? s)
683 (%trie-insert-xor t (leaf-key s) (leaf-value s)))
684 ((leaf? t)
685 (%trie-insert-xor s (leaf-key t) (leaf-value t)))
686 (else (merge-branches s t)))))
687 (merge-branches
688 (lambda (s t)
689 (tmatch s
690 ((branch ,p ,m ,s1 ,s2)
691 (tmatch t
692 ((branch ,q ,n ,t1 ,t2)
693 (cond ((and (fx=? m n) (fx=? p q))
694 (branch p m (merge s1 t1) (merge s2 t2)))
695 ((and (branching-bit-higher? m n) (match-prefix? q p m))
696 (if (zero-bit? q m)
697 (branch p m (merge s1 t) s2)
698 (branch p m s1 (merge s2 t))))
699 ((and (branching-bit-higher? n m) (match-prefix? p q n))
700 (if (zero-bit? p n)
701 (branch q n (merge s t1) t2)
702 (branch q n t1 (merge s t2))))
703 (else
704 (trie-join p m s q n t))))))))))
705 (merge trie1 trie2)))
706
707 ;; Return a trie containing all the elements of `trie' which are
708 ;; less than k, if `inclusive' is false, or less than or equal to
709 ;; k if `inclusive' is true.
710 (define (subtrie< trie k inclusive)
711 (letrec
712 ((split
713 (lambda (t)
714 (tmatch t
715 (empty the-empty-trie)
716 ((leaf ,tk ?)
717 (cond ((fx<? tk k) t)
718 ((and (fx=? tk k) inclusive) t)
719 (else the-empty-trie)))
720 ((branch ,p ,m ,l ,r)
721 (if (match-prefix? k p m)
722 (if (zero-bit? k m)
723 (split l)
724 (trie-union l (split r)))
725 (and (fx<? p k) t)))))))
726 (tmatch trie
727 ((branch ? ,m ,l ,r) (guard (fxnegative? m))
728 (if (fxnegative? k) (split r) (trie-union (split l) r)))
729 (else (split trie)))))
730
731 ;; Return a trie containing all the elements of `trie' which are
732 ;; greater than k, if `inclusive' is false, or greater than or equal
733 ;; to k if `inclusive' is true.
734 (define (subtrie> trie k inclusive)
735 (letrec
736 ((split
737 (lambda (t)
738 (tmatch t
739 (empty the-empty-trie)
740 ((leaf ,tk ?)
741 (cond ((fx>? tk k) t)
742 ((and (fx=? tk k) inclusive) t)
743 (else the-empty-trie)))
744 ((branch ,p ,m ,l ,r)
745 (if (match-prefix? k p m)
746 (if (zero-bit? k m)
747 (trie-union (split l) r)
748 (split r))
749 (and (fx>? p k) t)))))))
750 (tmatch trie
751 ((branch ? ,m ,l ,r) (guard (fxnegative? m))
752 (if (fxnegative? k) (trie-union (split r) l) (split l)))
753 (else (split trie)))))
754
755 ;; Return a trie containing all the elements of `trie' which are
756 ;; greater than/greater than or equal to a and less than/less than
757 ;; or equal to b, depending on the truth values of
758 ;; low-/high-inclusive.
759 (define (subtrie-interval trie a b low-inclusive high-inclusive)
760 (letrec
761 ((interval
762 (lambda (t)
763 (tmatch t
764 (empty the-empty-trie)
765 ((leaf ,tk ?)
766 (and ((if low-inclusive fx>=? fx>?) tk a)
767 ((if high-inclusive fx<=? fx<?) tk b)
768 t))
769 (else (branch-interval t)))))
770 (branch-interval
771 (lambda (t)
772 (tmatch t
773 ((branch ,p ,m ,l ,r)
774 (if (match-prefix? a p m)
775 (if (zero-bit? a m)
776 (if (match-prefix? b p m)
777 (if (zero-bit? b m)
778 (interval l) ; all x < b is in l
779 (trie-union (subtrie> l a low-inclusive)
780 (subtrie< r b high-inclusive)))
781 ;; everything or nothing is less than b
782 (and (fx<? b p)
783 (trie-union (subtrie> l a low-inclusive) r)))
784 (interval r)) ; all x > b is in r
785 ;; everything or nothing is greater than a
786 (and (fx>? p a) (subtrie< t b high-inclusive))))))))
787 (tmatch trie
788 ((branch ? ,m ,l ,r) (guard (fxnegative? m))
789 (cond ((and (fxnegative? a) (fxnegative? b)) (interval r))
790 ((and (fxpositive? a) (fxpositive? b)) (interval l))
791 ;; (a, 0) U (0, b)
792 (else (trie-union (subtrie> r a low-inclusive)
793 (subtrie< l b high-inclusive)))))
794 (else (interval trie)))))
795
796 (define (trie-split trie pivot)
797 (letrec
798 ((split
799 (lambda (t)
800 (tmatch t
801 ((leaf ,k ,v)
802 (if (fx<=? k pivot)
803 (values t the-empty-trie)
804 (values the-empty-trie t)))
805 ((branch ,p ,m ,l ,r)
806 (if (match-prefix? pivot p m)
807 (if (zero-bit? pivot m)
808 (let-values (((ta tb) (split l)))
809 (values ta (trie-union tb r)))
810 (let-values (((ta tb) (split r)))
811 (values (trie-union l ta) tb)))
812 (if (fx<=? p pivot)
813 (values t the-empty-trie)
814 (values the-empty-trie t))))))))
815
816 (tmatch trie
817 (empty (values the-empty-trie the-empty-trie))
818 ((branch ? ,m ,l ,r) (guard (fxnegative? m))
819 (if (fxnegative? pivot)
820 (let-values (((ta tb) (split r)))
821 (values ta (trie-union tb l)))
822 (let-values (((ta tb) (split l)))
823 (values (trie-union r ta) tb))))
824 (else (split trie)))))
825
826 ;;;; Tries as (Integer, *) relations
827
828 (define (trie-relation-map proc trie)
829 (trie-fold-left (lambda (k v t)
830 (let-values (((k* v*) (proc k v)))
831 (assume (valid-integer? k*))
832 (trie-insert t k* v*)))
833 the-empty-trie
834 trie))