@@ -98,7 +98,6 @@ import Control.DeepSeq (NFData(rnf))
98
98
import Control.Monad.ST (ST )
99
99
import Data.Bits ((.&.) , (.|.) , complement )
100
100
import Data.Data hiding (Typeable )
101
- import Data.Ord (comparing )
102
101
import qualified Data.Foldable as Foldable
103
102
import qualified Data.List as L
104
103
import GHC.Exts ((==#) , build , reallyUnsafePtrEquality #)
@@ -219,23 +218,24 @@ equal t1 t2 = go (toList' t1 []) (toList' t2 [])
219
218
go [] [] = True
220
219
go _ _ = False
221
220
222
- instance (Hashable k , Ord k , Hashable v ) => Hashable (HashMap k v ) where
221
+ instance (Hashable k , Hashable v ) => Hashable (HashMap k v ) where
223
222
hashWithSalt salt hm = go salt (toList' hm [] )
224
223
where
225
224
go :: Int -> [HashMap k v ] -> Int
226
- go salt [] = salt
227
- go salt (Leaf _ l : tl) = salt `hashLeafWithSalt` l `go` tl
228
- go salt (Collision _ a : tl) = salt `hashCollisionWithSalt` a `go` tl
229
- go salt (_ : tl) = salt `go` tl
225
+ go s [] = s
226
+ go s (Leaf _ l : tl) = s `hashLeafWithSalt` l `go` tl
227
+ -- For collisions we hashmix hash value, and then array of values' hashes sorted
228
+ go s (Collision h a : tl) = (s `H.hashWithSalt` h) `hashCollisionWithSalt` a `go` tl
229
+ go s (_ : tl) = s `go` tl
230
230
231
231
hashLeafWithSalt :: Int -> Leaf k v -> Int
232
232
hashLeafWithSalt s (L k v) = s `H.hashWithSalt` k `H.hashWithSalt` v
233
233
234
234
hashCollisionWithSalt :: Int -> A. Array (Leaf k v ) -> Int
235
- hashCollisionWithSalt s a = L. foldl' (hashLeafWithSalt) s (L. sortBy (comparing leafKey ) (A. toList a))
235
+ hashCollisionWithSalt s a = L. foldl' H. hashWithSalt s (L. sort ( L. map ( H. hash . leafValue ) (A. toList a) ))
236
236
237
- leafKey :: Leaf k v -> k
238
- leafKey (L k _ ) = k
237
+ leafValue :: Leaf k v -> v
238
+ leafValue (L _ v ) = v
239
239
240
240
-- Helper to get 'Leaf's and 'Collision's as a list.
241
241
toList' :: HashMap k v -> [HashMap k v ] -> [HashMap k v ]
0 commit comments