Skip to content

Commit 62011a7

Browse files
Fix intersection bug #420 (#427)
Fixes #420. Co-authored-by: Brian Shu <[email protected]>
1 parent b86f32c commit 62011a7

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Data/HashMap/Internal.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,11 @@ intersectionArrayBy f !b1 !b2 !ary1 !ary2
18731873
(len, bFinal) <- go 0 0 0 bCombined bIntersect
18741874
case len of
18751875
0 -> pure Empty
1876-
1 -> A.read mary 0
1876+
1 -> do
1877+
l <- A.read mary 0
1878+
if isLeafOrCollision l
1879+
then pure l
1880+
else BitmapIndexed bFinal <$> (A.unsafeFreeze =<< A.shrink mary 1)
18771881
_ -> bitmapIndexedOrFull bFinal <$> (A.unsafeFreeze =<< A.shrink mary len)
18781882
where
18791883
bCombined = b1 .|. b2
@@ -1927,7 +1931,6 @@ searchSwap toFind start = go start toFind start
19271931
else go i0 k (i + 1) mary
19281932
{-# INLINE searchSwap #-}
19291933

1930-
19311934
------------------------------------------------------------------------
19321935
-- * Folds
19331936

tests/Regressions.hs

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Regressions (tests) where
77

88
import Control.Exception (evaluate)
99
import Control.Monad (replicateM)
10+
import Data.Bits (shiftL)
1011
import Data.Hashable (Hashable (..))
1112
import Data.List (delete)
1213
import Data.Maybe (isJust, isNothing)
@@ -24,6 +25,7 @@ import Test.Tasty.QuickCheck (testProperty)
2425

2526
import qualified Data.HashMap.Lazy as HML
2627
import qualified Data.HashMap.Strict as HMS
28+
import qualified Data.HashSet as HS
2729

2830
#if MIN_VERSION_base(4,12,0)
2931
-- nothunks requires base >= 4.12
@@ -248,6 +250,18 @@ issue383 = do
248250

249251
#endif
250252

253+
------------------------------------------------------------------------
254+
-- Issue #420
255+
256+
issue420 :: Assertion
257+
issue420 = do
258+
let k1 :: Int = 1 `shiftL` 10
259+
let k2 :: Int = 2 `shiftL` 10
260+
let s0 = HS.fromList [k1, k2]
261+
let s1 = s0 `HS.intersection` s0
262+
assert $ k1 `HS.member` s1
263+
assert $ k2 `HS.member` s1
264+
251265
------------------------------------------------------------------------
252266
-- * Test list
253267

@@ -277,4 +291,5 @@ tests = testGroup "Regression tests"
277291
#ifdef HAVE_NOTHUNKS
278292
, testCase "issue383" issue383
279293
#endif
294+
, testCase "issue420" issue420
280295
]

0 commit comments

Comments
 (0)