module Data.EnumSet
( EnumSet
, intSetToEnumSet
, enumSetToIntSet
, (\\)
, null
, size
, member
, notMember
, isSubsetOf
, isProperSubsetOf
, empty
, singleton
, insert
, delete
, union
, unions
, difference
, intersection
, filter
, partition
, split
, splitMember
, map
, fold
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, maxView
, minView
, elems
, toList
, fromList
, toAscList
, fromAscList
, fromDistinctAscList
) where
import Prelude hiding ( filter, lookup, map, null )
import qualified Prelude as P
import Data.IntSet ( IntSet )
import qualified Data.IntSet as I
import Data.Monoid ( Monoid )
import Data.Typeable ( Typeable )
import Text.Read
newtype EnumSet e = EnumSet { unWrap :: IntSet }
deriving (Eq, Monoid, Ord, Typeable)
instance (Enum e, Show e) => Show (EnumSet e) where
showsPrec p es = showParen (p > 10) $
showString "fromList " . shows (toList es)
instance (Enum e, Read e) => Read (EnumSet e) where
readPrec = parens . prec 10 $ do
Ident "fromList" <- lexP
list <- readPrec
return (fromList list)
intSetToEnumSet :: IntSet -> EnumSet e
intSetToEnumSet = EnumSet
enumSetToIntSet :: EnumSet e -> IntSet
enumSetToIntSet = unWrap
pairWrap :: (IntSet, IntSet) -> (EnumSet e, EnumSet e)
pairWrap (is1, is2) = (EnumSet is1, EnumSet is2)
toEnumWrap :: (Enum e) => (Int, IntSet) -> (e, EnumSet e)
toEnumWrap (i, is) = (toEnum i, EnumSet is)
(\\) :: EnumSet e -> EnumSet e -> EnumSet e
(EnumSet is1) \\ (EnumSet is2) = EnumSet $ is1 I.\\ is2
null :: EnumSet e -> Bool
null = I.null . unWrap
size :: EnumSet e -> Int
size = I.size . unWrap
member :: (Enum e) => e -> EnumSet e -> Bool
member e = I.member (fromEnum e) . unWrap
notMember :: (Enum e) => e -> EnumSet e -> Bool
notMember e = I.notMember (fromEnum e) . unWrap
empty :: EnumSet e
empty = EnumSet I.empty
singleton :: (Enum e) => e -> EnumSet e
singleton = EnumSet . I.singleton . fromEnum
insert :: (Enum e) => e -> EnumSet e -> EnumSet e
insert e = EnumSet . I.insert (fromEnum e) . unWrap
delete :: (Enum e) => e -> EnumSet e -> EnumSet e
delete e = EnumSet . I.delete (fromEnum e) . unWrap
unions :: [EnumSet e] -> EnumSet e
unions = EnumSet . I.unions . P.map unWrap
union :: EnumSet e -> EnumSet e -> EnumSet e
union (EnumSet is1) (EnumSet is2) = EnumSet $ I.union is1 is2
difference :: EnumSet e -> EnumSet e -> EnumSet e
difference (EnumSet is1) (EnumSet is2) = EnumSet $ I.difference is1 is2
intersection :: EnumSet e -> EnumSet e -> EnumSet e
intersection (EnumSet is1) (EnumSet is2) = EnumSet $ I.intersection is1 is2
isProperSubsetOf :: EnumSet e -> EnumSet e -> Bool
isProperSubsetOf (EnumSet is1) (EnumSet is2) = I.isProperSubsetOf is1 is2
isSubsetOf :: EnumSet e -> EnumSet e -> Bool
isSubsetOf (EnumSet is1) (EnumSet is2) = I.isSubsetOf is1 is2
filter :: (Enum e) => (e -> Bool) -> EnumSet e -> EnumSet e
filter f = EnumSet . I.filter (f . toEnum) . unWrap
partition :: (Enum e) => (e -> Bool) -> EnumSet e -> (EnumSet e, EnumSet e)
partition f = pairWrap . I.partition (f . toEnum) . unWrap
split :: (Enum e) => e -> EnumSet e -> (EnumSet e, EnumSet e)
split e = pairWrap . I.split (fromEnum e) . unWrap
splitMember :: (Enum e) => e -> EnumSet e -> (EnumSet e, Bool, EnumSet e)
splitMember e = wrap . I.splitMember (fromEnum e) . unWrap
where
wrap (is1, b, is2) = (EnumSet is1, b, EnumSet is2)
maxView :: (Enum e) => EnumSet e -> Maybe (e, EnumSet e)
maxView = fmap toEnumWrap . I.maxView . unWrap
minView :: (Enum e) => EnumSet e -> Maybe (e, EnumSet e)
minView = fmap toEnumWrap . I.minView . unWrap
deleteFindMin :: (Enum e) => EnumSet e -> (e, EnumSet e)
deleteFindMin = toEnumWrap . I.deleteFindMin . unWrap
deleteFindMax :: (Enum e) => EnumSet e -> (e, EnumSet e)
deleteFindMax = toEnumWrap . I.deleteFindMax . unWrap
findMin :: (Enum e) => EnumSet e -> e
findMin = toEnum . I.findMin . unWrap
findMax :: (Enum e) => EnumSet e -> e
findMax = toEnum . I.findMax . unWrap
deleteMin :: EnumSet e -> EnumSet e
deleteMin = EnumSet . I.deleteMin . unWrap
deleteMax :: EnumSet e -> EnumSet e
deleteMax = EnumSet . I.deleteMax . unWrap
map :: (Enum e) => (e -> e) -> EnumSet e -> EnumSet e
map f = EnumSet . I.map (fromEnum . f . toEnum) . unWrap
fold :: (Enum e) => (e -> b -> b) -> b -> EnumSet e -> b
fold f acc = I.fold (f . toEnum) acc . unWrap
elems :: (Enum e) => EnumSet e -> [e]
elems = P.map toEnum . I.elems . unWrap
toList :: (Enum e) => EnumSet e -> [e]
toList = P.map toEnum . I.toList . unWrap
toAscList :: (Enum e) => EnumSet e -> [e]
toAscList = P.map toEnum . I.toAscList . unWrap
fromList :: (Enum e) => [e] -> EnumSet e
fromList = EnumSet . I.fromList . P.map fromEnum
fromAscList :: (Enum e) => [e] -> EnumSet e
fromAscList = EnumSet . I.fromAscList . P.map fromEnum
fromDistinctAscList :: (Enum e) => [e] -> EnumSet e
fromDistinctAscList = EnumSet . I.fromDistinctAscList . P.map fromEnum