Skip to content

Commit 8a69068

Browse files
committed
Replace unionWithKey.goDifferentHash with two'
Closes #468.
1 parent f1a4d3f commit 8a69068

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

Data/HashMap/Internal.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ module Data.HashMap.Internal
126126
, nextShift
127127
, sparseIndex
128128
, two
129+
, two'
129130
, unionArrayBy
130131
, updateFullArray
131132
, updateFullArrayM
@@ -1619,16 +1620,16 @@ unionWithKey f = go 0
16191620
| h1 == h2 = if k1 == k2
16201621
then Leaf h1 (L k1 (f k1 v1 v2))
16211622
else collision h1 l1 l2
1622-
| otherwise = goDifferentHash s h1 h2 t1 t2
1623+
| otherwise = runST (two' s h1 t1 h2 t2)
16231624
go s t1@(Leaf h1 (L k1 v1)) t2@(Collision h2 ls2)
16241625
| h1 == h2 = Collision h1 (updateOrSnocWithKey (\k a b -> (# f k a b #)) k1 v1 ls2)
1625-
| otherwise = goDifferentHash s h1 h2 t1 t2
1626+
| otherwise = runST (two' s h1 t1 h2 t2)
16261627
go s t1@(Collision h1 ls1) t2@(Leaf h2 (L k2 v2))
16271628
| h1 == h2 = Collision h1 (updateOrSnocWithKey (\k a b -> (# f k b a #)) k2 v2 ls1)
1628-
| otherwise = goDifferentHash s h1 h2 t1 t2
1629+
| otherwise = runST (two' s h1 t1 h2 t2)
16291630
go s t1@(Collision h1 ls1) t2@(Collision h2 ls2)
16301631
| h1 == h2 = Collision h1 (updateOrConcatWithKey (\k a b -> (# f k a b #)) ls1 ls2)
1631-
| otherwise = goDifferentHash s h1 h2 t1 t2
1632+
| otherwise = runST (two' s h1 t1 h2 t2)
16321633
-- branch vs. branch
16331634
go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
16341635
let b' = b1 .|. b2
@@ -1681,14 +1682,6 @@ unionWithKey f = go 0
16811682
leafHashCode (Leaf h _) = h
16821683
leafHashCode (Collision h _) = h
16831684
leafHashCode _ = error "leafHashCode"
1684-
1685-
goDifferentHash s h1 h2 t1 t2
1686-
| m1 == m2 = BitmapIndexed m1 (A.singleton $! goDifferentHash (nextShift s) h1 h2 t1 t2)
1687-
| m1 < m2 = BitmapIndexed (m1 .|. m2) (A.pair t1 t2)
1688-
| otherwise = BitmapIndexed (m1 .|. m2) (A.pair t2 t1)
1689-
where
1690-
m1 = mask h1 s
1691-
m2 = mask h2 s
16921685
{-# INLINE unionWithKey #-}
16931686

16941687
-- | Strict in the result of @f@.

Data/HashMap/Internal/Strict.hs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,16 @@ unionWithKey f = go 0
465465
| h1 == h2 = if k1 == k2
466466
then leaf h1 k1 (f k1 v1 v2)
467467
else HM.collision h1 l1 l2
468-
| otherwise = goDifferentHash s h1 h2 t1 t2
468+
| otherwise = runST (HM.two' s h1 t1 h2 t2)
469469
go s t1@(Leaf h1 (L k1 v1)) t2@(Collision h2 ls2)
470470
| h1 == h2 = Collision h1 (updateOrSnocWithKey f k1 v1 ls2)
471-
| otherwise = goDifferentHash s h1 h2 t1 t2
471+
| otherwise = runST (HM.two' s h1 t1 h2 t2)
472472
go s t1@(Collision h1 ls1) t2@(Leaf h2 (L k2 v2))
473473
| h1 == h2 = Collision h1 (updateOrSnocWithKey (flip . f) k2 v2 ls1)
474-
| otherwise = goDifferentHash s h1 h2 t1 t2
474+
| otherwise = runST (HM.two' s h1 t1 h2 t2)
475475
go s t1@(Collision h1 ls1) t2@(Collision h2 ls2)
476476
| h1 == h2 = Collision h1 (HM.updateOrConcatWithKey (\k a b -> let !v = f k a b in (# v #)) ls1 ls2)
477-
| otherwise = goDifferentHash s h1 h2 t1 t2
477+
| otherwise = runST (HM.two' s h1 t1 h2 t2)
478478
-- branch vs. branch
479479
go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
480480
let b' = b1 .|. b2
@@ -527,14 +527,6 @@ unionWithKey f = go 0
527527
leafHashCode (Leaf h _) = h
528528
leafHashCode (Collision h _) = h
529529
leafHashCode _ = error "leafHashCode"
530-
531-
goDifferentHash s h1 h2 t1 t2
532-
| m1 == m2 = BitmapIndexed m1 (A.singleton $! goDifferentHash (nextShift s) h1 h2 t1 t2)
533-
| m1 < m2 = BitmapIndexed (m1 .|. m2) (A.pair t1 t2)
534-
| otherwise = BitmapIndexed (m1 .|. m2) (A.pair t2 t1)
535-
where
536-
m1 = mask h1 s
537-
m2 = mask h2 s
538530
{-# INLINE unionWithKey #-}
539531

540532
------------------------------------------------------------------------

0 commit comments

Comments
 (0)