7 | defaultSalt = 14695981039346656037
9 | export infixl 10 `hashWithSalt`
10 | export infixl 10 `hash`
15 | interface Hashable a where
18 | hashWithSalt : Bits64 -> a -> Bits64
22 | hash = hashWithSalt defaultSalt
26 | combine : Bits64 -> Bits64 -> Bits64
27 | combine h1 h2 = (h1 * 16777619) `prim__xor_Bits64` h2
31 | defaultHashWithSalt : (a -> Bits64) -> Bits64 -> a -> Bits64
32 | defaultHashWithSalt hash salt x = salt `combine` hash x
35 | Hashable Bits8 where
37 | hashWithSalt = defaultHashWithSalt hash
40 | Hashable Bits16 where
42 | hashWithSalt = defaultHashWithSalt hash
45 | Hashable Bits32 where
47 | hashWithSalt = defaultHashWithSalt hash
50 | Hashable Bits64 where
52 | hashWithSalt = defaultHashWithSalt hash
57 | hashWithSalt = defaultHashWithSalt hash
62 | hashWithSalt = defaultHashWithSalt hash
65 | Hashable Int16 where
67 | hashWithSalt = defaultHashWithSalt hash
70 | Hashable Int32 where
72 | hashWithSalt = defaultHashWithSalt hash
75 | Hashable Int64 where
77 | hashWithSalt = defaultHashWithSalt hash
82 | hashWithSalt = defaultHashWithSalt hash
85 | Hashable Integer where
86 | hashWithSalt salt x = hashPositive (makeSalt salt x) (abs x)
88 | makeSalt : Bits64 -> Integer -> Bits64
89 | makeSalt x y = if y >= 0
94 | bits64Max = 1 `prim__shl_Integer` 64 - 1
96 | hashPositive : Bits64 -> Integer -> Bits64
97 | hashPositive salt x = if x > bits64Max
100 | (cast {to=Bits64} (x `prim__and_Integer` bits64Max)))
101 | (assert_smaller x $
x `prim__shr_Integer` 64)
102 | else hashWithSalt salt (cast {to=Bits64} x)
106 | hash x = hash (cast {to=Integer} x)
107 | hashWithSalt salt x = hashWithSalt salt (cast {to=Integer} x)
110 | Hashable String where
111 | hashWithSalt salt str = finalise (foldl step (salt, 0) $
unpack str)
113 | step : (Bits64, Bits64) -> Char -> (Bits64, Bits64)
114 | step (s, l) x = (hashWithSalt s x, l + 1)
115 | finalise : (Bits64, Bits64) -> Bits64
116 | finalise (s, l) = hashWithSalt s l
119 | Hashable Bool where
123 | hashWithSalt = defaultHashWithSalt hash
126 | (Hashable a, Hashable b) => Hashable (a, b) where
127 | hashWithSalt salt (a, b) = salt `hashWithSalt` a `hashWithSalt` b
130 | Hashable a => Hashable (Maybe a) where
131 | hashWithSalt salt Nothing = hashWithSalt salt 0
132 | hashWithSalt salt (Just x) = hashWithSalt salt 1 `hashWithSalt` x
135 | Hashable a => Hashable (List a) where
136 | hashWithSalt salt [] = hashWithSalt salt 0
137 | hashWithSalt salt (x :: xs) =
144 | Hashable a => Hashable (SnocList a) where
145 | hashWithSalt salt [<] = hashWithSalt salt 0
146 | hashWithSalt salt (sx :< x) =
153 | Hashable a => Hashable (Vect len a) where
154 | hashWithSalt salt [] = hashWithSalt salt 0
155 | hashWithSalt salt (x :: xs) =