@@ -1787,19 +1787,12 @@ intersectionWithKey# f = go 0
17871787 go s (Leaf h1 (L k1 v1)) t2 = lookupCont (\ _ -> Empty ) (\ v _ -> case f k1 v1 v of (# v' # ) -> Leaf h1 $ L k1 v') h1 k1 s t2
17881788 go s t1 (Leaf h2 (L k2 v2)) = lookupCont (\ _ -> Empty ) (\ v _ -> case f k2 v v2 of (# v' # ) -> Leaf h2 $ L k2 v') h2 k2 s t1
17891789 -- collision vs. collision
1790- go _ (Collision h1 ls1) (Collision h2 ls2)
1791- | h1 == h2 = runST $ do
1792- (len, mary) <- intersectionCollisions f ls1 ls2
1793- case len of
1794- 0 -> pure Empty
1795- 1 -> Leaf h1 <$> A. read mary 0
1796- _ -> Collision h1 <$> (A. unsafeFreeze =<< A. shrink mary len)
1797- | otherwise = Empty
1790+ go _ (Collision h1 ls1) (Collision h2 ls2) = intersectionCollisions f h1 h2 ls1 ls2
17981791 -- branch vs. branch
1799- go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) = intersectionArray s b1 b2 ary1 ary2
1800- go s (BitmapIndexed b1 ary1) (Full ary2) = intersectionArray s b1 fullNodeMask ary1 ary2
1801- go s (Full ary1) (BitmapIndexed b2 ary2) = intersectionArray s fullNodeMask b2 ary1 ary2
1802- go s (Full ary1) (Full ary2) = intersectionArray s fullNodeMask fullNodeMask ary1 ary2
1792+ go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) = intersectionArrayBy (go (s + bitsPerSubkey)) b1 b2 ary1 ary2
1793+ go s (BitmapIndexed b1 ary1) (Full ary2) = intersectionArrayBy (go (s + bitsPerSubkey)) b1 fullNodeMask ary1 ary2
1794+ go s (Full ary1) (BitmapIndexed b2 ary2) = intersectionArrayBy (go (s + bitsPerSubkey)) fullNodeMask b2 ary1 ary2
1795+ go s (Full ary1) (Full ary2) = intersectionArrayBy (go (s + bitsPerSubkey)) fullNodeMask fullNodeMask ary1 ary2
18031796 -- collision vs. branch
18041797 go s (BitmapIndexed b1 ary1) t2@ (Collision h2 _ls2)
18051798 | b1 .&. m2 == 0 = Empty
@@ -1819,16 +1812,6 @@ intersectionWithKey# f = go 0
18191812 go s t1@ (Collision h1 _ls1) (Full ary2) = go (s + bitsPerSubkey) t1 (A. index ary2 i)
18201813 where
18211814 i = index h1 s
1822-
1823- intersectionArray s b1 b2 ary1 ary2
1824- -- don't create an array of size zero in intersectionArrayBy
1825- | b1 .&. b2 == 0 = Empty
1826- | otherwise = runST $ do
1827- (b, len, ary) <- intersectionArrayBy (go (s + bitsPerSubkey)) b1 b2 ary1 ary2
1828- case len of
1829- 0 -> pure Empty
1830- 1 -> A. read ary 0
1831- _ -> bitmapIndexedOrFull b <$> (A. unsafeFreeze =<< A. shrink ary len)
18321815{-# INLINE intersectionWithKey# #-}
18331816
18341817intersectionArrayBy ::
@@ -1840,10 +1823,12 @@ intersectionArrayBy ::
18401823 Bitmap ->
18411824 A. Array (HashMap k v1 ) ->
18421825 A. Array (HashMap k v2 ) ->
1843- ST s (Bitmap , Int , A. MArray s (HashMap k v3 ))
1844- intersectionArrayBy f ! b1 ! b2 ! ary1 ! ary2 = do
1826+ HashMap k v3
1827+ intersectionArrayBy f ! b1 ! b2 ! ary1 ! ary2
1828+ | b1 .&. b2 == 0 = Empty
1829+ | otherwise = runST $ do
18451830 mary <- A. new_ $ popCount bIntersect
1846- -- iterate over nonzero bits of b1 .& . b2
1831+ -- iterate over nonzero bits of b1 .| . b2
18471832 let go ! i ! i1 ! i2 ! b ! bFinal
18481833 | b == 0 = pure (i, bFinal)
18491834 | testBit $ b1 .&. b2 = do
@@ -1860,15 +1845,19 @@ intersectionArrayBy f !b1 !b2 !ary1 !ary2 = do
18601845 m = 1 `unsafeShiftL` countTrailingZeros b
18611846 testBit x = x .&. m /= 0
18621847 b' = b .&. complement m
1863- (maryLen, bFinal) <- go 0 0 0 bCombined bIntersect
1864- pure (bFinal, maryLen, mary)
1848+ (len, bFinal) <- go 0 0 0 bCombined bIntersect
1849+ case len of
1850+ 0 -> pure Empty
1851+ 1 -> A. read mary 0
1852+ _ -> bitmapIndexedOrFull bFinal <$> (A. unsafeFreeze =<< A. shrink mary len)
18651853 where
18661854 bCombined = b1 .|. b2
18671855 bIntersect = b1 .&. b2
18681856{-# INLINE intersectionArrayBy #-}
18691857
1870- intersectionCollisions :: Eq k => (k -> v1 -> v2 -> (# v3 # )) -> A. Array (Leaf k v1 ) -> A. Array (Leaf k v2 ) -> ST s (Int , A. MArray s (Leaf k v3 ))
1871- intersectionCollisions f ary1 ary2 = do
1858+ intersectionCollisions :: Eq k => (k -> v1 -> v2 -> (# v3 # )) -> Hash -> Hash -> A. Array (Leaf k v1 ) -> A. Array (Leaf k v2 ) -> HashMap k v3
1859+ intersectionCollisions f h1 h2 ary1 ary2
1860+ | h1 == h2 = runST $ do
18721861 mary2 <- A. thaw ary2 0 $ A. length ary2
18731862 mary <- A. new_ $ min (A. length ary1) (A. length ary2)
18741863 let go i j
@@ -1882,8 +1871,12 @@ intersectionCollisions f ary1 ary2 = do
18821871 go (i + 1 ) (j + 1 )
18831872 Nothing -> do
18841873 go (i + 1 ) j
1885- maryLen <- go 0 0
1886- pure (maryLen, mary)
1874+ len <- go 0 0
1875+ case len of
1876+ 0 -> pure Empty
1877+ 1 -> Leaf h1 <$> A. read mary 0
1878+ _ -> Collision h1 <$> (A. unsafeFreeze =<< A. shrink mary len)
1879+ | otherwise = Empty
18871880{-# INLINE intersectionCollisions #-}
18881881
18891882-- | Say we have
0 commit comments