8
8
9
9
module Data.HashMap.Base
10
10
(
11
- HashMapInner (.. )
11
+ Tree (.. )
12
12
, HashMap (.. )
13
13
, Leaf (.. )
14
14
@@ -154,22 +154,22 @@ instance (NFData k, NFData v) => NFData (Leaf k v) where
154
154
155
155
-- | A map from keys to values. A map cannot contain duplicate keys;
156
156
-- each key can map to at most one value.
157
- data HashMapInner k v
157
+ data Tree k v
158
158
= Empty
159
- | BitmapIndexed ! Bitmap ! (A. Array (HashMapInner k v ))
159
+ | BitmapIndexed ! Bitmap ! (A. Array (Tree k v ))
160
160
| Leaf ! Hash ! (Leaf k v )
161
- | Full ! (A. Array (HashMapInner k v ))
161
+ | Full ! (A. Array (Tree k v ))
162
162
| Collision ! Hash ! (A. Array (Leaf k v ))
163
163
deriving Typeable
164
164
165
- type role HashMapInner nominal representational
165
+ type role Tree nominal representational
166
166
167
- -- | A wrapper over 'HashMapInner '. The 'Int' field represent the hashmap's
167
+ -- | A wrapper over 'Tree '. The 'Int' field represent the hashmap's
168
168
-- size.
169
- data HashMap k v = HashMap {- # UNPACK #-} !Int ! (HashMapInner k v )
169
+ data HashMap k v = HashMap {- # UNPACK #-} !Int ! (Tree k v )
170
170
deriving Typeable
171
171
172
- instance (NFData k , NFData v ) => NFData (HashMapInner k v ) where
172
+ instance (NFData k , NFData v ) => NFData (Tree k v ) where
173
173
rnf Empty = ()
174
174
rnf (BitmapIndexed _ ary) = rnf ary
175
175
rnf (Leaf _ l) = rnf l
@@ -350,7 +350,7 @@ equalKeys eq (HashMap s1 t1) (HashMap s2 t2)
350
350
instance H. Hashable2 HashMap where
351
351
liftHashWithSalt2 hk hv salt (HashMap _ hm) = go salt (toList' hm [] )
352
352
where
353
- -- go :: Int -> [HashMapInner k v] -> Int
353
+ -- go :: Int -> [Tree k v] -> Int
354
354
go s [] = s
355
355
go s (Leaf _ l : tl)
356
356
= s `hashLeafWithSalt` l `go` tl
@@ -377,7 +377,7 @@ instance (Hashable k) => H.Hashable1 (HashMap k) where
377
377
instance (Hashable k , Hashable v ) => Hashable (HashMap k v ) where
378
378
hashWithSalt salt (HashMap _ hm) = go salt (toList' hm [] )
379
379
where
380
- go :: Int -> [HashMapInner k v ] -> Int
380
+ go :: Int -> [Tree k v ] -> Int
381
381
go s [] = s
382
382
go s (Leaf _ l : tl)
383
383
= s `hashLeafWithSalt` l `go` tl
@@ -398,15 +398,15 @@ instance (Hashable k, Hashable v) => Hashable (HashMap k v) where
398
398
arrayHashesSorted s = L. sort . L. map (hashLeafWithSalt s) . A. toList
399
399
400
400
-- 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 ]
402
402
toList' (BitmapIndexed _ ary) a = A. foldr toList' a ary
403
403
toList' (Full ary) a = A. foldr toList' a ary
404
404
toList' l@ (Leaf _ _) a = l : a
405
405
toList' c@ (Collision _ _) a = c : a
406
406
toList' Empty a = a
407
407
408
408
-- Helper function to detect 'Leaf's and 'Collision's.
409
- isLeafOrCollision :: HashMapInner k v -> Bool
409
+ isLeafOrCollision :: Tree k v -> Bool
410
410
isLeafOrCollision (Leaf _ _) = True
411
411
isLeafOrCollision (Collision _ _) = True
412
412
isLeafOrCollision _ = False
@@ -483,7 +483,7 @@ lookupDefault def k t = case lookup k t of
483
483
infixl 9 !
484
484
485
485
-- | 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
487
487
collision h e1 e2 =
488
488
let v = A. run $ do mary <- A. new 2 e1
489
489
A. write mary 1 e2
@@ -492,7 +492,7 @@ collision h e1 e2 =
492
492
{-# INLINE collision #-}
493
493
494
494
-- | 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
496
496
bitmapIndexedOrFull b ary
497
497
| b == fullNodeMask = Full ary
498
498
| otherwise = BitmapIndexed b ary
@@ -511,7 +511,7 @@ insert k0 v0 (HashMap sz m0) =
511
511
-- key in this map. If this map previously contained a mapping for
512
512
-- the key, the old value is replaced. Returns a tuple containing the
513
513
-- 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 )
515
515
insertInternal k0 v0 m0 = go h0 k0 v0 0 m0
516
516
where
517
517
h0 = hash k0
@@ -560,7 +560,7 @@ unsafeInsert k0 v0 (HashMap sz m0) =
560
560
561
561
-- | In-place update version of insert. Returns a tuple with the
562
562
-- 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 )
564
564
unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
565
565
where
566
566
h0 = hash k0
@@ -599,7 +599,7 @@ unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
599
599
{-# INLINABLE unsafeInsertInternal #-}
600
600
601
601
-- | 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 )
603
603
two = go
604
604
where
605
605
go s h1 k1 v1 h2 k2 v2
@@ -644,8 +644,8 @@ insertWith f k0 v0 (HashMap sz m0) =
644
644
--
645
645
-- > insertWithInternal f k v map
646
646
-- > 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 )
649
649
insertWithInternal f k0 v0 m0 = go h0 k0 v0 0 m0
650
650
where
651
651
h0 = hash k0
@@ -692,12 +692,12 @@ unsafeInsertWith f k0 v0 (HashMap sz m0) =
692
692
693
693
-- | In-place update version of insertWithInternal
694
694
unsafeInsertWithInternal :: 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 )
697
697
unsafeInsertWithInternal f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
698
698
where
699
699
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 )
701
701
go ! h ! k x ! _ Empty = return $! (1 , Leaf h (L k x))
702
702
go h k x s (Leaf hy l@ (L ky y))
703
703
| hy == h = if ky == k
@@ -743,7 +743,7 @@ delete k0 (HashMap sz m0) =
743
743
-- | /O(log n)/ Remove the mapping for the specified key from this map
744
744
-- if present. Returns a tuple with the hashmap's change in size and the
745
745
-- 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 )
747
747
deleteInternal k0 m0 = go h0 k0 0 m0
748
748
where
749
749
h0 = hash k0
@@ -886,17 +886,17 @@ unionWithKey f (HashMap sz m) hw =
886
886
unionWithKeyInternal
887
887
:: forall k v . (Eq k , Hashable k )
888
888
=> (k -> v -> v -> v )
889
- -> HashMapInner k v
889
+ -> Tree k v
890
890
-> HashMap k v
891
- -> (Int , HashMapInner k v )
891
+ -> (Int , Tree k v )
892
892
unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
893
893
where
894
894
go :: Int -- ^ Bitmask accumulator
895
895
-> Int -- ^ Size accumulator.
896
896
-- 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 )
900
900
-- empty vs. anything
901
901
go ! _ ! sz t1 Empty = (sz, t1)
902
902
go _ ! sz Empty t2 = (sz, t2)
@@ -1110,7 +1110,7 @@ map f = mapWithKey (const f)
1110
1110
{-# INLINE map #-}
1111
1111
1112
1112
-- TODO: We should be able to use mutation to create the new
1113
- -- 'HashMapInner '.
1113
+ -- 'Tree '.
1114
1114
1115
1115
-- | /O(n)/ Transform this map by accumulating an Applicative result
1116
1116
-- from every value.
@@ -1284,10 +1284,10 @@ filterWithKey pred (HashMap _ m) = HashMap size' m'
1284
1284
-- allowing the former and latter to reuse terms.
1285
1285
-- Returns the result hashmap's size, and the hashmap itself.
1286
1286
filterMapAuxInternal :: forall k v1 v2
1287
- . (HashMapInner k v1 -> Maybe (HashMapInner k v2 ))
1287
+ . (Tree k v1 -> Maybe (Tree k v2 ))
1288
1288
-> (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 )
1291
1291
filterMapAuxInternal onLeaf onColl = go 0
1292
1292
where
1293
1293
go ! sz Empty = (sz, Empty )
@@ -1304,9 +1304,9 @@ filterMapAuxInternal onLeaf onColl = go 0
1304
1304
mary <- A. new_ n
1305
1305
step ary0 mary b0 0 0 1 n sz
1306
1306
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 )
1308
1308
-> Bitmap -> Int -> Int -> Bitmap -> Int -> Int
1309
- -> ST s (Int , HashMapInner k v2 )
1309
+ -> ST s (Int , Tree k v2 )
1310
1310
step ! ary ! mary ! b i ! j ! bi n ! siz
1311
1311
| i >= n = case j of
1312
1312
0 -> return (siz, Empty )
@@ -1336,7 +1336,7 @@ filterMapAuxInternal onLeaf onColl = go 0
1336
1336
where
1337
1337
step :: A. Array (Leaf k v1 ) -> A. MArray s (Leaf k v2 )
1338
1338
-> Int -> Int -> Int -> Int
1339
- -> ST s (Int , HashMapInner k v2 )
1339
+ -> ST s (Int , Tree k v2 )
1340
1340
step ! ary ! mary i ! j n ! sz
1341
1341
| i >= n = case j of
1342
1342
0 -> return (sz, Empty )
0 commit comments