88
99module Data.HashMap.Base
1010 (
11- HashMap (.. )
11+ HashMapInner (.. )
1212 , HashMapW (.. )
1313 , Leaf (.. )
1414
@@ -154,21 +154,21 @@ instance (NFData k, NFData v) => NFData (Leaf k v) where
154154
155155-- | A map from keys to values. A map cannot contain duplicate keys;
156156-- each key can map to at most one value.
157- data HashMap k v
157+ data HashMapInner k v
158158 = Empty
159- | BitmapIndexed ! Bitmap ! (A. Array (HashMap k v ))
159+ | BitmapIndexed ! Bitmap ! (A. Array (HashMapInner k v ))
160160 | Leaf ! Hash ! (Leaf k v )
161- | Full ! (A. Array (HashMap k v ))
161+ | Full ! (A. Array (HashMapInner k v ))
162162 | Collision ! Hash ! (A. Array (Leaf k v ))
163163 deriving (Show , Typeable )
164164
165- type role HashMap nominal representational
165+ type role HashMapInner nominal representational
166166
167167-- | WIP. This will become the user-facing 'HashMap' after this PR is
168168-- finalized.
169- data HashMapW k v = HashMapW {- # UNPACK #-} !Int ! (HashMap k v )
169+ data HashMapW k v = HashMapW {- # UNPACK #-} !Int ! (HashMapInner k v )
170170
171- instance (NFData k , NFData v ) => NFData (HashMap k v ) where
171+ instance (NFData k , NFData v ) => NFData (HashMapInner k v ) where
172172 rnf Empty = ()
173173 rnf (BitmapIndexed _ ary) = rnf ary
174174 rnf (Leaf _ l) = rnf l
@@ -254,7 +254,7 @@ instance Traversable (HashMapW k) where
254254 traverse f = traverseWithKey (const f)
255255
256256#if MIN_VERSION_base(4,9,0)
257- instance Eq2 HashMap where
257+ instance Eq2 HashMapInner where
258258 liftEq2 = equal
259259
260260instance Eq k => Eq1 (HashMapW k ) where
@@ -349,7 +349,7 @@ equalKeys eq (HashMapW s1 t1) (HashMapW s2 t2)
349349instance H. Hashable2 HashMapW where
350350 liftHashWithSalt2 hk hv salt (HashMapW _ hm) = go salt (toList' hm [] )
351351 where
352- -- go :: Int -> [HashMap k v] -> Int
352+ -- go :: Int -> [HashMapInner k v] -> Int
353353 go s [] = s
354354 go s (Leaf _ l : tl)
355355 = s `hashLeafWithSalt` l `go` tl
@@ -376,7 +376,7 @@ instance (Hashable k) => H.Hashable1 (HashMapW k) where
376376instance (Hashable k , Hashable v ) => Hashable (HashMapW k v ) where
377377 hashWithSalt salt (HashMapW _ hm) = go salt (toList' hm [] )
378378 where
379- go :: Int -> [HashMap k v ] -> Int
379+ go :: Int -> [HashMapInner k v ] -> Int
380380 go s [] = s
381381 go s (Leaf _ l : tl)
382382 = s `hashLeafWithSalt` l `go` tl
@@ -397,15 +397,15 @@ instance (Hashable k, Hashable v) => Hashable (HashMapW k v) where
397397 arrayHashesSorted s = L. sort . L. map (hashLeafWithSalt s) . A. toList
398398
399399 -- Helper to get 'Leaf's and 'Collision's as a list.
400- toList' :: HashMap k v -> [HashMap k v ] -> [HashMap k v ]
400+ toList' :: HashMapInner k v -> [HashMapInner k v ] -> [HashMapInner k v ]
401401toList' (BitmapIndexed _ ary) a = A. foldr toList' a ary
402402toList' (Full ary) a = A. foldr toList' a ary
403403toList' l@ (Leaf _ _) a = l : a
404404toList' c@ (Collision _ _) a = c : a
405405toList' Empty a = a
406406
407407-- Helper function to detect 'Leaf's and 'Collision's.
408- isLeafOrCollision :: HashMap k v -> Bool
408+ isLeafOrCollision :: HashMapInner k v -> Bool
409409isLeafOrCollision (Leaf _ _) = True
410410isLeafOrCollision (Collision _ _) = True
411411isLeafOrCollision _ = False
@@ -482,7 +482,7 @@ lookupDefault def k t = case lookup k t of
482482infixl 9 !
483483
484484-- | Create a 'Collision' value with two 'Leaf' values.
485- collision :: Hash -> Leaf k v -> Leaf k v -> HashMap k v
485+ collision :: Hash -> Leaf k v -> Leaf k v -> HashMapInner k v
486486collision h e1 e2 =
487487 let v = A. run $ do mary <- A. new 2 e1
488488 A. write mary 1 e2
@@ -491,7 +491,7 @@ collision h e1 e2 =
491491{-# INLINE collision #-}
492492
493493-- | Create a 'BitmapIndexed' or 'Full' node.
494- bitmapIndexedOrFull :: Bitmap -> A. Array (HashMap k v ) -> HashMap k v
494+ bitmapIndexedOrFull :: Bitmap -> A. Array (HashMapInner k v ) -> HashMapInner k v
495495bitmapIndexedOrFull b ary
496496 | b == fullNodeMask = Full ary
497497 | otherwise = BitmapIndexed b ary
@@ -510,7 +510,7 @@ insert k0 v0 (HashMapW sz m0) =
510510-- key in this map. If this map previously contained a mapping for
511511-- the key, the old value is replaced. Returns a tuple containing the
512512-- hashmap's change in size, and the hashmap after the insertion.
513- insertInternal :: (Eq k , Hashable k ) => k -> v -> HashMap k v -> (Int , HashMap k v )
513+ insertInternal :: (Eq k , Hashable k ) => k -> v -> HashMapInner k v -> (Int , HashMapInner k v )
514514insertInternal k0 v0 m0 = go h0 k0 v0 0 m0
515515 where
516516 h0 = hash k0
@@ -559,7 +559,7 @@ unsafeInsert k0 v0 (HashMapW sz m0) =
559559
560560-- | In-place update version of insert. Returns a tuple with the
561561-- HashMap's change in size and the hashmap itself.
562- unsafeInsertInternal :: (Eq k , Hashable k ) => k -> v -> HashMap k v -> (Int , HashMap k v )
562+ unsafeInsertInternal :: (Eq k , Hashable k ) => k -> v -> HashMapInner k v -> (Int , HashMapInner k v )
563563unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
564564 where
565565 h0 = hash k0
@@ -598,7 +598,7 @@ unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
598598{-# INLINABLE unsafeInsertInternal #-}
599599
600600-- | Create a map from two key-value pairs which hashes don't collide.
601- two :: Shift -> Hash -> k -> v -> Hash -> k -> v -> ST s (HashMap k v )
601+ two :: Shift -> Hash -> k -> v -> Hash -> k -> v -> ST s (HashMapInner k v )
602602two = go
603603 where
604604 go s h1 k1 v1 h2 k2 v2
@@ -643,8 +643,8 @@ insertWith f k0 v0 (HashMapW sz m0) =
643643--
644644-- > insertWithInternal f k v map
645645-- > where f new old = new + old
646- insertWithInternal :: (Eq k , Hashable k ) => (v -> v -> v ) -> k -> v -> HashMap k v
647- -> (Int , HashMap k v )
646+ insertWithInternal :: (Eq k , Hashable k ) => (v -> v -> v ) -> k -> v -> HashMapInner k v
647+ -> (Int , HashMapInner k v )
648648insertWithInternal f k0 v0 m0 = go h0 k0 v0 0 m0
649649 where
650650 h0 = hash k0
@@ -691,12 +691,12 @@ unsafeInsertWith f k0 v0 (HashMapW sz m0) =
691691
692692-- | In-place update version of insertWithInternal
693693unsafeInsertWithInternal :: forall k v . (Eq k , Hashable k )
694- => (v -> v -> v ) -> k -> v -> HashMap k v
695- -> (Int , HashMap k v )
694+ => (v -> v -> v ) -> k -> v -> HashMapInner k v
695+ -> (Int , HashMapInner k v )
696696unsafeInsertWithInternal f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
697697 where
698698 h0 = hash k0
699- go :: Hash -> k -> v -> Shift -> HashMap k v -> ST s (Int , HashMap k v )
699+ go :: Hash -> k -> v -> Shift -> HashMapInner k v -> ST s (Int , HashMapInner k v )
700700 go ! h ! k x ! _ Empty = return $! (1 , Leaf h (L k x))
701701 go h k x s (Leaf hy l@ (L ky y))
702702 | hy == h = if ky == k
@@ -742,7 +742,7 @@ delete k0 (HashMapW sz m0) =
742742-- | /O(log n)/ Remove the mapping for the specified key from this map
743743-- if present. Returns a tuple with the hashmap's change in size and the
744744-- hashmap after the deletion.
745- deleteInternal :: (Eq k , Hashable k ) => k -> HashMap k v -> (Int , HashMap k v )
745+ deleteInternal :: (Eq k , Hashable k ) => k -> HashMapInner k v -> (Int , HashMapInner k v )
746746deleteInternal k0 m0 = go h0 k0 0 m0
747747 where
748748 h0 = hash k0
@@ -885,17 +885,17 @@ unionWithKey f (HashMapW sz m) hw =
885885unionWithKeyInternal
886886 :: forall k v . (Eq k , Hashable k )
887887 => (k -> v -> v -> v )
888- -> HashMap k v
888+ -> HashMapInner k v
889889 -> HashMapW k v
890- -> (Int , HashMap k v )
890+ -> (Int , HashMapInner k v )
891891unionWithKeyInternal f hm1 (HashMapW siz hm2) = go 0 siz hm1 hm2
892892 where
893893 go :: Int -- ^ Bitmask accumulator
894894 -> Int -- ^ Size accumulator.
895895 -- Counts down from the second hashmap's size.
896- -> HashMap k v
897- -> HashMap k v
898- -> (Int , HashMap k v )
896+ -> HashMapInner k v
897+ -> HashMapInner k v
898+ -> (Int , HashMapInner k v )
899899 -- empty vs. anything
900900 go ! _ ! sz t1 Empty = (sz, t1)
901901 go _ ! sz Empty t2 = (sz, t2)
@@ -1107,7 +1107,7 @@ map f = mapWithKey (const f)
11071107{-# INLINE map #-}
11081108
11091109-- TODO: We should be able to use mutation to create the new
1110- -- 'HashMap '.
1110+ -- 'HashMapInner '.
11111111
11121112-- | /O(n)/ Transform this map by accumulating an Applicative result
11131113-- from every value.
@@ -1281,10 +1281,10 @@ filterWithKey pred (HashMapW _ m) = HashMapW size' m'
12811281-- allowing the former and latter to reuse terms.
12821282-- Returns the result hashmap's size, and the hashmap itself.
12831283filterMapAuxInternal :: forall k v1 v2
1284- . (HashMap k v1 -> Maybe (HashMap k v2 ))
1284+ . (HashMapInner k v1 -> Maybe (HashMapInner k v2 ))
12851285 -> (Leaf k v1 -> Maybe (Leaf k v2 ))
1286- -> HashMap k v1
1287- -> (Int , HashMap k v2 )
1286+ -> HashMapInner k v1
1287+ -> (Int , HashMapInner k v2 )
12881288filterMapAuxInternal onLeaf onColl = go 0
12891289 where
12901290 go ! sz Empty = (sz, Empty )
@@ -1301,9 +1301,9 @@ filterMapAuxInternal onLeaf onColl = go 0
13011301 mary <- A. new_ n
13021302 step ary0 mary b0 0 0 1 n sz
13031303 where
1304- step :: A. Array (HashMap k v1 ) -> A. MArray s (HashMap k v2 )
1304+ step :: A. Array (HashMapInner k v1 ) -> A. MArray s (HashMapInner k v2 )
13051305 -> Bitmap -> Int -> Int -> Bitmap -> Int -> Int
1306- -> ST s (Int , HashMap k v2 )
1306+ -> ST s (Int , HashMapInner k v2 )
13071307 step ! ary ! mary ! b i ! j ! bi n ! siz
13081308 | i >= n = case j of
13091309 0 -> return (siz, Empty )
@@ -1333,7 +1333,7 @@ filterMapAuxInternal onLeaf onColl = go 0
13331333 where
13341334 step :: A. Array (Leaf k v1 ) -> A. MArray s (Leaf k v2 )
13351335 -> Int -> Int -> Int -> Int
1336- -> ST s (Int , HashMap k v2 )
1336+ -> ST s (Int , HashMapInner k v2 )
13371337 step ! ary ! mary i ! j n ! sz
13381338 | i >= n = case j of
13391339 0 -> return (sz, Empty )
0 commit comments