Skip to content
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 :: forall ixs a. Indexable ixs a => IxSet ixs a -> IxSet ixs a -> IxSet ixs a
difference (IxSet elements ixs) (IxSet deletes deleteIxs) =
IxSet (elements `Set.difference` deletes) $
zipWithIxList' diffIx ixs deleteIxs
where
diffIx (Ix ix ixer) (Ix delIx _) = Ix (Ix.difference ix delIx) ixer

-- | Remove every element of a 'Set' from an 'IxSet'.
deleteSet :: Indexable ixs a => Set a -> IxSet ixs a -> IxSet ixs a
deleteSet deletes set = set `difference` fromSet deletes

-- | 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
26 changes: 22 additions & 4 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
, difference
, union
, intersection
)
Expand All @@ -27,10 +28,12 @@ import Control.DeepSeq
import Data.Kind
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Map as Map
import qualified Data.Map.Strict as Map.Strict
import qualified Data.Map.Merge.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 @@ -116,6 +119,21 @@ union index1 index2 = Map.unionWith Set.union index1 index2
-- | Takes the intersection of two sets.
intersection :: (Ord a, Ord k)
=> Map k (Set a) -> Map k (Set a) -> Map k (Set a)
intersection index1 index2 = Map.filter (not . Set.null) $
Map.intersectionWith Set.intersection index1 index2

intersection = Map.Strict.merge
Map.Strict.dropMissing
Map.Strict.dropMissing
(Map.Strict.zipWithMaybeMatched $ \_ els1 els2 ->
let r = Set.intersection els1 els2
in r <$ guard (not (Set.null r))
)

-- | Deletes a multimap of values from the index.
difference :: (Ord a, Ord k)
=> Map k (Set a) -> Map k (Set a) -> Map k (Set a)
difference = Map.Strict.merge
Map.Strict.preserveMissing
Map.Strict.dropMissing
(Map.Strict.zipWithMaybeMatched $ \_ els dels ->
let deleted = els `Set.difference` dels
in deleted <$ guard (not (Set.null els))
)
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