@@ -1784,8 +1784,16 @@ intersectionWithKey# f = go 0
17841784 go ! _ _ Empty = Empty
17851785 go _ Empty _ = Empty
17861786 -- leaf vs. anything
1787- 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
1788- 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
1787+ go s (Leaf h1 (L k1 v1)) t2 =
1788+ lookupCont
1789+ (\ _ -> Empty )
1790+ (\ v _ -> case f k1 v1 v of (# v' # ) -> Leaf h1 $ L k1 v')
1791+ h1 k1 s t2
1792+ go s t1 (Leaf h2 (L k2 v2)) =
1793+ lookupCont
1794+ (\ _ -> Empty )
1795+ (\ v _ -> case f k2 v v2 of (# v' # ) -> Leaf h2 $ L k2 v')
1796+ h2 k2 s t1
17891797 -- collision vs. collision
17901798 go _ (Collision h1 ls1) (Collision h2 ls2) = intersectionCollisions f h1 h2 ls1 ls2
17911799 -- branch vs. branch
@@ -1827,55 +1835,55 @@ intersectionArrayBy ::
18271835intersectionArrayBy f ! b1 ! b2 ! ary1 ! ary2
18281836 | b1 .&. b2 == 0 = Empty
18291837 | otherwise = runST $ do
1830- mary <- A. new_ $ popCount bIntersect
1831- -- iterate over nonzero bits of b1 .|. b2
1832- let go ! i ! i1 ! i2 ! b ! bFinal
1833- | b == 0 = pure (i, bFinal)
1834- | testBit $ b1 .&. b2 = do
1835- x1 <- A. indexM ary1 i1
1836- x2 <- A. indexM ary2 i2
1837- case f x1 x2 of
1838- Empty -> go i (i1 + 1 ) (i2 + 1 ) b' (bFinal .&. complement m)
1839- _ -> do
1840- A. write mary i $! f x1 x2
1841- go (i + 1 ) (i1 + 1 ) (i2 + 1 ) b' bFinal
1842- | testBit b1 = go i (i1 + 1 ) i2 b' bFinal
1843- | otherwise = go i i1 (i2 + 1 ) b' bFinal
1844- where
1845- m = 1 `unsafeShiftL` countTrailingZeros b
1846- testBit x = x .&. m /= 0
1847- b' = b .&. complement m
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)
1838+ mary <- A. new_ $ popCount bIntersect
1839+ -- iterate over nonzero bits of b1 .|. b2
1840+ let go ! i ! i1 ! i2 ! b ! bFinal
1841+ | b == 0 = pure (i, bFinal)
1842+ | testBit $ b1 .&. b2 = do
1843+ x1 <- A. indexM ary1 i1
1844+ x2 <- A. indexM ary2 i2
1845+ case f x1 x2 of
1846+ Empty -> go i (i1 + 1 ) (i2 + 1 ) b' (bFinal .&. complement m)
1847+ _ -> do
1848+ A. write mary i $! f x1 x2
1849+ go (i + 1 ) (i1 + 1 ) (i2 + 1 ) b' bFinal
1850+ | testBit b1 = go i (i1 + 1 ) i2 b' bFinal
1851+ | otherwise = go i i1 (i2 + 1 ) b' bFinal
1852+ where
1853+ m = 1 `unsafeShiftL` countTrailingZeros b
1854+ testBit x = x .&. m /= 0
1855+ b' = b .&. complement m
1856+ (len, bFinal) <- go 0 0 0 bCombined bIntersect
1857+ case len of
1858+ 0 -> pure Empty
1859+ 1 -> A. read mary 0
1860+ _ -> bitmapIndexedOrFull bFinal <$> (A. unsafeFreeze =<< A. shrink mary len)
18531861 where
18541862 bCombined = b1 .|. b2
18551863 bIntersect = b1 .&. b2
18561864{-# INLINE intersectionArrayBy #-}
18571865
18581866intersectionCollisions :: Eq k => (k -> v1 -> v2 -> (# v3 # )) -> Hash -> Hash -> A. Array (Leaf k v1 ) -> A. Array (Leaf k v2 ) -> HashMap k v3
18591867intersectionCollisions f h1 h2 ary1 ary2
1860- | h1 == h2 = runST $ do
1861- mary2 <- A. thaw ary2 0 $ A. length ary2
1862- mary <- A. new_ $ min (A. length ary1) (A. length ary2)
1863- let go i j
1864- | i >= A. length ary1 || j >= A. lengthM mary2 = pure j
1865- | otherwise = do
1866- L k1 v1 <- A. indexM ary1 i
1867- searchSwap k1 j mary2 >>= \ case
1868- Just (L _k2 v2) -> do
1869- let ! (# v3 # ) = f k1 v1 v2
1870- A. write mary j $ L k1 v3
1871- go (i + 1 ) (j + 1 )
1872- Nothing -> do
1873- go (i + 1 ) j
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)
1868+ | h1 == h2 = runST $ do
1869+ mary2 <- A. thaw ary2 0 $ A. length ary2
1870+ mary <- A. new_ $ min (A. length ary1) (A. length ary2)
1871+ let go i j
1872+ | i >= A. length ary1 || j >= A. lengthM mary2 = pure j
1873+ | otherwise = do
1874+ L k1 v1 <- A. indexM ary1 i
1875+ searchSwap k1 j mary2 >>= \ case
1876+ Just (L _k2 v2) -> do
1877+ let ! (# v3 # ) = f k1 v1 v2
1878+ A. write mary j $ L k1 v3
1879+ go (i + 1 ) (j + 1 )
1880+ Nothing -> do
1881+ go (i + 1 ) j
1882+ len <- go 0 0
1883+ case len of
1884+ 0 -> pure Empty
1885+ 1 -> Leaf h1 <$> A. read mary 0
1886+ _ -> Collision h1 <$> (A. unsafeFreeze =<< A. shrink mary len)
18791887 | otherwise = Empty
18801888{-# INLINE intersectionCollisions #-}
18811889
0 commit comments