@@ -1787,19 +1787,12 @@ intersectionWithKey# f = go 0
1787
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
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
1789
1789
-- 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
1798
1791
-- 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
1803
1796
-- collision vs. branch
1804
1797
go s (BitmapIndexed b1 ary1) t2@ (Collision h2 _ls2)
1805
1798
| b1 .&. m2 == 0 = Empty
@@ -1819,16 +1812,6 @@ intersectionWithKey# f = go 0
1819
1812
go s t1@ (Collision h1 _ls1) (Full ary2) = go (s + bitsPerSubkey) t1 (A. index ary2 i)
1820
1813
where
1821
1814
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)
1832
1815
{-# INLINE intersectionWithKey# #-}
1833
1816
1834
1817
intersectionArrayBy ::
@@ -1840,10 +1823,12 @@ intersectionArrayBy ::
1840
1823
Bitmap ->
1841
1824
A. Array (HashMap k v1 ) ->
1842
1825
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
1845
1830
mary <- A. new_ $ popCount bIntersect
1846
- -- iterate over nonzero bits of b1 .& . b2
1831
+ -- iterate over nonzero bits of b1 .| . b2
1847
1832
let go ! i ! i1 ! i2 ! b ! bFinal
1848
1833
| b == 0 = pure (i, bFinal)
1849
1834
| testBit $ b1 .&. b2 = do
@@ -1860,15 +1845,19 @@ intersectionArrayBy f !b1 !b2 !ary1 !ary2 = do
1860
1845
m = 1 `unsafeShiftL` countTrailingZeros b
1861
1846
testBit x = x .&. m /= 0
1862
1847
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)
1865
1853
where
1866
1854
bCombined = b1 .|. b2
1867
1855
bIntersect = b1 .&. b2
1868
1856
{-# INLINE intersectionArrayBy #-}
1869
1857
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
1872
1861
mary2 <- A. thaw ary2 0 $ A. length ary2
1873
1862
mary <- A. new_ $ min (A. length ary1) (A. length ary2)
1874
1863
let go i j
@@ -1882,8 +1871,12 @@ intersectionCollisions f ary1 ary2 = do
1882
1871
go (i + 1 ) (j + 1 )
1883
1872
Nothing -> do
1884
1873
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
1887
1880
{-# INLINE intersectionCollisions #-}
1888
1881
1889
1882
-- | Say we have
0 commit comments