boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

164-hash-table.scm (1722B)


      1 ; Private micro+boot2 hash tables: content-keyed bytes, identity-keyed scalar
      2 ; values, replacement, deletion/tombstones, resize, and GC tracing.
      3 
      4 (define (check ok code)
      5   (if ok #t (sys-exit code)))
      6 
      7 (define h (%make-hash-table 1))
      8 
      9 (%hash-set! h "alpha" "kept")
     10 (%hash-set! h (bytevector 98 101 116 97) 2) ; "beta" as a bytevector
     11 (check (equal? (%hash-ref h "alpha") "kept") 1)
     12 (check (= (%hash-ref h "beta") 2) 2)        ; mixed string/bv lookup
     13 (check (= (%hash-size h) 2) 3)
     14 
     15 ; Replacement must not change size.
     16 (%hash-set! h "alpha" 11)
     17 (check (= (%hash-ref h "alpha") 11) 4)
     18 (check (= (%hash-size h) 2) 5)
     19 
     20 ; Grow through several capacities and retain every scalar key.
     21 (let loop ((i 0))
     22   (if (= i 200)
     23       #t
     24       (begin
     25         (%hash-set! h i (+ i 1000))
     26         (loop (+ i 1)))))
     27 (check (= (%hash-ref h 0) 1000) 6)
     28 (check (= (%hash-ref h 199) 1199) 7)
     29 (check (= (%hash-size h) 202) 8)
     30 
     31 ; Delete both a byte key and enough scalar keys to leave tombstones, then
     32 ; insert a disjoint range so the table must reuse/rebuild them.
     33 (check (%hash-delete! h "beta") 9)
     34 (check (not (%hash-delete! h "missing")) 10)
     35 (let loop ((i 0))
     36   (if (= i 100)
     37       #t
     38       (begin
     39         (check (%hash-delete! h i) 11)
     40         (loop (+ i 1)))))
     41 (let loop ((i 200))
     42   (if (= i 400)
     43       #t
     44       (begin
     45         (%hash-set! h i (+ i 1000))
     46         (loop (+ i 1)))))
     47 (check (not (%hash-ref h "beta")) 12)
     48 (check (not (%hash-ref h 50)) 13)
     49 (check (= (%hash-ref h 350) 1350) 14)
     50 (check (= (%hash-size h) 301) 15)
     51 
     52 ; Table storage, keys, and values are traced through a collection.
     53 (collect-garbage)
     54 (check (= (%hash-ref h "alpha") 11) 16)
     55 (check (= (%hash-ref h 199) 1199) 17)
     56 (check (= (%hash-ref h 399) 1399) 18)
     57 
     58 (sys-exit 0)