Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deleteSet and difference operations #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Data/IxSet/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ module Data.IxSet.Typed
insert,
insertList,
delete,
deleteSet,
updateIx,
deleteIx,

Expand All @@ -152,8 +153,10 @@ module Data.IxSet.Typed
-- * Set operations
(&&&),
(|||),
(\\\),
union,
intersection,
difference,

-- * Indexing
(@=),
Expand Down Expand Up @@ -737,6 +740,18 @@ insert = change Set.insert Ix.insert
delete :: Indexable ixs a => a -> IxSet ixs a -> IxSet ixs a
delete = change Set.delete Ix.delete

-- | Remove every item in the second 'IxSet' from the first 'IxSet'.
difference :: Indexable ixs a => IxSet ixs a -> IxSet ixs a -> IxSet ixs a
difference ixSet (IxSet deletes _) = deleteSet deletes ixSet

-- | Remove every element of a 'Set' from an 'IxSet'.
deleteSet :: Indexable ixs a => Set a -> IxSet ixs a -> IxSet ixs a
deleteSet deletes (IxSet set ixs) = IxSet
(Set.difference set deletes)
(mapIxList'
(\(Ix m f) -> Ix (Ix.deleteSet deletes m) f)
ixs)

-- | Will replace the item with the given index of type 'ix'.
-- Only works if there is at most one item with that index in the 'IxSet'.
-- Will not change 'IxSet' if you have more than one item with given index.
Expand Down Expand Up @@ -822,6 +837,9 @@ null (IxSet a _) = Set.null a
(|||) :: Indexable ixs a => IxSet ixs a -> IxSet ixs a -> IxSet ixs a
(|||) = union

(\\\) :: Indexable ixs a => IxSet ixs a -> IxSet ixs a -> IxSet ixs a
(\\\) = difference

infixr 5 &&&
infixr 5 |||

Expand Down
10 changes: 10 additions & 0 deletions src/Data/IxSet/Typed/Ix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Data.IxSet.Typed.Ix
, fromList
, insertList
, deleteList
, deleteSet
, union
, intersection
)
Expand All @@ -31,6 +32,7 @@ import qualified Data.Map as Map
import qualified Data.Map.Strict as Map.Strict
import Data.Set (Set)
import qualified Data.Set as Set
import Control.Monad (guard)

-- the core datatypes

Expand Down Expand Up @@ -119,3 +121,11 @@ intersection :: (Ord a, Ord k)
intersection index1 index2 = Map.filter (not . Set.null) $
Map.intersectionWith Set.intersection index1 index2

-- | Deletes a set of values from the index.
deleteSet :: (Ord a, Ord k) => Set a -> Map k (Set a) -> Map k (Set a)
deleteSet deletes index2 =
Map.mapMaybe
(\set ->
let diff = Set.difference set deletes
in diff <$ guard (not (Set.null diff)))
index2
6 changes: 6 additions & 0 deletions tests/Data/IxSet/Typed/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ prop_intersection ixset1 ixset2 =
toSet (ixset1 `intersection` ixset2) ==
toSet ixset1 `Set.intersection` toSet ixset2

prop_difference :: Foos -> Foos -> Bool
prop_difference ixset1 ixset2 =
toSet (ixset1 `difference` ixset2) ==
toSet ixset1 `Set.difference` toSet ixset2

prop_any :: Foos -> [Int] -> Bool
prop_any ixset idxs =
(ixset @+ idxs) == foldr union empty (map ((@=) ixset) idxs)
Expand All @@ -172,6 +177,7 @@ setOps :: TestTree
setOps = testGroup "set operations" $
[ testProperty "distributivity toSet / union" $ prop_union
, testProperty "distributivity toSet / intersection" $ prop_intersection
, testProperty "distributivity toSet / difference" $ prop_difference
, testProperty "any (@+)" $ prop_any
, testProperty "all (@*)" $ prop_all
]
Expand Down