Skip to content

Commit 2b23b81

Browse files
committed
Drop Ord requirement for Hashable HashMap instance
1 parent 89d753f commit 2b23b81

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Data/HashMap/Base.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ import Control.DeepSeq (NFData(rnf))
9898
import Control.Monad.ST (ST)
9999
import Data.Bits ((.&.), (.|.), complement)
100100
import Data.Data hiding (Typeable)
101-
import Data.Ord (comparing)
102101
import qualified Data.Foldable as Foldable
103102
import qualified Data.List as L
104103
import GHC.Exts ((==#), build, reallyUnsafePtrEquality#)
@@ -219,23 +218,24 @@ equal t1 t2 = go (toList' t1 []) (toList' t2 [])
219218
go [] [] = True
220219
go _ _ = False
221220

222-
instance (Hashable k, Ord k, Hashable v) => Hashable (HashMap k v) where
221+
instance (Hashable k, Hashable v) => Hashable (HashMap k v) where
223222
hashWithSalt salt hm = go salt (toList' hm [])
224223
where
225224
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
230230

231231
hashLeafWithSalt :: Int -> Leaf k v -> Int
232232
hashLeafWithSalt s (L k v) = s `H.hashWithSalt` k `H.hashWithSalt` v
233233

234234
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)))
236236

237-
leafKey :: Leaf k v -> k
238-
leafKey (L k _) = k
237+
leafValue :: Leaf k v -> v
238+
leafValue (L _ v) = v
239239

240240
-- Helper to get 'Leaf's and 'Collision's as a list.
241241
toList' :: HashMap k v -> [HashMap k v] -> [HashMap k v]

Data/HashSet.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ instance (Data a, Eq a, Hashable a) => Data (HashSet a) where
136136
dataTypeOf _ = hashSetDataType
137137
dataCast1 f = gcast1 f
138138

139-
instance (Hashable a, Ord a) => Hashable (HashSet a) where
139+
instance (Hashable a) => Hashable (HashSet a) where
140140
hashWithSalt salt = hashWithSalt salt . asMap
141141

142142
fromListConstr :: Constr

0 commit comments

Comments
 (0)