88
99module Data.HashMap.Base
1010 (
11- HashMapInner (.. )
11+ Tree (.. )
1212 , HashMap (.. )
1313 , Leaf (.. )
1414
@@ -154,22 +154,22 @@ 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 HashMapInner k v
157+ data Tree k v
158158 = Empty
159- | BitmapIndexed ! Bitmap ! (A. Array (HashMapInner k v ))
159+ | BitmapIndexed ! Bitmap ! (A. Array (Tree k v ))
160160 | Leaf ! Hash ! (Leaf k v )
161- | Full ! (A. Array (HashMapInner k v ))
161+ | Full ! (A. Array (Tree k v ))
162162 | Collision ! Hash ! (A. Array (Leaf k v ))
163163 deriving Typeable
164164
165- type role HashMapInner nominal representational
165+ type role Tree nominal representational
166166
167- -- | A wrapper over 'HashMapInner '. The 'Int' field represent the hashmap's
167+ -- | A wrapper over 'Tree '. The 'Int' field represent the hashmap's
168168-- size.
169- data HashMap k v = HashMap {- # UNPACK #-} !Int ! (HashMapInner k v )
169+ data HashMap k v = HashMap {- # UNPACK #-} !Int ! (Tree k v )
170170 deriving Typeable
171171
172- instance (NFData k , NFData v ) => NFData (HashMapInner k v ) where
172+ instance (NFData k , NFData v ) => NFData (Tree k v ) where
173173 rnf Empty = ()
174174 rnf (BitmapIndexed _ ary) = rnf ary
175175 rnf (Leaf _ l) = rnf l
@@ -350,7 +350,7 @@ equalKeys eq (HashMap s1 t1) (HashMap s2 t2)
350350instance H. Hashable2 HashMap where
351351 liftHashWithSalt2 hk hv salt (HashMap _ hm) = go salt (toList' hm [] )
352352 where
353- -- go :: Int -> [HashMapInner k v] -> Int
353+ -- go :: Int -> [Tree k v] -> Int
354354 go s [] = s
355355 go s (Leaf _ l : tl)
356356 = s `hashLeafWithSalt` l `go` tl
@@ -377,7 +377,7 @@ instance (Hashable k) => H.Hashable1 (HashMap k) where
377377instance (Hashable k , Hashable v ) => Hashable (HashMap k v ) where
378378 hashWithSalt salt (HashMap _ hm) = go salt (toList' hm [] )
379379 where
380- go :: Int -> [HashMapInner k v ] -> Int
380+ go :: Int -> [Tree k v ] -> Int
381381 go s [] = s
382382 go s (Leaf _ l : tl)
383383 = s `hashLeafWithSalt` l `go` tl
@@ -398,15 +398,15 @@ instance (Hashable k, Hashable v) => Hashable (HashMap k v) where
398398 arrayHashesSorted s = L. sort . L. map (hashLeafWithSalt s) . A. toList
399399
400400 -- Helper to get 'Leaf's and 'Collision's as a list.
401- toList' :: HashMapInner k v -> [HashMapInner k v ] -> [HashMapInner k v ]
401+ toList' :: Tree k v -> [Tree k v ] -> [Tree k v ]
402402toList' (BitmapIndexed _ ary) a = A. foldr toList' a ary
403403toList' (Full ary) a = A. foldr toList' a ary
404404toList' l@ (Leaf _ _) a = l : a
405405toList' c@ (Collision _ _) a = c : a
406406toList' Empty a = a
407407
408408-- Helper function to detect 'Leaf's and 'Collision's.
409- isLeafOrCollision :: HashMapInner k v -> Bool
409+ isLeafOrCollision :: Tree k v -> Bool
410410isLeafOrCollision (Leaf _ _) = True
411411isLeafOrCollision (Collision _ _) = True
412412isLeafOrCollision _ = False
@@ -483,7 +483,7 @@ lookupDefault def k t = case lookup k t of
483483infixl 9 !
484484
485485-- | Create a 'Collision' value with two 'Leaf' values.
486- collision :: Hash -> Leaf k v -> Leaf k v -> HashMapInner k v
486+ collision :: Hash -> Leaf k v -> Leaf k v -> Tree k v
487487collision h e1 e2 =
488488 let v = A. run $ do mary <- A. new 2 e1
489489 A. write mary 1 e2
@@ -492,7 +492,7 @@ collision h e1 e2 =
492492{-# INLINE collision #-}
493493
494494-- | Create a 'BitmapIndexed' or 'Full' node.
495- bitmapIndexedOrFull :: Bitmap -> A. Array (HashMapInner k v ) -> HashMapInner k v
495+ bitmapIndexedOrFull :: Bitmap -> A. Array (Tree k v ) -> Tree k v
496496bitmapIndexedOrFull b ary
497497 | b == fullNodeMask = Full ary
498498 | otherwise = BitmapIndexed b ary
@@ -511,7 +511,7 @@ insert k0 v0 (HashMap sz m0) =
511511-- key in this map. If this map previously contained a mapping for
512512-- the key, the old value is replaced. Returns a tuple containing the
513513-- hashmap's change in size, and the hashmap after the insertion.
514- insertInternal :: (Eq k , Hashable k ) => k -> v -> HashMapInner k v -> (Int , HashMapInner k v )
514+ insertInternal :: (Eq k , Hashable k ) => k -> v -> Tree k v -> (Int , Tree k v )
515515insertInternal k0 v0 m0 = go h0 k0 v0 0 m0
516516 where
517517 h0 = hash k0
@@ -560,7 +560,7 @@ unsafeInsert k0 v0 (HashMap sz m0) =
560560
561561-- | In-place update version of insert. Returns a tuple with the
562562-- HashMap's change in size and the hashmap itself.
563- unsafeInsertInternal :: (Eq k , Hashable k ) => k -> v -> HashMapInner k v -> (Int , HashMapInner k v )
563+ unsafeInsertInternal :: (Eq k , Hashable k ) => k -> v -> Tree k v -> (Int , Tree k v )
564564unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
565565 where
566566 h0 = hash k0
@@ -599,7 +599,7 @@ unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
599599{-# INLINABLE unsafeInsertInternal #-}
600600
601601-- | Create a map from two key-value pairs which hashes don't collide.
602- two :: Shift -> Hash -> k -> v -> Hash -> k -> v -> ST s (HashMapInner k v )
602+ two :: Shift -> Hash -> k -> v -> Hash -> k -> v -> ST s (Tree k v )
603603two = go
604604 where
605605 go s h1 k1 v1 h2 k2 v2
@@ -644,8 +644,8 @@ insertWith f k0 v0 (HashMap sz m0) =
644644--
645645-- > insertWithInternal f k v map
646646-- > where f new old = new + old
647- insertWithInternal :: (Eq k , Hashable k ) => (v -> v -> v ) -> k -> v -> HashMapInner k v
648- -> (Int , HashMapInner k v )
647+ insertWithInternal :: (Eq k , Hashable k ) => (v -> v -> v ) -> k -> v -> Tree k v
648+ -> (Int , Tree k v )
649649insertWithInternal f k0 v0 m0 = go h0 k0 v0 0 m0
650650 where
651651 h0 = hash k0
@@ -692,12 +692,12 @@ unsafeInsertWith f k0 v0 (HashMap sz m0) =
692692
693693-- | In-place update version of insertWithInternal
694694unsafeInsertWithInternal :: forall k v . (Eq k , Hashable k )
695- => (v -> v -> v ) -> k -> v -> HashMapInner k v
696- -> (Int , HashMapInner k v )
695+ => (v -> v -> v ) -> k -> v -> Tree k v
696+ -> (Int , Tree k v )
697697unsafeInsertWithInternal f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
698698 where
699699 h0 = hash k0
700- go :: Hash -> k -> v -> Shift -> HashMapInner k v -> ST s (Int , HashMapInner k v )
700+ go :: Hash -> k -> v -> Shift -> Tree k v -> ST s (Int , Tree k v )
701701 go ! h ! k x ! _ Empty = return $! (1 , Leaf h (L k x))
702702 go h k x s (Leaf hy l@ (L ky y))
703703 | hy == h = if ky == k
@@ -743,7 +743,7 @@ delete k0 (HashMap sz m0) =
743743-- | /O(log n)/ Remove the mapping for the specified key from this map
744744-- if present. Returns a tuple with the hashmap's change in size and the
745745-- hashmap after the deletion.
746- deleteInternal :: (Eq k , Hashable k ) => k -> HashMapInner k v -> (Int , HashMapInner k v )
746+ deleteInternal :: (Eq k , Hashable k ) => k -> Tree k v -> (Int , Tree k v )
747747deleteInternal k0 m0 = go h0 k0 0 m0
748748 where
749749 h0 = hash k0
@@ -886,17 +886,17 @@ unionWithKey f (HashMap sz m) hw =
886886unionWithKeyInternal
887887 :: forall k v . (Eq k , Hashable k )
888888 => (k -> v -> v -> v )
889- -> HashMapInner k v
889+ -> Tree k v
890890 -> HashMap k v
891- -> (Int , HashMapInner k v )
891+ -> (Int , Tree k v )
892892unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
893893 where
894894 go :: Int -- ^ Bitmask accumulator
895895 -> Int -- ^ Size accumulator.
896896 -- Counts down from the second hashmap's size.
897- -> HashMapInner k v
898- -> HashMapInner k v
899- -> (Int , HashMapInner k v )
897+ -> Tree k v
898+ -> Tree k v
899+ -> (Int , Tree k v )
900900 -- empty vs. anything
901901 go ! _ ! sz t1 Empty = (sz, t1)
902902 go _ ! sz Empty t2 = (sz, t2)
@@ -1110,7 +1110,7 @@ map f = mapWithKey (const f)
11101110{-# INLINE map #-}
11111111
11121112-- TODO: We should be able to use mutation to create the new
1113- -- 'HashMapInner '.
1113+ -- 'Tree '.
11141114
11151115-- | /O(n)/ Transform this map by accumulating an Applicative result
11161116-- from every value.
@@ -1284,10 +1284,10 @@ filterWithKey pred (HashMap _ m) = HashMap size' m'
12841284-- allowing the former and latter to reuse terms.
12851285-- Returns the result hashmap's size, and the hashmap itself.
12861286filterMapAuxInternal :: forall k v1 v2
1287- . (HashMapInner k v1 -> Maybe (HashMapInner k v2 ))
1287+ . (Tree k v1 -> Maybe (Tree k v2 ))
12881288 -> (Leaf k v1 -> Maybe (Leaf k v2 ))
1289- -> HashMapInner k v1
1290- -> (Int , HashMapInner k v2 )
1289+ -> Tree k v1
1290+ -> (Int , Tree k v2 )
12911291filterMapAuxInternal onLeaf onColl = go 0
12921292 where
12931293 go ! sz Empty = (sz, Empty )
@@ -1304,9 +1304,9 @@ filterMapAuxInternal onLeaf onColl = go 0
13041304 mary <- A. new_ n
13051305 step ary0 mary b0 0 0 1 n sz
13061306 where
1307- step :: A. Array (HashMapInner k v1 ) -> A. MArray s (HashMapInner k v2 )
1307+ step :: A. Array (Tree k v1 ) -> A. MArray s (Tree k v2 )
13081308 -> Bitmap -> Int -> Int -> Bitmap -> Int -> Int
1309- -> ST s (Int , HashMapInner k v2 )
1309+ -> ST s (Int , Tree k v2 )
13101310 step ! ary ! mary ! b i ! j ! bi n ! siz
13111311 | i >= n = case j of
13121312 0 -> return (siz, Empty )
@@ -1336,7 +1336,7 @@ filterMapAuxInternal onLeaf onColl = go 0
13361336 where
13371337 step :: A. Array (Leaf k v1 ) -> A. MArray s (Leaf k v2 )
13381338 -> Int -> Int -> Int -> Int
1339- -> ST s (Int , HashMapInner k v2 )
1339+ -> ST s (Int , Tree k v2 )
13401340 step ! ary ! mary i ! j n ! sz
13411341 | i >= n = case j of
13421342 0 -> return (sz, Empty )
0 commit comments