1234567891011121314151617181920212223242526 |
- (use-modules (srfi srfi-1))
- (define (make-dumbhash size)
- "Make a dumb hash table: an array of buckets"
- (make-array '() size))
- (define* (dumbhash-ref dumbhash key #:optional (default #f))
- "Pull a value out of a dumbhash"
- (let* ((hashed-key (hash key (array-length dumbhash)))
- (bucket (array-ref dumbhash hashed-key)))
- (or (find (lambda (x) (equal? (car x) key))
- bucket)
- default)))
- (define (dumbhash-set! dumbhash key val)
- "Set a value in a dumbhash"
- (let* ((hashed-key (hash key (array-length dumbhash)))
- (bucket (array-ref dumbhash hashed-key)))
- ;; Only act if it's not already a member
- (if (not (find (lambda (x) (equal? (car x) key))
- bucket))
- (array-set! dumbhash
- ;; extend the bucket with the key-val pair
- (cons (cons key val) bucket)
- hashed-key))))
|