Debian Codegraph @

%3a146/gleckler/hamt.sls

raw
1 ;;;; Persistent Hash Map
2
3 ;;; Copyright MMXV-MMXVII Arthur A. Gleckler. All rights reserved.
4
5 ;; Permission is hereby granted, free of charge, to any person
6 ;; obtaining a copy of this software and associated documentation
7 ;; files (the "Software"), to deal in the Software without
8 ;; restriction, including without limitation the rights to use, copy,
9 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
10 ;; of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
12
13 ;; The above copyright notice and this permission notice shall be
14 ;; included in all copies or substantial portions of the Software.
15
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 ;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 ;; DEALINGS IN THE SOFTWARE.
24 (library (srfi :146 gleckler hamt)
25 (export fragment->mask
26 hamt->list
27 hamt-fetch
28 hamt-null
29 hamt-null?
30 hamt/count
31 hamt/empty?
32 hamt/for-each
33 hamt/immutable
34 hamt/mutable
35 hamt/mutable?
36 hamt/payload?
37 hamt/put
38 hamt/put!
39 hamt/replace
40 hamt/replace!
41 hash-array-mapped-trie?
42 make-hamt
43
44 ;; These are only needed by tests:
45 collision?
46 hamt-bucket-size
47 hamt-hash-size
48 hamt/root
49 leaf-stride
50 narrow/array
51 narrow/leaves
52 narrow?
53 next-set-bit
54 wide/array
55 wide/children
56 wide?)
57 (import (except (rnrs) define-record-type assert assoc)
58 (only (chezscheme) remainder)
59 (srfi :9)
60 (only (srfi :1) find-tail assoc)
61 (srfi :16)
62 (only (srfi :143) fx-width)
63 (srfi :151)
64 (srfi :146 gleckler hamt-misc)
65 (srfi :146 gleckler vector-edit))
66
67 ;;; Naming conventions:
68
69 ;; =: procedure that compares keys
70 ;; c: bit string representing the non-leaf children present
71 ;; immediately below a sparse node
72 ;; d: datum, or `hamt-null' to represent absence or deletion
73 ;; dp: procedure that takes an existing datum and returns the datum
74 ;; that should replace it. Either may be `hamt-null'. When
75 ;; there is no payload, `hamt-null' is passed.
76 ;; h: hash
77 ;; hp: procedure that computes hash
78 ;; k: key that maps to a particular datum
79 ;; l: bit string representing the leaves present below a sparse node
80 ;; n: node (of type `collision', `narrow', or `wide')
81
82 ;;; Background
83
84 ;; See these papers:
85
86 ;; - Ideal Hash Trees, Phil Bagwell, 2000,
87 ;; <https://infoscience.epfl.ch/record/64398/files/idealhashtrees.pdf>
88
89 ;; - Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable
90 ;; JVM Collections, Steinforder & Vinju, 2015,
91 ;; <http://michael.steindorfer.name/publications/oopsla15.pdf>
92
93 ;; Also, see Clojure's persistent hash maps, which support both
94 ;; mutable ("transient") and persistent modes.
95
96 ;;; Design
97
98 ;; According to Phil Bagwell's paper, "Occasionally an entire 32 bit
99 ;; hash may be consumed and a new one must be computed to
100 ;; differentiate the two keys." Later, he says "The hash function was
101 ;; tailored to give a 32 bit hash. The algorithm requires that the
102 ;; hash can be extended to an arbitrary number of bits. This was
103 ;; accomplished by rehashing the key combined with an integer
104 ;; representing the trie level, zero being the root. Hence if two
105 ;; keys do give the same initial hash then the rehash has a
106 ;; probability of 1 in 2^32 of a further collision." However, I
107 ;; implement collision lists instead because they will be rarely used
108 ;; when hash functions are good, but work well when they're not, as in
109 ;; the case of MIT Scheme's `string-hash'.
110
111 (define (error* msg . args)
112 (error 'hamt msg args))
113
114 (define hamt-hash-slice-size 5)
115 (define hamt-hash-size
116 (let ((word-size fx-width))
117 (- word-size
118 (remainder word-size hamt-hash-slice-size))))
119 (define hamt-hash-modulus (expt 2 hamt-hash-size))
120 (define hamt-bucket-size (expt 2 hamt-hash-slice-size))
121 (define hamt-null (cons 'hamt 'null))
122
123 (define-record-type hash-array-mapped-trie
124 (%make-hamt = count hash mutable? payload? root)
125 hash-array-mapped-trie?
126 (= hamt/=)
127 (count hamt/count set-hamt/count!)
128 (hash hamt/hash)
129 (mutable? hamt/mutable?)
130 (payload? hamt/payload?)
131 (root hamt/root set-hamt/root!))
132
133 (define (make-hamt = hash payload?)
134 (%make-hamt = 0 hash #f payload? (make-empty-narrow)))
135
136 (define-record-type collision
137 (make-collision entries hash)
138 collision?
139 (entries collision/entries)
140 (hash collision/hash))
141
142 (define-record-type narrow
143 (make-narrow array children leaves)
144 narrow?
145 (array narrow/array)
146 (children narrow/children)
147 (leaves narrow/leaves))
148
149 (define-record-type wide
150 (make-wide array children leaves)
151 wide?
152 (array wide/array)
153 (children wide/children set-wide/children!)
154 (leaves wide/leaves set-wide/leaves!))
155
156 (define (hamt/empty? hamt)
157 (zero? (hamt/count hamt)))
158
159 (define (hamt/immutable-inner hamt replace)
160 "Return a HAMT equivalent to `hamt', but that is immutable. Even if
161 `hamt' is mutable, no change to it will affect the returned HAMT. If
162 `hamt' has payloads, replace each datum in a wide node with what
163 `replace' returns when passed the key and corresponding datum. This
164 is useful for converting HAMT sets stored as values in a HAMT map back
165 to immutable ones when the containing map is made immutable. (Only
166 data in wide nodes will have been modified since the change to mutable
167 happened.)"
168 (if (hamt/mutable? hamt)
169 (let ((payload? (hamt/payload? hamt)))
170 (%make-hamt (hamt/= hamt)
171 (hamt/count hamt)
172 (hamt/hash hamt)
173 #f
174 payload?
175 (->immutable (hamt/root hamt) payload? replace)))
176 hamt))
177
178 (define hamt/immutable
179 (case-lambda
180 ((hamt) (hamt/immutable-inner hamt (lambda (k d) d)))
181 ((hamt replace) (hamt/immutable-inner hamt replace))))
182
183 (define (hamt/mutable hamt)
184 (if (hamt/mutable? hamt)
185 hamt
186 (%make-hamt (hamt/= hamt)
187 (hamt/count hamt)
188 (hamt/hash hamt)
189 #t
190 (hamt/payload? hamt)
191 (hamt/root hamt))))
192
193 (define (hamt/replace hamt key dp)
194 (assert (not (hamt/mutable? hamt)))
195 (let*-values (((payload?) (hamt/payload? hamt))
196 ((root) (hamt/root hamt))
197 ((==) (hamt/= hamt))
198 ((hp) (hamt/hash hamt))
199 ((hash) (hash-bits hp key))
200 ((change node) (modify-pure hamt root 0 dp hash key)))
201 (if (eq? node root)
202 hamt
203 (let ((count (+ (hamt/count hamt) change)))
204 (%make-hamt == count hp #f payload? node)))))
205
206 (define (hamt/put hamt key datum)
207 (hamt/replace hamt key (lambda (x) datum)))
208
209 (define (hamt/replace! hamt key dp)
210 (assert (hamt/mutable? hamt))
211 (let*-values (((root) (hamt/root hamt))
212 ((hp) (hamt/hash hamt))
213 ((hash) (hash-bits hp key))
214 ((change node) (mutate hamt root 0 dp hash key)))
215 (unless (zero? change)
216 (set-hamt/count! hamt (+ (hamt/count hamt) change)))
217 (unless (eq? node root)
218 (set-hamt/root! hamt node))
219 hamt))
220
221 (define (hamt/put! hamt key datum)
222 (hamt/replace! hamt key (lambda (x) datum)))
223
224 (define (make-empty-narrow)
225 (make-narrow (vector) 0 0))
226
227 (define (hamt-null? n)
228 (eq? n hamt-null))
229
230 (define (collision-single-leaf? n)
231 (let ((elements (collision/entries n)))
232 (and (not (null? elements))
233 (null? (cdr elements)))))
234
235 (define (narrow-single-leaf? n)
236 (and (zero? (narrow/children n))
237 (= 1 (bit-count (narrow/leaves n)))))
238
239 (define (wide-single-leaf? n)
240 (and (zero? (wide/children n))
241 (= 1 (bit-count (wide/leaves n)))))
242
243 (define (hash-bits hp key)
244 (remainder (hp key) hamt-hash-modulus))
245
246 (define (next-set-bit i start end)
247 (let ((index (first-set-bit (bit-field i start end))))
248 (and (not (= index -1))
249 (+ index start))))
250
251 (define (narrow->wide n payload?)
252 (let* ((c (narrow/children n))
253 (l (narrow/leaves n))
254 (stride (leaf-stride payload?))
255 (a-in (narrow/array n))
256 (a-out (make-vector (* stride hamt-bucket-size))))
257 (let next-leaf ((start 0) (count 0))
258 (let ((i (next-set-bit l start hamt-bucket-size)))
259 (when i
260 (let ((j (* stride i)))
261 (vector-set! a-out j (vector-ref a-in count))
262 (when payload?
263 (vector-set! a-out (+ j 1) (vector-ref a-in (+ count 1)))))
264 (next-leaf (+ i 1) (+ stride count)))))
265 (let next-child ((start 0) (offset (* stride (bit-count l))))
266 (let ((i (next-set-bit c start hamt-bucket-size)))
267 (when i
268 (vector-set! a-out (* stride i) (vector-ref a-in offset))
269 (next-child (+ i 1) (+ offset 1)))))
270 (make-wide a-out c l)))
271
272 (define (->immutable n payload? replace)
273 "Convert `n' and its descendants into `collision' or `narrow' nodes.
274 Stop at the first `collision' node or `narrow' node on each path. If
275 `payload?' is true, then expect data, not just keys, and replace each
276 datum in a wide node with what `replace' returns when passed the key
277 and corresponding datum."
278 (cond ((collision? n) n)
279 ((narrow? n) n)
280 ((wide? n)
281 (let* ((c (wide/children n))
282 (l (wide/leaves n))
283 (stride (leaf-stride payload?))
284 (l-count (bit-count l))
285 (a-in (wide/array n))
286 (a-out (make-vector
287 (+ (* stride l-count) (bit-count c)))))
288 (let next-leaf ((start 0) (count 0))
289 (let ((i (next-set-bit l
290 start
291 hamt-bucket-size)))
292 (when i
293 (let* ((j (* stride i))
294 (key (vector-ref a-in j)))
295 (vector-set! a-out count key)
296 (when payload?
297 (vector-set! a-out
298 (+ count 1)
299 (replace
300 key
301 (vector-ref a-in (+ j 1))))))
302 (next-leaf (+ i 1) (+ stride count)))))
303 (let next-child ((start 0) (offset (* stride l-count)))
304 (let ((i (next-set-bit c
305 start
306 hamt-bucket-size)))
307 (when i
308 (vector-set! a-out
309 offset
310 (->immutable (vector-ref a-in (* stride i))
311 payload?
312 replace))
313 (next-child (+ i 1) (+ offset 1)))))
314 (make-narrow a-out c l)))
315 (else (error* "Unexpected type of node."))))
316
317 (define (hash-fragment shift hash)
318 (bit-field hash shift (+ shift hamt-hash-slice-size)))
319
320 (define (fragment->mask fragment)
321 (- (expt 2 fragment) 1))
322
323 (define (mutate hamt n shift dp h k)
324 (cond ((collision? n) (modify-collision hamt n shift dp h k))
325 ((narrow? n)
326 (modify-wide hamt
327 (narrow->wide n (hamt/payload? hamt))
328 shift
329 dp
330 h
331 k))
332 ((wide? n) (modify-wide hamt n shift dp h k))
333 (else (error* "Unknown HAMT node type." n))))
334
335 (define (modify-wide hamt n shift dp h k)
336 (let ((fragment (hash-fragment shift h)))
337 (cond ((bit-set? fragment (wide/children n))
338 (modify-wide-child hamt n shift dp h k))
339 ((bit-set? fragment (wide/leaves n))
340 (modify-wide-leaf hamt n shift dp h k))
341 (else
342 (let ((d (dp hamt-null)))
343 (if (hamt-null? d)
344 (values 0 n)
345 (modify-wide-new hamt n shift d h k)))))))
346
347 (define (modify-wide-child hamt n shift dp h k)
348 (let*-values (((fragment) (hash-fragment shift h))
349 ((array) (wide/array n))
350 ((payload?) (hamt/payload? hamt))
351 ((stride) (leaf-stride payload?))
352 ((i) (* stride fragment))
353 ((child) (vector-ref array i))
354 ((change new-child)
355 (mutate hamt
356 child
357 (+ shift hamt-hash-slice-size)
358 dp
359 h
360 k)))
361 (define (coalesce key datum)
362 (vector-set! array i key)
363 (when payload?
364 (vector-set! array (+ i 1) datum))
365 (set-wide/children! n (copy-bit fragment (wide/children n) #f))
366 (set-wide/leaves! n (copy-bit fragment (wide/leaves n) #t))
367 (values change n))
368 (define (replace)
369 (vector-set! array i new-child)
370 (values change n))
371 (cond ((eq? new-child child) (values change n))
372 ((hamt-null? new-child)
373 (error* "Child cannot become null." n))
374 ((collision? new-child)
375 (if (collision-single-leaf? new-child)
376 (let ((a (car (collision/entries new-child))))
377 (if payload?
378 (coalesce (car a) (cdr a))
379 (coalesce a #f)))
380 (replace)))
381 ((wide? new-child)
382 (if (wide-single-leaf? new-child)
383 (let ((a (wide/array new-child))
384 (j (* stride (next-set-bit (wide/leaves new-child)
385 0
386 hamt-bucket-size))))
387 (coalesce (vector-ref a j)
388 (and payload? (vector-ref a (+ j 1)))))
389 (replace)))
390 ((narrow? new-child)
391 (replace))
392 (else (error* "Unexpected type of child node.")))))
393
394 (define (modify-wide-leaf hamt n shift dp h k)
395 (let* ((fragment (hash-fragment shift h))
396 (array (wide/array n))
397 (payload? (hamt/payload? hamt))
398 (stride (leaf-stride payload?))
399 (i (* stride fragment))
400 (key (vector-ref array i)))
401 (if ((hamt/= hamt) k key)
402 (let* ((existing (if payload? (vector-ref array (+ i 1)) hamt-null))
403 (d (dp existing)))
404 (cond ((hamt-null? d)
405 (vector-set! array i #f)
406 (when payload? (vector-set! array (+ i 1) #f))
407 (set-wide/leaves! n (copy-bit fragment (wide/leaves n) #f))
408 (values -1 n))
409 (else
410 (when payload? (vector-set! array (+ i 1) d))
411 (values 0 n))))
412 (let ((d (dp hamt-null)))
413 (if (hamt-null? d)
414 (values 0 n)
415 (add-wide-leaf-key hamt n shift d h k))))))
416
417 (define (add-wide-leaf-key hamt n shift d h k)
418 (define payload? (hamt/payload? hamt))
419 (define make-entry
420 (if payload? cons (lambda (k d) k)))
421 (let* ((fragment (hash-fragment shift h))
422 (array (wide/array n))
423 (stride (leaf-stride payload?))
424 (i (* stride fragment))
425 (key (vector-ref array i))
426 (hash (hash-bits (hamt/hash hamt) key))
427 (datum (and payload? (vector-ref array (+ i 1)))))
428 (vector-set! array
429 i
430 (if (= h hash)
431 (make-collision (list (make-entry k d)
432 (make-entry key datum))
433 h)
434 (make-narrow-with-two-keys
435 payload?
436 (+ shift hamt-hash-slice-size)
437 h
438 k
439 d
440 hash
441 key
442 datum)))
443 (when payload?
444 (vector-set! array (+ i 1) #f))
445 (set-wide/children! n (copy-bit fragment (wide/children n) #t))
446 (set-wide/leaves! n (copy-bit fragment (wide/leaves n) #f))
447 (values 1 n)))
448
449 (define (modify-wide-new hamt n shift d h k)
450 (let* ((fragment (hash-fragment shift h))
451 (array (wide/array n))
452 (payload? (hamt/payload? hamt))
453 (stride (leaf-stride payload?))
454 (i (* stride fragment)))
455 (vector-set! array i k)
456 (when payload?
457 (vector-set! array (+ i 1) d))
458 (set-wide/leaves! n (copy-bit fragment (wide/leaves n) #t))
459 (values 1 n)))
460
461 (define (make-narrow-with-two-keys payload? shift h1 k1 d1 h2 k2 d2)
462 (define (two-leaves f1 k1 d1 f2 k2 d2)
463 (make-narrow
464 (if payload?
465 (vector k1 d1 k2 d2)
466 (vector k1 k2))
467 0
468 (copy-bit f2 (copy-bit f1 0 #t) #t)))
469 (assert (not (= h1 h2)))
470 (let ((f1 (hash-fragment shift h1))
471 (f2 (hash-fragment shift h2)))
472 (cond ((= f1 f2)
473 (make-narrow
474 (vector (make-narrow-with-two-keys payload?
475 (+ shift hamt-hash-slice-size)
476 h1
477 k1
478 d1
479 h2
480 k2
481 d2))
482 (copy-bit f1 0 #t)
483 0))
484 ((< f1 f2)
485 (two-leaves f1 k1 d1 f2 k2 d2))
486 (else
487 (two-leaves f2 k2 d2 f1 k1 d1)))))
488
489 (define (modify-pure hamt n shift dp h k)
490 (cond ((collision? n) (modify-collision hamt n shift dp h k))
491 ((narrow? n) (modify-narrow hamt n shift dp h k))
492 ((wide? n) (error* "Should have been converted to narrow before here."))
493 (else (error* "Unknown HAMT node type." n))))
494
495 (define (lower-collision hamt n shift dp h k)
496 "If we try to add a key to a collision but it has a different hash
497 than the collision's elements, add it to a narrow above the collision
498 instead. Add as many levels of child-only narrows as needed to reach
499 the point where the hash fragments differ. This is guaranteed to
500 happen at some level because we're only called when the full hashes
501 differ."
502 (let ((collision-hash (collision/hash n))
503 (d (dp hamt-null)))
504 (if (hamt-null? d)
505 (values 0 n)
506 (values
507 1
508 (let descend ((shift shift))
509 (let ((collision-fragment (hash-fragment shift collision-hash))
510 (leaf-fragment (hash-fragment shift h)))
511 (if (= collision-fragment leaf-fragment)
512 (let ((child (descend (+ shift hamt-hash-slice-size))))
513 (make-narrow
514 (vector child)
515 (copy-bit collision-fragment 0 #t)
516 0))
517 (make-narrow
518 (if (hamt/payload? hamt)
519 (vector k d n)
520 (vector k n))
521 (copy-bit collision-fragment 0 #t)
522 (copy-bit leaf-fragment 0 #t)))))))))
523
524 (define (modify-collision hamt n shift dp h k)
525 (if (= h (collision/hash n))
526 (let ((payload? (hamt/payload? hamt)))
527 (let next ((entries (collision/entries n))
528 (checked '()))
529 (if (null? entries)
530 (let ((d (dp hamt-null)))
531 (if (hamt-null? d)
532 (values 0 n)
533 (values 1
534 (make-collision (if payload?
535 (cons (cons k d) checked)
536 (cons k checked))
537 h))))
538 (let* ((entry (car entries))
539 (key (if payload? (car entry) entry)))
540 (if ((hamt/= hamt) k key)
541 (let* ((existing (if payload? (cdr entry) hamt-null))
542 (d (dp existing))
543 (delete? (hamt-null? d))
544 (others (append checked (cdr entries))))
545 (values
546 (if delete? -1 0)
547 (make-collision (cond (delete? others)
548 (payload? (cons (cons k d) others))
549 (else (cons k others)))
550 h)))
551 (next (cdr entries)
552 (cons (car entries) checked)))))))
553 (lower-collision hamt n shift dp h k)))
554
555 ;; If we're storing "payloads," i.e. a datum to go with each key, we
556 ;; must reserve two spots for each key in each vector. Otherwise, we
557 ;; need only one.
558 (define (leaf-stride payload?)
559 (if payload? 2 1))
560
561 (define (narrow-child-index l c mask payload?)
562 (+ (* (leaf-stride payload?) (bit-count l))
563 (bit-count (bitwise-and c mask))))
564
565 (define (narrow-leaf-index l mask payload?)
566 (* (leaf-stride payload?) (bit-count (bitwise-and l mask))))
567
568 (define (modify-narrow hamt n shift dp h k)
569 (let ((fragment (hash-fragment shift h)))
570 (cond ((bit-set? fragment (narrow/children n))
571 (modify-narrow-child hamt n shift dp h k))
572 ((bit-set? fragment (narrow/leaves n))
573 (modify-narrow-leaf hamt n shift dp h k))
574 (else
575 (let ((d (dp hamt-null)))
576 (if (hamt-null? d)
577 (values 0 n)
578 (modify-narrow-new hamt n shift d h k)))))))
579
580 (define (modify-narrow-child hamt n shift dp h k)
581 (let*-values (((fragment) (hash-fragment shift h))
582 ((mask) (fragment->mask fragment))
583 ((c) (narrow/children n))
584 ((l) (narrow/leaves n))
585 ((array) (narrow/array n))
586 ((payload?) (hamt/payload? hamt))
587 ((child-index)
588 (narrow-child-index l c mask payload?))
589 ((child) (vector-ref array child-index))
590 ((change new-child)
591 (modify-pure hamt
592 child
593 (+ shift hamt-hash-slice-size)
594 dp
595 h
596 k)))
597 (define (coalesce key datum)
598 (let ((leaf-index (narrow-leaf-index l mask payload?)))
599 (values change
600 (make-narrow (if payload?
601 (vector-edit array
602 (add leaf-index key)
603 (add leaf-index datum)
604 (drop child-index 1))
605 (vector-edit array
606 (add leaf-index key)
607 (drop child-index 1)))
608 (copy-bit fragment c #f)
609 (copy-bit fragment l #t)))))
610 (define (replace)
611 (values change
612 (make-narrow (vector-replace-one array child-index new-child)
613 c
614 l)))
615 (cond ((eq? new-child child) (values 0 n))
616 ((hamt-null? new-child)
617 (error* "Child cannot become null." n))
618 ((collision? new-child)
619 (if (collision-single-leaf? new-child)
620 (let ((a (car (collision/entries new-child))))
621 (if payload?
622 (coalesce (car a) (cdr a))
623 (coalesce a #f)))
624 (replace)))
625 ((narrow? new-child)
626 (if (narrow-single-leaf? new-child)
627 (let ((a (narrow/array new-child)))
628 (coalesce (vector-ref a 0)
629 (and payload? (vector-ref a 1))))
630 (replace)))
631 ((wide? new-child)
632 (error* "New child should be collision or narrow."))
633 (else (error* "Unexpected type of child node.")))))
634
635 (define (modify-narrow-leaf hamt n shift dp h k)
636 (let* ((fragment (hash-fragment shift h))
637 (mask (fragment->mask fragment))
638 (c (narrow/children n))
639 (l (narrow/leaves n))
640 (array (narrow/array n))
641 (payload? (hamt/payload? hamt))
642 (stride (leaf-stride payload?))
643 (leaf-index (narrow-leaf-index l mask payload?))
644 (key (vector-ref array leaf-index)))
645 (if ((hamt/= hamt) k key)
646 (let* ((existing (if payload?
647 (vector-ref array (+ leaf-index 1))
648 hamt-null))
649 (d (dp existing)))
650 (cond ((hamt-null? d)
651 (values -1
652 (make-narrow (vector-without array
653 leaf-index
654 (+ leaf-index stride))
655 c
656 (copy-bit fragment l #f))))
657 (payload?
658 (values
659 0
660 (make-narrow (vector-replace-one array (+ leaf-index 1) d)
661 c
662 l)))
663 (else (values 0 n))))
664 (let ((d (dp hamt-null)))
665 (if (hamt-null? d)
666 (values 0 n)
667 (add-narrow-leaf-key hamt n shift d h k))))))
668
669 (define (add-narrow-leaf-key hamt n shift d h k)
670 (define payload? (hamt/payload? hamt))
671 (define make-entry
672 (if payload? cons (lambda (k d) k)))
673 (let* ((fragment (hash-fragment shift h))
674 (mask (fragment->mask fragment))
675 (c (narrow/children n))
676 (l (narrow/leaves n))
677 (array (narrow/array n))
678 (payload? (hamt/payload? hamt))
679 (stride (leaf-stride payload?))
680 (leaf-index (narrow-leaf-index l mask payload?))
681 (key (vector-ref array leaf-index))
682 (child-index (narrow-child-index l c mask payload?))
683 (hash (hash-bits (hamt/hash hamt) key))
684 (datum (and payload? (vector-ref array (+ leaf-index 1)))))
685 (values 1
686 (make-narrow (if (= h hash)
687 (vector-edit
688 array
689 (drop leaf-index stride)
690 (add child-index
691 (make-collision (list (make-entry k d)
692 (make-entry key datum))
693 h)))
694 (vector-edit
695 array
696 (drop leaf-index stride)
697 (add child-index
698 (make-narrow-with-two-keys
699 payload?
700 (+ shift hamt-hash-slice-size)
701 h
702 k
703 d
704 hash
705 key
706 datum))))
707 (copy-bit fragment c #t)
708 (copy-bit fragment l #f)))))
709
710 (define (modify-narrow-new hamt n shift d h k)
711 (let* ((fragment (hash-fragment shift h))
712 (mask (fragment->mask fragment))
713 (c (narrow/children n))
714 (l (narrow/leaves n))
715 (array (narrow/array n))
716 (payload? (hamt/payload? hamt))
717 (leaf-index (narrow-leaf-index l mask payload?))
718 (delete? (hamt-null? d)))
719 (values 1
720 (make-narrow (if payload?
721 (vector-edit array
722 (add leaf-index k)
723 (add leaf-index d))
724 (vector-edit array
725 (add leaf-index k)))
726 c
727 (copy-bit fragment l #t)))))
728
729 (define (hamt-fetch hamt key)
730 "Fetch datum from `hamt' at `key'. Return `hamt-null' if the key is
731 not present. If `hamt' stores no payloads, return the symbol
732 `present' if the key is present."
733 (let ((h (hash-bits (hamt/hash hamt) key))
734 (payload? (hamt/payload? hamt)))
735 (let descend ((n (hamt/root hamt))
736 (shift 0))
737 (cond ((collision? n)
738 (let ((entries (collision/entries n))
739 (key= (hamt/= hamt)))
740 (if payload?
741 (cond ((assoc key entries key=) => cdr)
742 (else hamt-null))
743 (if (find-tail (lambda (e) (key= key e)) entries)
744 'present
745 hamt-null))))
746 ((narrow? n)
747 (let ((array (narrow/array n))
748 (c (narrow/children n))
749 (l (narrow/leaves n))
750 (fragment (hash-fragment shift h)))
751 (cond ((bit-set? fragment c)
752 (let* ((mask (fragment->mask fragment))
753 (child-index (narrow-child-index
754 l
755 c
756 mask
757 (hamt/payload? hamt))))
758 (descend (vector-ref array child-index)
759 (+ shift hamt-hash-slice-size))))
760 ((bit-set? fragment l)
761 (let* ((mask (fragment->mask fragment))
762 (leaf-index
763 (narrow-leaf-index l mask (hamt/payload? hamt)))
764 (k (vector-ref array leaf-index)))
765 (if ((hamt/= hamt) k key)
766 (if payload?
767 (vector-ref array (+ leaf-index 1))
768 'present)
769 hamt-null)))
770 (else hamt-null))))
771 ((wide? n)
772 (let ((array (wide/array n))
773 (stride (leaf-stride (hamt/payload? hamt)))
774 (c (wide/children n))
775 (l (wide/leaves n))
776 (i (hash-fragment shift h)))
777 (cond ((bit-set? i c)
778 (descend (vector-ref array (* stride i))
779 (+ shift hamt-hash-slice-size)))
780 ((bit-set? i l)
781 (let* ((j (* stride i))
782 (k (vector-ref array j)))
783 (if ((hamt/= hamt) k key)
784 (if payload?
785 (vector-ref array (+ j 1))
786 'present)
787 hamt-null)))
788 (else hamt-null))))
789 (else (error* "Unexpected type of child node."))))))
790
791 (define (collision/for-each procedure node payload?)
792 (if payload?
793 (do-list (e (collision/entries node))
794 (procedure (car e) (cdr e)))
795 (do-list (e (collision/entries node))
796 (procedure e #f))))
797
798 (define (narrow/for-each procedure node payload?)
799 (let ((array (narrow/array node))
800 (stride (leaf-stride payload?))
801 (c (narrow/children node))
802 (l (narrow/leaves node)))
803 (let next-leaf ((count 0)
804 (start 0))
805 (let ((i (next-set-bit l start hamt-bucket-size)))
806 (if i
807 (let* ((j (* stride count))
808 (k (vector-ref array j))
809 (d (and payload? (vector-ref array (+ j 1)))))
810 (procedure k d)
811 (next-leaf (+ count 1) (+ i 1)))
812 (let next-child ((start 0)
813 (offset (* stride count)))
814 (let ((i (next-set-bit c start hamt-bucket-size)))
815 (when i
816 (let ((child (vector-ref array offset)))
817 (hamt-node/for-each child payload? procedure)
818 (next-child (+ i 1) (+ offset 1)))))))))))
819
820 (define (wide/for-each procedure node payload?)
821 (let ((array (wide/array node))
822 (stride (leaf-stride payload?))
823 (c (wide/children node))
824 (l (wide/leaves node)))
825 (do ((i 0 (+ i 1)))
826 ((= i hamt-bucket-size))
827 (let ((j (* stride i)))
828 (cond ((bit-set? i l)
829 (let ((k (vector-ref array j))
830 (d (and payload? (vector-ref array (+ j 1)))))
831 (procedure k d)))
832 ((bit-set? i c)
833 (let ((child (vector-ref array j)))
834 (hamt-node/for-each child payload? procedure))))))))
835
836 (define (hamt-node/for-each node payload? procedure)
837 (cond ((collision? node) (collision/for-each procedure node payload?))
838 ((narrow? node) (narrow/for-each procedure node payload?))
839 ((wide? node) (wide/for-each procedure node payload?))
840 (else (error* "Invalid type of node." node))))
841
842 (define (hamt/for-each procedure hamt)
843 (hamt-node/for-each (hamt/root hamt)
844 (hamt/payload? hamt)
845 procedure))
846
847 (define (hamt->list hamt procedure)
848 (let ((accumulator '()))
849 (hamt/for-each (lambda (k v)
850 (set! accumulator
851 (cons (procedure k v)
852 accumulator)))
853 hamt)
854 accumulator))
855
856 ;;; Debugging
857
858 (define (assert-collision-valid node hp payload?)
859 "Do sanity checks on a collision. Return the list of all keys
860 present."
861 (let ((entries (collision/entries node))
862 (hash (collision/hash node))
863 (extract (if payload? car (lambda (x) x))))
864 (do-list (a entries)
865 (assert (= hash (hash-bits hp (extract a)))))
866 (if payload?
867 (map car entries)
868 entries)))
869
870 (define (assert-narrow-valid node hp payload? shift)
871 "Do sanity checks on a narrow and all its children. Return the list
872 of all keys present."
873 (let ((array (narrow/array node))
874 (stride (leaf-stride payload?))
875 (c (narrow/children node))
876 (l (narrow/leaves node)))
877 (assert (zero? (bitwise-and c l)))
878 (let next-leaf ((count 0)
879 (i 0)
880 (keys '()))
881 (if (< i hamt-bucket-size)
882 (cond ((bit-set? i l)
883 (let ((k (vector-ref array (* stride count))))
884 (assert (= i (hash-fragment shift (hash-bits hp k))))
885 (next-leaf (+ count 1) (+ i 1) (cons k keys))))
886 (else (next-leaf count (+ i 1) keys)))
887 (let next-child ((i 0)
888 (key-groups (list keys))
889 (offset (* stride count)))
890 (if (= i hamt-bucket-size)
891 (apply append key-groups)
892 (cond ((bit-set? i c)
893 (let* ((child (vector-ref array offset))
894 (child-keys (assert-hamt-node-valid
895 child
896 hp
897 payload?
898 (+ shift hamt-hash-slice-size))))
899 (do-list (k child-keys)
900 (assert (= i
901 (hash-fragment shift (hash-bits hp k)))))
902 (next-child (+ i 1)
903 (cons child-keys key-groups)
904 (+ offset 1))))
905 (else (next-child (+ i 1) key-groups offset)))))))))
906
907 (define (assert-wide-valid node hp payload? shift)
908 "Do sanity checks on a wide and all its children. Return the list
909 of all keys present."
910 (let ((array (wide/array node))
911 (stride (leaf-stride payload?))
912 (c (wide/children node))
913 (l (wide/leaves node)))
914 (assert (zero? (bitwise-and c l)))
915 (let next-fragment ((i 0)
916 (key-groups '()))
917 (if (= i hamt-bucket-size)
918 (apply append key-groups)
919 (let ((j (* stride i)))
920 (cond ((bit-set? i l)
921 (let ((k (vector-ref array j)))
922 (assert (= i (hash-fragment shift (hash-bits hp k))))
923 (next-fragment (+ i 1) (cons (list k) key-groups))))
924 ((bit-set? i c)
925 (let* ((child (vector-ref array j))
926 (child-keys (assert-hamt-node-valid
927 child
928 hp
929 payload?
930 (+ shift hamt-hash-slice-size))))
931 (do-list (k child-keys)
932 (assert (= i
933 (hash-fragment shift (hash-bits hp k)))))
934 (next-fragment (+ i 1)
935 (cons child-keys key-groups))))
936 (else
937 (assert (not (vector-ref array j)))
938 (when payload?
939 (assert (not (vector-ref array (+ j 1)))))
940 (next-fragment (+ i 1) key-groups))))))))
941
942 (define (assert-hamt-node-valid node hp payload? shift)
943 "Do sanity checks on a HAMT node and all its children. Return the
944 list of all keys present."
945 (cond ((collision? node) (assert-collision-valid node hp payload?))
946 ((narrow? node) (assert-narrow-valid node hp payload? shift))
947 ((wide? node) (assert-wide-valid node hp payload? shift))
948 (else (error* "Invalid type of node." node))))
949
950 (define (assert-hamt-valid hamt)
951 "Do sanity checks on `hamt'."
952 (let ((hp (hamt/hash hamt)))
953 (assert (procedure? (hamt/= hamt)))
954 (assert (procedure? hp))
955 (assert (memq (hamt/mutable? hamt) '(#t #f)))
956 (let* ((payload? (hamt/payload? hamt))
957 (keys (assert-hamt-node-valid (hamt/root hamt) hp payload? 0)))
958 (assert (= (hamt/count hamt) (length keys)))))))