8
8
9
9
module Data.HashMap.Base
10
10
(
11
- HashMap (.. )
11
+ HashMapInner (.. )
12
12
, HashMapW (.. )
13
13
, Leaf (.. )
14
14
@@ -154,21 +154,21 @@ 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 HashMap k v
157
+ data HashMapInner k v
158
158
= Empty
159
- | BitmapIndexed ! Bitmap ! (A. Array (HashMap k v ))
159
+ | BitmapIndexed ! Bitmap ! (A. Array (HashMapInner k v ))
160
160
| Leaf ! Hash ! (Leaf k v )
161
- | Full ! (A. Array (HashMap k v ))
161
+ | Full ! (A. Array (HashMapInner k v ))
162
162
| Collision ! Hash ! (A. Array (Leaf k v ))
163
163
deriving (Show , Typeable )
164
164
165
- type role HashMap nominal representational
165
+ type role HashMapInner nominal representational
166
166
167
167
-- | WIP. This will become the user-facing 'HashMap' after this PR is
168
168
-- finalized.
169
- data HashMapW k v = HashMapW {- # UNPACK #-} !Int ! (HashMap k v )
169
+ data HashMapW k v = HashMapW {- # UNPACK #-} !Int ! (HashMapInner k v )
170
170
171
- instance (NFData k , NFData v ) => NFData (HashMap k v ) where
171
+ instance (NFData k , NFData v ) => NFData (HashMapInner k v ) where
172
172
rnf Empty = ()
173
173
rnf (BitmapIndexed _ ary) = rnf ary
174
174
rnf (Leaf _ l) = rnf l
@@ -254,7 +254,7 @@ instance Traversable (HashMapW k) where
254
254
traverse f = traverseWithKey (const f)
255
255
256
256
#if MIN_VERSION_base(4,9,0)
257
- instance Eq2 HashMap where
257
+ instance Eq2 HashMapInner where
258
258
liftEq2 = equal
259
259
260
260
instance Eq k => Eq1 (HashMapW k ) where
@@ -349,7 +349,7 @@ equalKeys eq (HashMapW s1 t1) (HashMapW s2 t2)
349
349
instance H. Hashable2 HashMapW where
350
350
liftHashWithSalt2 hk hv salt (HashMapW _ hm) = go salt (toList' hm [] )
351
351
where
352
- -- go :: Int -> [HashMap k v] -> Int
352
+ -- go :: Int -> [HashMapInner k v] -> Int
353
353
go s [] = s
354
354
go s (Leaf _ l : tl)
355
355
= s `hashLeafWithSalt` l `go` tl
@@ -376,7 +376,7 @@ instance (Hashable k) => H.Hashable1 (HashMapW k) where
376
376
instance (Hashable k , Hashable v ) => Hashable (HashMapW k v ) where
377
377
hashWithSalt salt (HashMapW _ hm) = go salt (toList' hm [] )
378
378
where
379
- go :: Int -> [HashMap k v ] -> Int
379
+ go :: Int -> [HashMapInner k v ] -> Int
380
380
go s [] = s
381
381
go s (Leaf _ l : tl)
382
382
= s `hashLeafWithSalt` l `go` tl
@@ -397,15 +397,15 @@ instance (Hashable k, Hashable v) => Hashable (HashMapW k v) where
397
397
arrayHashesSorted s = L. sort . L. map (hashLeafWithSalt s) . A. toList
398
398
399
399
-- 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 ]
401
401
toList' (BitmapIndexed _ ary) a = A. foldr toList' a ary
402
402
toList' (Full ary) a = A. foldr toList' a ary
403
403
toList' l@ (Leaf _ _) a = l : a
404
404
toList' c@ (Collision _ _) a = c : a
405
405
toList' Empty a = a
406
406
407
407
-- Helper function to detect 'Leaf's and 'Collision's.
408
- isLeafOrCollision :: HashMap k v -> Bool
408
+ isLeafOrCollision :: HashMapInner k v -> Bool
409
409
isLeafOrCollision (Leaf _ _) = True
410
410
isLeafOrCollision (Collision _ _) = True
411
411
isLeafOrCollision _ = False
@@ -482,7 +482,7 @@ lookupDefault def k t = case lookup k t of
482
482
infixl 9 !
483
483
484
484
-- | 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
486
486
collision h e1 e2 =
487
487
let v = A. run $ do mary <- A. new 2 e1
488
488
A. write mary 1 e2
@@ -491,7 +491,7 @@ collision h e1 e2 =
491
491
{-# INLINE collision #-}
492
492
493
493
-- | 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
495
495
bitmapIndexedOrFull b ary
496
496
| b == fullNodeMask = Full ary
497
497
| otherwise = BitmapIndexed b ary
@@ -510,7 +510,7 @@ insert k0 v0 (HashMapW sz m0) =
510
510
-- key in this map. If this map previously contained a mapping for
511
511
-- the key, the old value is replaced. Returns a tuple containing the
512
512
-- 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 )
514
514
insertInternal k0 v0 m0 = go h0 k0 v0 0 m0
515
515
where
516
516
h0 = hash k0
@@ -559,7 +559,7 @@ unsafeInsert k0 v0 (HashMapW sz m0) =
559
559
560
560
-- | In-place update version of insert. Returns a tuple with the
561
561
-- 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 )
563
563
unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
564
564
where
565
565
h0 = hash k0
@@ -598,7 +598,7 @@ unsafeInsertInternal k0 v0 m0 = runST (go h0 k0 v0 0 m0)
598
598
{-# INLINABLE unsafeInsertInternal #-}
599
599
600
600
-- | 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 )
602
602
two = go
603
603
where
604
604
go s h1 k1 v1 h2 k2 v2
@@ -643,8 +643,8 @@ insertWith f k0 v0 (HashMapW sz m0) =
643
643
--
644
644
-- > insertWithInternal f k v map
645
645
-- > 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 )
648
648
insertWithInternal f k0 v0 m0 = go h0 k0 v0 0 m0
649
649
where
650
650
h0 = hash k0
@@ -691,12 +691,12 @@ unsafeInsertWith f k0 v0 (HashMapW sz m0) =
691
691
692
692
-- | In-place update version of insertWithInternal
693
693
unsafeInsertWithInternal :: 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 )
696
696
unsafeInsertWithInternal f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
697
697
where
698
698
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 )
700
700
go ! h ! k x ! _ Empty = return $! (1 , Leaf h (L k x))
701
701
go h k x s (Leaf hy l@ (L ky y))
702
702
| hy == h = if ky == k
@@ -742,7 +742,7 @@ delete k0 (HashMapW sz m0) =
742
742
-- | /O(log n)/ Remove the mapping for the specified key from this map
743
743
-- if present. Returns a tuple with the hashmap's change in size and the
744
744
-- 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 )
746
746
deleteInternal k0 m0 = go h0 k0 0 m0
747
747
where
748
748
h0 = hash k0
@@ -885,17 +885,17 @@ unionWithKey f (HashMapW sz m) hw =
885
885
unionWithKeyInternal
886
886
:: forall k v . (Eq k , Hashable k )
887
887
=> (k -> v -> v -> v )
888
- -> HashMap k v
888
+ -> HashMapInner k v
889
889
-> HashMapW k v
890
- -> (Int , HashMap k v )
890
+ -> (Int , HashMapInner k v )
891
891
unionWithKeyInternal f hm1 (HashMapW siz hm2) = go 0 siz hm1 hm2
892
892
where
893
893
go :: Int -- ^ Bitmask accumulator
894
894
-> Int -- ^ Size accumulator.
895
895
-- 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 )
899
899
-- empty vs. anything
900
900
go ! _ ! sz t1 Empty = (sz, t1)
901
901
go _ ! sz Empty t2 = (sz, t2)
@@ -1107,7 +1107,7 @@ map f = mapWithKey (const f)
1107
1107
{-# INLINE map #-}
1108
1108
1109
1109
-- TODO: We should be able to use mutation to create the new
1110
- -- 'HashMap '.
1110
+ -- 'HashMapInner '.
1111
1111
1112
1112
-- | /O(n)/ Transform this map by accumulating an Applicative result
1113
1113
-- from every value.
@@ -1281,10 +1281,10 @@ filterWithKey pred (HashMapW _ m) = HashMapW size' m'
1281
1281
-- allowing the former and latter to reuse terms.
1282
1282
-- Returns the result hashmap's size, and the hashmap itself.
1283
1283
filterMapAuxInternal :: forall k v1 v2
1284
- . (HashMap k v1 -> Maybe (HashMap k v2 ))
1284
+ . (HashMapInner k v1 -> Maybe (HashMapInner k v2 ))
1285
1285
-> (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 )
1288
1288
filterMapAuxInternal onLeaf onColl = go 0
1289
1289
where
1290
1290
go ! sz Empty = (sz, Empty )
@@ -1301,9 +1301,9 @@ filterMapAuxInternal onLeaf onColl = go 0
1301
1301
mary <- A. new_ n
1302
1302
step ary0 mary b0 0 0 1 n sz
1303
1303
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 )
1305
1305
-> Bitmap -> Int -> Int -> Bitmap -> Int -> Int
1306
- -> ST s (Int , HashMap k v2 )
1306
+ -> ST s (Int , HashMapInner k v2 )
1307
1307
step ! ary ! mary ! b i ! j ! bi n ! siz
1308
1308
| i >= n = case j of
1309
1309
0 -> return (siz, Empty )
@@ -1333,7 +1333,7 @@ filterMapAuxInternal onLeaf onColl = go 0
1333
1333
where
1334
1334
step :: A. Array (Leaf k v1 ) -> A. MArray s (Leaf k v2 )
1335
1335
-> Int -> Int -> Int -> Int
1336
- -> ST s (Int , HashMap k v2 )
1336
+ -> ST s (Int , HashMapInner k v2 )
1337
1337
step ! ary ! mary i ! j n ! sz
1338
1338
| i >= n = case j of
1339
1339
0 -> return (sz, Empty )
0 commit comments