[commit: packages/containers] cleaned_bugfix394, master, revert-408-bugfix_394: Fix buggy restrictKeys and withoutKeys (#393) (de8766e)
git at git.haskell.org
git at git.haskell.org
Mon Apr 17 21:47:24 UTC 2017
Repository : ssh://git@git.haskell.org/containers
On branches: cleaned_bugfix394,master,revert-408-bugfix_394
Link : http://git.haskell.org/packages/containers.git/commitdiff/de8766eb9f5e148bb3b0145232e00816dbd2414a
>---------------------------------------------------------------
commit de8766eb9f5e148bb3b0145232e00816dbd2414a
Author: David Feuer <David.Feuer at gmail.com>
Date: Mon Feb 6 16:07:33 2017 -0500
Fix buggy restrictKeys and withoutKeys (#393)
`restrictKeys` and `withoutKeys` for `Data.IntMap` were completely
wrong. The QuickCheck properties that should have caught this were
never actually run.
* Fix the implementations
* Make the tests actually run.
Fixes #392
>---------------------------------------------------------------
de8766eb9f5e148bb3b0145232e00816dbd2414a
Data/IntMap/Internal.hs | 53 +++++++++-------------------------------------
tests/intmap-properties.hs | 2 ++
2 files changed, 12 insertions(+), 43 deletions(-)
diff --git a/Data/IntMap/Internal.hs b/Data/IntMap/Internal.hs
index b42aab1..c3fe437 100644
--- a/Data/IntMap/Internal.hs
+++ b/Data/IntMap/Internal.hs
@@ -1058,30 +1058,14 @@ withoutKeys = go
| zero p1 m2 = bin p2 m2 (go t1 l2) Nil
| otherwise = bin p2 m2 Nil (go t1 r2)
- go t1'@(Bin _ _ _ _) t2'@(IntSet.Tip k2' _) = merge0 t2' k2' t1'
- where
- merge0 t2 k2 t1@(Bin p1 m1 l1 r1)
- | nomatch k2 p1 m1 = t1
- | zero k2 m1 = binCheckLeft p1 m1 (merge0 t2 k2 l1) r1
- | otherwise = binCheckRight p1 m1 l1 (merge0 t2 k2 r1)
- merge0 _ k2 t1@(Tip k1 _)
- | k1 == k2 = Nil
- | otherwise = t1
- merge0 _ _ Nil = Nil
+ go t1'@(Bin _ _ _ _) t2'@(IntSet.Tip _ _) =
+ filterWithKey (\k _ -> k `IntSet.notMember` t2') t1'
go t1@(Bin _ _ _ _) IntSet.Nil = t1
- go t1'@(Tip k1' _) t2' = merge0 t1' k1' t2'
- where
- merge0 t1 k1 (IntSet.Bin p2 m2 l2 r2)
- | nomatch k1 p2 m2 = t1
- | zero k1 m2 = bin p2 m2 (merge0 t1 k1 l2) Nil
- | otherwise = bin p2 m2 Nil (merge0 t1 k1 r2)
- merge0 t1 k1 (IntSet.Tip k2 _)
- | k1 == k2 = Nil
- | otherwise = t1
- merge0 t1 _ IntSet.Nil = t1
-
+ go t1'@(Tip k1' _) t2'
+ | k1' `IntSet.member` t2' = Nil
+ | otherwise = t1'
go Nil _ = Nil
@@ -1119,30 +1103,13 @@ restrictKeys = go
| zero p1 m2 = bin p2 m2 (go t1 l2) Nil
| otherwise = bin p2 m2 Nil (go t1 r2)
- go t1'@(Bin _ _ _ _) t2'@(IntSet.Tip k2' _) = merge0 t2' k2' t1'
- where
- merge0 t2 k2 (Bin p1 m1 l1 r1)
- | nomatch k2 p1 m1 = Nil
- | zero k2 m1 = bin p1 m1 (merge0 t2 k2 l1) Nil
- | otherwise = bin p1 m1 Nil (merge0 t2 k2 r1)
- merge0 _ k2 t1@(Tip k1 _)
- | k1 == k2 = t1
- | otherwise = Nil
- merge0 _ _ Nil = Nil
-
+ go t1'@(Bin _ _ _ _) t2'@(IntSet.Tip _ _) =
+ filterWithKey (\k _ -> k `IntSet.member` t2') t1'
go (Bin _ _ _ _) IntSet.Nil = Nil
- go t1'@(Tip k1' _) t2' = merge0 t1' k1' t2'
- where
- merge0 t1 k1 (IntSet.Bin p2 m2 l2 r2)
- | nomatch k1 p2 m2 = Nil
- | zero k1 m2 = bin p2 m2 (merge0 t1 k1 l2) Nil
- | otherwise = bin p2 m2 Nil (merge0 t1 k1 r2)
- merge0 t1 k1 (IntSet.Tip k2 _)
- | k1 == k2 = t1
- | otherwise = Nil
- merge0 _ _ IntSet.Nil = Nil
-
+ go t1'@(Tip k1' _) t2'
+ | k1' `IntSet.member` t2' = t1'
+ | otherwise = Nil
go Nil _ = Nil
-- | /O(n+m)/. The intersection with a combining function.
diff --git a/tests/intmap-properties.hs b/tests/intmap-properties.hs
index 21ee9f6..a6fbe2f 100644
--- a/tests/intmap-properties.hs
+++ b/tests/intmap-properties.hs
@@ -167,6 +167,8 @@ main = defaultMain
, testProperty "foldl'" prop_foldl'
, testProperty "keysSet" prop_keysSet
, testProperty "fromSet" prop_fromSet
+ , testProperty "restrictKeys" prop_restrictKeys
+ , testProperty "withoutKeys" prop_withoutKeys
]
apply2 :: Fun (a, b) c -> a -> b -> c
More information about the ghc-commits
mailing list