Skip to content

Commit 182b06b

Browse files
author
Johannes Waldmann
committed
use actual upper bound, not its successor, as this might overflow
1 parent 7d89135 commit 182b06b

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

containers/src/Data/IntSet/Internal.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,20 +1261,20 @@ relate Nil t2 = Prefix
12611261
relate t1 Nil = FlipPrefix
12621262
relate t1@(Tip p1 bm1) t2@(Tip p2 bm2) = relateTipTip t1 t2
12631263
relate t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
1264-
| succUpperbound t1 <= lowerbound t2 = Less
1265-
| lowerbound t1 >= succUpperbound t2 = Greater
1264+
| upperbound t1 < lowerbound t2 = Less
1265+
| lowerbound t1 > upperbound t2 = Greater
12661266
| otherwise = case compare (natFromInt m1) (natFromInt m2) of
12671267
GT -> combine_left (relate l1 t2)
12681268
EQ -> combine (relate l1 l2) (relate r1 r2)
12691269
LT -> combine_right (relate t1 l2)
12701270
relate t1@(Bin p1 m1 l1 r1) t2@(Tip p2 _)
1271-
| succUpperbound t1 <= lowerbound t2 = Less
1272-
| lowerbound t1 >= succUpperbound t2 = Greater
1271+
| upperbound t1 < lowerbound t2 = Less
1272+
| lowerbound t1 > upperbound t2 = Greater
12731273
| 0 == (m1 .&. p2) = combine_left (relate l1 t2)
12741274
| otherwise = Less
12751275
relate t1@(Tip p1 _) t2@(Bin p2 m2 l2 r2)
1276-
| succUpperbound t1 <= lowerbound t2 = Less
1277-
| lowerbound t1 >= succUpperbound t2 = Greater
1276+
| upperbound t1 < lowerbound t2 = Less
1277+
| lowerbound t1 > upperbound t2 = Greater
12781278
| 0 == (p1 .&. m2) = combine_right (relate t1 l2)
12791279
| otherwise = Greater
12801280

@@ -1343,12 +1343,11 @@ lowerbound :: IntSet -> Int
13431343
lowerbound (Tip p _) = p
13441344
lowerbound (Bin p _ _ _) = p
13451345

1346-
-- | this is one more than the actual upper bound (to save one operation)
1347-
-- shall only be applied to non-mixed non-Nil trees
1348-
succUpperbound :: IntSet -> Int
1349-
{-# INLINE succUpperbound #-}
1350-
succUpperbound (Tip p _) = p + wordSize
1351-
succUpperbound (Bin p m _ _) = p + shiftL m 1
1346+
-- | shall only be applied to non-mixed non-Nil trees
1347+
upperbound :: IntSet -> Int
1348+
{-# INLINE upperbound #-}
1349+
upperbound (Tip p _) = p + (wordSize - 1)
1350+
upperbound (Bin p m _ _) = p + (shiftL m 1 - 1)
13521351

13531352
-- | split a set into subsets of negative and non-negative elements
13541353
splitSign :: IntSet -> (IntSet,IntSet)

0 commit comments

Comments
 (0)