Add gen:equal+hash for tries to properly support O(1) hashconsing

This commit is contained in:
Tony Garnock-Jones 2016-03-13 12:45:14 -04:00
parent fc271b6398
commit e0ba76dc4e
1 changed files with 17 additions and 0 deletions

View File

@ -122,6 +122,23 @@
#:methods gen:custom-write
[(define (write-proc v port mode) (pretty-print-trie v port #:with-parens #t))])
(struct branch (opens wild sigmas) #:transparent
#:methods gen:equal+hash
[(define (equal-proc a b =?)
(match-define (branch os1 w1 h1) a)
(match-define (branch os2 w2 h2) b)
(and (eq? os1 os2)
(eq? w1 w2)
(eq? h1 h2)))
(define (hash-proc a h)
(match-define (branch os w h) a)
(+ (eq-hash-code a)
(eq-hash-code w)
(eq-hash-code h)))
(define (hash2-proc a h)
(match-define (branch os w h) a)
(bitwise-xor (eq-hash-code a)
(eq-hash-code w)
(eq-hash-code h)))]
#:methods gen:custom-write
[(define (write-proc v port mode) (pretty-print-trie v port #:with-parens #t))])