{-# LANGUAGE UndecidableInstances, FlexibleInstances,
MultiParamTypeClasses, TemplateHaskell, PolymorphicComponents,
DeriveDataTypeable,ExistentialQuantification, KindSignatures,
StandaloneDeriving, GADTs #-}
module Data.IxSet.Typed.Ix
( Ix(..)
, insert
, delete
, fromList
, insertList
, deleteList
, union
, intersection
)
where
import Control.DeepSeq
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
data Ix (ix :: *) (a :: *) where
Ix :: !(Map ix (Set a)) -> (a -> [ix]) -> Ix ix a
instance (NFData ix, NFData a) => NFData (Ix ix a) where
rnf (Ix m f) = rnf m `seq` f `seq` ()
insert :: (Ord a, Ord k)
=> k -> a -> Map k (Set a) -> Map k (Set a)
insert k v index = Map.insertWith' Set.union k (Set.singleton v) index
insertList :: (Ord a, Ord k)
=> [(k,a)] -> Map k (Set a) -> Map k (Set a)
insertList xs index = List.foldl' (\m (k,v)-> insert k v m) index xs
fromList :: (Ord a, Ord k) => [(k, a)] -> Map k (Set a)
fromList xs =
Map.fromListWith Set.union (List.map (\ (k, v) -> (k, Set.singleton v)) xs)
delete :: (Ord a, Ord k)
=> k -> a -> Map k (Set a) -> Map k (Set a)
delete k v index = Map.update remove k index
where
remove set = let set' = Set.delete v set
in if Set.null set' then Nothing else Just set'
deleteList :: (Ord a, Ord k)
=> [(k,a)] -> Map k (Set a) -> Map k (Set a)
deleteList xs index = List.foldl' (\m (k,v) -> delete k v m) index xs
union :: (Ord a, Ord k)
=> Map k (Set a) -> Map k (Set a) -> Map k (Set a)
union index1 index2 = Map.unionWith Set.union index1 index2
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