Skip to content

Commit b2c38c3

Browse files
committed
Change two to take two HashMap nodes
Context: #468
1 parent 6a0fed1 commit b2c38c3

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

Data/HashMap/Internal.hs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ insert' h0 k0 v0 m0 = go h0 k0 v0 0 m0
812812
then t
813813
else Leaf h (L k x)
814814
else collision h l (L k x)
815-
| otherwise = runST (two s h k x hy t)
815+
| otherwise = runST (two s h (Leaf h (L k x)) hy t)
816816
go h k x s t@(BitmapIndexed b ary)
817817
| b .&. m == 0 =
818818
let !ary' = A.insert ary i $! Leaf h (L k x)
@@ -850,7 +850,7 @@ insertNewKey !h0 !k0 x0 !m0 = go h0 k0 x0 0 m0
850850
go !h !k x !_ Empty = Leaf h (L k x)
851851
go h k x s t@(Leaf hy l)
852852
| hy == h = collision h l (L k x)
853-
| otherwise = runST (two s h k x hy t)
853+
| otherwise = runST (two s h (Leaf h (L k x)) hy t)
854854
go h k x s (BitmapIndexed b ary)
855855
| b .&. m == 0 =
856856
let !ary' = A.insert ary i $! Leaf h (L k x)
@@ -935,7 +935,7 @@ unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
935935
then return t
936936
else return $! Leaf h (L k x)
937937
else return $! collision h l (L k x)
938-
| otherwise = two s h k x hy t
938+
| otherwise = two s h (Leaf h (L k x)) hy t
939939
go h k x s t@(BitmapIndexed b ary)
940940
| b .&. m == 0 = do
941941
ary' <- A.insertM ary i $! Leaf h (L k x)
@@ -958,24 +958,21 @@ unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
958958
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
959959
{-# INLINABLE unsafeInsert #-}
960960

961-
-- | Create a map from two key-value pairs which hashes don't collide. To
962-
-- enhance sharing, the second key-value pair is represented by the hash of its
963-
-- key and a singleton HashMap pairing its key with its value.
961+
-- | Create a map from two key-value pairs which hashes don't collide.
964962
--
965-
-- Note: to avoid silly thunks, this function must be strict in the
966-
-- key. See issue #232. We don't need to force the HashMap argument
963+
-- Note: We don't need to force the HashMap argument
967964
-- because it's already in WHNF (having just been matched) and we
968965
-- just put it directly in an array.
969-
two :: Shift -> Hash -> k -> v -> Hash -> HashMap k v -> ST s (HashMap k v)
966+
two :: Shift -> Hash -> HashMap k v -> Hash -> HashMap k v -> ST s (HashMap k v)
970967
two = go
971968
where
972-
go s h1 k1 v1 h2 t2
969+
go s h1 t1 h2 t2
973970
| bp1 == bp2 = do
974-
st <- go (nextShift s) h1 k1 v1 h2 t2
971+
st <- go (nextShift s) h1 t1 h2 t2
975972
ary <- A.singletonM st
976973
return $ BitmapIndexed bp1 ary
977974
| otherwise = do
978-
mary <- A.new 2 $! Leaf h1 (L k1 v1)
975+
mary <- A.new 2 $! t1
979976
A.write mary idx2 t2
980977
ary <- A.unsafeFreeze mary
981978
return $ BitmapIndexed (bp1 .|. bp2) ary
@@ -1024,7 +1021,7 @@ insertModifying x f k0 m0 = go h0 k0 0 m0
10241021
(# v' #) | ptrEq y v' -> t
10251022
| otherwise -> Leaf h (L k v')
10261023
else collision h l (L k x)
1027-
| otherwise = runST (two s h k x hy t)
1024+
| otherwise = runST (two s h (Leaf h (L k x)) hy t)
10281025
go h k s t@(BitmapIndexed b ary)
10291026
| b .&. m == 0 =
10301027
let ary' = A.insert ary i $! Leaf h (L k x)
@@ -1091,7 +1088,7 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
10911088
then case f k x y of
10921089
(# v #) -> return $! Leaf h (L k v)
10931090
else return $! collision h l (L k x)
1094-
| otherwise = two s h k x hy t
1091+
| otherwise = two s h (Leaf h (L k x)) hy t
10951092
go h k x s t@(BitmapIndexed b ary)
10961093
| b .&. m == 0 = do
10971094
ary' <- A.insertM ary i $! Leaf h (L k x)

Data/HashMap/Internal/Strict.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ insertWith f k0 v0 m0 = go h0 k0 v0 0 m0
196196
| hy == h = if ky == k
197197
then leaf h k (f x y)
198198
else x `seq` HM.collision h l (L k x)
199-
| otherwise = x `seq` runST (HM.two s h k x hy t)
199+
| otherwise = x `seq` runST (HM.two s h (Leaf h (L k x)) hy t)
200200
go h k x s (BitmapIndexed b ary)
201201
| b .&. m == 0 =
202202
let ary' = A.insert ary i $! leaf h k x
@@ -237,7 +237,7 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
237237
else do
238238
let l' = x `seq` L k x
239239
return $! HM.collision h l l'
240-
| otherwise = x `seq` HM.two s h k x hy t
240+
| otherwise = x `seq` HM.two s h (Leaf h (L k x)) hy t
241241
go h k x s t@(BitmapIndexed b ary)
242242
| b .&. m == 0 = do
243243
ary' <- A.insertM ary i $! leaf h k x

0 commit comments

Comments
 (0)