EnumSet EnumMap
Jump to navigation
Jump to search
I wanted to replace (Map Char) with (IntMap) and keep type safety. So I wrote a newtype layer over IntMap and IntSet
ChrisKuklewicz 14:41, 23 February 2007 (UTC)
EnumSet
module EnumSet where
import Data.List as L (map)
import qualified Data.IntMap as M(IntMap)
import qualified Data.IntSet as S
import Data.Monoid(Monoid)
newtype EnumSet e = EnumSet {unEnumSet :: S.IntSet}
deriving (Monoid,Eq,Read,Show,Ord)
(\\) :: (Enum e) => EnumSet e -> EnumSet e -> EnumSet e
(\\) (EnumSet s1) (EnumSet s2) = EnumSet ((S.\\) s1 s2)
null :: (Enum e) => EnumSet e -> Bool
null (EnumSet s) = S.null s
size :: (Enum e) => EnumSet e -> Int
size (EnumSet s) = S.size s
member :: (Enum e) => e -> EnumSet e -> Bool
member e (EnumSet s) = S.member (fromEnum e) s
notMember :: (Enum e) => Int -> EnumSet e -> Bool
notMember e (EnumSet s) = S.notMember (fromEnum e) s
isSubsetOf :: (Enum e) => EnumSet e -> EnumSet e -> Bool
isSubsetOf (EnumSet e1) (EnumSet e2) = S.isSubsetOf e1 e2
isProperSubsetOf :: (Enum e) => EnumSet e -> EnumSet e -> Bool
isProperSubsetOf (EnumSet e1) (EnumSet e2) = S.isProperSubsetOf e1 e2
empty :: (Enum e) => EnumSet e
empty = EnumSet (S.empty)
singleton :: (Enum e) => e -> EnumSet e
singleton e = EnumSet (S.singleton (fromEnum e))
insert :: (Enum e) => e -> EnumSet e -> EnumSet e
insert e (EnumSet s) = EnumSet (S.insert (fromEnum e) s)
delete :: (Enum e) => e -> EnumSet e -> EnumSet e
delete e (EnumSet s) = EnumSet (S.delete (fromEnum e) s)
union :: (Enum e) => EnumSet e -> EnumSet e -> EnumSet e
union (EnumSet s1) (EnumSet s2) = EnumSet (S.union s1 s2)
unions :: (Enum e) => [EnumSet e] -> EnumSet e
unions es = EnumSet (S.unions (L.map unEnumSet es))
difference :: (Enum e) => EnumSet e -> EnumSet e -> EnumSet e
difference (EnumSet e1) (EnumSet e2) = EnumSet (S.difference e1 e2)
intersection :: (Enum e) => EnumSet e -> EnumSet e -> EnumSet e
intersection (EnumSet e1) (EnumSet e2) = EnumSet (S.intersection e1 e2)
filter :: (Enum e) => (e -> Bool) -> EnumSet e -> EnumSet e
filter f (EnumSet s) = EnumSet (S.filter f' s)
where f' b = f (toEnum b)
partition :: (Enum e) => (e -> Bool) -> EnumSet e -> (EnumSet e, EnumSet e)
partition f (EnumSet s) = (EnumSet s1', EnumSet s2')
where (s1',s2') = S.partition f' s
f' b = f (toEnum b)
split :: (Enum e) => e -> EnumSet e -> (EnumSet e, EnumSet e)
split e (EnumSet s) = (EnumSet s1', EnumSet s2')
where (s1',s2') = S.split (fromEnum e) s
splitMember :: (Enum e) => e -> EnumSet e -> (EnumSet e, Bool, EnumSet e)
splitMember e (EnumSet s) = (EnumSet s1',a,EnumSet s2')
where (s1',a,s2') = S.splitMember (fromEnum e) s
map :: (Enum e) => (e -> e) -> EnumSet e -> EnumSet e
map f (EnumSet s) = EnumSet (S.map f' s)
where f' b = fromEnum (f (toEnum b))
fold :: (Enum e) => (e -> b -> b) -> b -> EnumSet e -> b
fold f a (EnumSet s) = S.fold f' a s
where f' b a = f (toEnum b) a
elems :: (Enum e) => EnumSet e -> [e]
elems (EnumSet s) = L.map toEnum (S.elems s)
toList :: (Enum e) => EnumSet e -> [e]
toList (EnumSet s) = L.map toEnum (S.toList s)
fromList :: (Enum e) => [e] -> EnumSet e
fromList es = EnumSet (S.fromList (L.map fromEnum es))
toAscList :: (Enum e) => EnumSet e -> [e]
toAscList (EnumSet s) = L.map toEnum (S.toAscList s)
fromAscList :: (Enum e) => [e] -> EnumSet e
fromAscList es = EnumSet (S.fromAscList (L.map fromEnum es))
fromDistinctAscList :: (Enum e) => [e] -> EnumSet e
fromDistinctAscList es = EnumSet (S.fromDistinctAscList (L.map fromEnum es))
showTree :: (Enum e) => EnumSet e -> String
showTree (EnumSet s) = S.showTree s
showTreeWith :: (Enum e) => Bool -> Bool -> EnumSet e -> String
showTreeWith a1 a2 (EnumSet s) = S.showTreeWith a1 a2 s
EnumMap
module EnumMap where
import Data.List as L (map)
import qualified Data.IntMap as M -- everything
import Data.Monoid(Monoid)
import Data.Foldable
import qualified EnumSet as S (EnumSet(..))
newtype EnumMap k a = EnumMap {unEnumMap :: M.IntMap a}
deriving (Eq,Read,Show,Ord,Monoid,Foldable,Functor)
(!) :: (Enum key) => EnumMap key a -> key -> a
(!) (EnumMap m) k = (M.!) m (fromEnum k)
(\\) :: (Enum key) => EnumMap key a -> EnumMap key b -> EnumMap key a
(\\) (EnumMap m1) (EnumMap m2) = EnumMap ((M.\\) m1 m2)
null :: (Enum key) => EnumMap key a -> Bool
null (EnumMap m) = M.null m
size :: (Enum key) => EnumMap key a -> Int
size (EnumMap m) = M.size m
member :: (Enum key) => key -> EnumMap key a -> Bool
member k (EnumMap m) = M.member (fromEnum k) m
notMember :: (Enum key) => key -> EnumMap key a -> Bool
notMember k (EnumMap m) = M.notMember (fromEnum k) m
lookup :: (Enum key,Monad m) => key -> EnumMap key a -> m a
lookup k (EnumMap m) = M.lookup (fromEnum k) m
findWithDefault :: (Enum key) => a -> key -> EnumMap key a -> a
findWithDefault a k (EnumMap m) = M.findWithDefault a (fromEnum k) m
empty :: (Enum key) => EnumMap key a
empty = EnumMap M.empty
singleton :: (Enum key) => key -> a -> EnumMap key a
singleton k a = EnumMap (M.singleton (fromEnum k) a)
insert :: (Enum key) => key -> a -> EnumMap key a -> EnumMap key a
insert k a (EnumMap m) = EnumMap (M.insert (fromEnum k) a m)
insertWith :: (Enum key) => (a -> a -> a) -> key -> a -> EnumMap key a -> EnumMap key a
insertWith f k a (EnumMap m) = EnumMap (M.insertWith f (fromEnum k) a m)
insertWithKey :: (Enum key) => (key -> a -> a -> a) -> key -> a -> EnumMap key a -> EnumMap key a
insertWithKey f k a (EnumMap m) = EnumMap (M.insertWithKey f' (fromEnum k) a m)
where f' b a1 a2 = f (toEnum b) a1 a2
insertLookupWithKey :: (Enum key) => (key -> a -> a -> a) -> key -> a -> EnumMap key a -> (Maybe a, EnumMap key a)
insertLookupWithKey f k a (EnumMap m) = (ma,EnumMap m')
where (ma,m') = M.insertLookupWithKey f' (fromEnum k) a m
f' b a1 a2 = f (toEnum b) a1 a2
delete :: (Enum key) => key -> EnumMap key a -> EnumMap key a
delete k (EnumMap m) = EnumMap (M.delete (fromEnum k) m)
adjust :: (Enum key) => (a -> a) -> key -> EnumMap key a -> EnumMap key a
adjust f k (EnumMap m) = EnumMap (M.adjust f (fromEnum k) m)
adjustWithKey :: (Enum key) => (key -> a -> a) -> key -> EnumMap key a -> EnumMap key a
adjustWithKey f k (EnumMap m) = EnumMap (M.adjustWithKey f' (fromEnum k) m)
where f' b a = f (toEnum b) a
update :: (Enum key) => (a -> Maybe a) -> key -> EnumMap key a -> EnumMap key a
update f k (EnumMap m) = EnumMap (M.update f (fromEnum k) m)
updateWithKey :: (Enum key) => (key -> a -> Maybe a) -> key -> EnumMap key a -> EnumMap key a
updateWithKey f k (EnumMap m) = EnumMap (M.updateWithKey f' (fromEnum k) m)
where f' b a = f (toEnum b) a
updateLookupWithKey :: (Enum key) => (key -> a -> Maybe a) -> key -> EnumMap key a -> (Maybe a, EnumMap key a)
updateLookupWithKey f k (EnumMap m) = (a,EnumMap m')
where (a,m') = M.updateLookupWithKey f' (fromEnum k) m
f' b a = f (toEnum b) a
union :: (Enum key) => EnumMap key a -> EnumMap key a -> EnumMap key a
union (EnumMap m1) (EnumMap m2) = EnumMap (M.union m1 m2)
unionWith :: (Enum key) => (a -> a -> a) -> EnumMap key a -> EnumMap key a -> EnumMap key a
unionWith f (EnumMap m1) (EnumMap m2) = EnumMap (M.unionWith f m1 m2)
unionWithKey :: (Enum key) => (key -> a -> a -> a) -> EnumMap key a -> EnumMap key a -> EnumMap key a
unionWithKey f (EnumMap m1) (EnumMap m2) = EnumMap (M.unionWithKey f' m1 m2)
where f' b a1 a2 = f (toEnum b) a2 a2
unions :: (Enum key) => [EnumMap key a] -> EnumMap key a
unions cs = EnumMap (M.unions (L.map unEnumMap cs))
unionsWith :: (Enum key) => (a -> a -> a) -> [EnumMap key a] -> EnumMap key a
unionsWith f cs = EnumMap (M.unionsWith f (L.map unEnumMap cs))
difference :: (Enum key) => EnumMap key a -> EnumMap key b -> EnumMap key a
difference (EnumMap m1) (EnumMap m2) = EnumMap (M.difference m1 m2)
differenceWith :: (Enum key) => (a -> b -> Maybe a) -> EnumMap key a -> EnumMap key b -> EnumMap key a
differenceWith f (EnumMap m1) (EnumMap m2) = EnumMap (M.differenceWith f m1 m2)
differenceWithKey :: (Enum key) => (key -> a -> b -> Maybe a) -> EnumMap key a -> EnumMap key b -> EnumMap key a
differenceWithKey f (EnumMap m1) (EnumMap m2) = EnumMap (M.differenceWithKey f' m1 m2)
where f' b a1 a2 = f (toEnum b) a1 a2
intersection :: (Enum key) => EnumMap key a -> EnumMap key b -> EnumMap key a
intersection (EnumMap m1) (EnumMap m2) = EnumMap (M.intersection m1 m2)
intersectionWith :: (Enum key) => (a -> b -> a) -> EnumMap key a -> EnumMap key b -> EnumMap key a
intersectionWith f (EnumMap m1) (EnumMap m2) = EnumMap (M.intersectionWith f m1 m2)
intersectionWithKey :: (Enum key) => (key -> a -> b -> a) -> EnumMap key a -> EnumMap key b -> EnumMap key a
intersectionWithKey f (EnumMap m1) (EnumMap m2) = EnumMap (M.intersectionWithKey f' m1 m2)
where f' b a1 a2 = f (toEnum b) a1 a2
map :: (Enum key) => (a -> b) -> EnumMap key a -> EnumMap key b
map f (EnumMap m) = EnumMap (M.map f m)
mapWithKey :: (Enum key) => (key -> a -> b) -> EnumMap key a -> EnumMap key b
mapWithKey f (EnumMap m) = EnumMap (M.mapWithKey f' m)
where f' b a = f (toEnum b) a
mapAccum :: (Enum key) => (a -> b -> (a, c)) -> a -> EnumMap key b -> (a, EnumMap key c)
mapAccum f a (EnumMap m) = (a',EnumMap m')
where (a',m') = M.mapAccum f a m
mapAccumWithKey :: (Enum key) => (a -> key -> b -> (a, c)) -> a -> EnumMap key b -> (a, EnumMap key c)
mapAccumWithKey f a (EnumMap m) = (a',EnumMap m')
where (a',m') = M.mapAccumWithKey f' a m
f' a1 b a2 = f a1 (toEnum b) a2
fold :: (Enum key) => (a -> b -> b) -> b -> EnumMap key a -> b
fold f a (EnumMap m) = M.fold f a m
foldWithKey :: (Enum key) => (key -> a -> b -> b) -> b -> EnumMap key a -> b
foldWithKey f a (EnumMap m) = M.foldWithKey f' a m
where f' b a1 a2 = f (toEnum b) a1 a2
elems :: (Enum key) => EnumMap key a -> [a]
elems (EnumMap m) = M.elems m
keys :: (Enum key) => EnumMap key a -> [key]
keys (EnumMap m) = L.map toEnum (M.keys m)
-- Depends on EnumSet
keysSet :: (Enum key) => EnumMap key a -> S.EnumSet key
keysSet (EnumMap m) = S.EnumSet (M.keysSet m)
assocs :: (Enum key) => EnumMap key a -> [(key, a)]
assocs (EnumMap m) = L.map (\(b,a) -> (toEnum b,a)) (M.assocs m)
toList :: (Enum key) => EnumMap key a -> [(key, a)]
toList (EnumMap m) = L.map (\(b,a) -> (toEnum b,a)) (M.toList m)
fromList :: (Enum key) => [(key, a)] -> EnumMap key a
fromList ka = EnumMap (M.fromList (L.map (\(k,a) -> (fromEnum k,a)) ka))
fromListWith :: (Enum key) => (a -> a -> a) -> [(key, a)] -> EnumMap key a
fromListWith f ka = EnumMap (M.fromListWith f (L.map (\(k,a) -> (fromEnum k,a)) ka))
fromListWithKey :: (Enum key) => (key -> a -> a -> a) -> [(key, a)] -> EnumMap key a
fromListWithKey f ka = EnumMap (M.fromListWithKey f' (L.map (\(k,a) -> (fromEnum k,a)) ka))
where f' b a1 a2 = f (toEnum b) a1 a2
toAscList :: (Enum key) => EnumMap key a -> [(key, a)]
toAscList (EnumMap m) = L.map (\(b,a) -> (toEnum b,a)) (M.toAscList m)
fromAscList :: (Enum key) => [(key, a)] -> EnumMap key a
fromAscList ka = EnumMap (M.fromAscList (L.map (\(k,a) -> (fromEnum k,a)) ka))
fromAscListWith :: (Enum key) => (a -> a -> a) -> [(key, a)] -> EnumMap key a
fromAscListWith f ka = EnumMap (M.fromAscListWith f (L.map (\(k,a) -> (fromEnum k,a)) ka))
fromAscListWithKey :: (Enum key) => (key -> a -> a -> a) -> [(key, a)] -> EnumMap key a
fromAscListWithKey f ka = EnumMap (M.fromAscListWithKey f' (L.map (\(k,a) -> (fromEnum k,a)) ka))
where f' b a1 a2 = f (toEnum b) a1 a2
fromDistinctAscList :: (Enum key) => [(key, a)] -> EnumMap key a
fromDistinctAscList ka = EnumMap (M.fromDistinctAscList (L.map (\(k,a) -> (fromEnum k,a)) ka))
filter :: (Enum key) => (a -> Bool) -> EnumMap key a -> EnumMap key a
filter f (EnumMap m) = EnumMap (M.filter f m)
filterWithKey :: (Enum key) => (key -> a -> Bool) -> EnumMap key a -> EnumMap key a
filterWithKey f (EnumMap m) = EnumMap (M.filterWithKey f' m)
where f' b a = f (toEnum b) a
partition :: (Enum key) => (a -> Bool) -> EnumMap key a -> (EnumMap key a, EnumMap key a)
partition f (EnumMap m) = (EnumMap m1', EnumMap m2')
where (m1',m2') = M.partition f m
partitionWithKey :: (Enum key) => (key -> a -> Bool) -> EnumMap key a -> (EnumMap key a, EnumMap key a)
partitionWithKey f (EnumMap m) = (EnumMap m1', EnumMap m2')
where (m1',m2') = M.partitionWithKey f' m
f' b a = f (toEnum b) a
mapMaybe :: (Enum key) => (a -> Maybe b) -> EnumMap key a -> EnumMap key b
mapMaybe f (EnumMap m) = EnumMap (M.mapMaybe f m)
mapMaybeWithKey :: (Enum key) => (key -> a -> Maybe b) -> EnumMap key a -> EnumMap key b
mapMaybeWithKey f (EnumMap m) = EnumMap (M.mapMaybeWithKey f' m)
where f' b a = f (toEnum b) a
mapEither :: (Enum key) => (a -> Either b c) -> EnumMap key a -> (EnumMap key b, EnumMap key c)
mapEither f (EnumMap m) = (EnumMap m1', EnumMap m2')
where (m1',m2') = M.mapEither f m
mapEitherWithKey :: (Enum key) => (key -> a -> Either b c) -> EnumMap key a -> (EnumMap key b, EnumMap key c)
mapEitherWithKey f (EnumMap m) = (EnumMap m1', EnumMap m2')
where (m1',m2') = M.mapEitherWithKey f' m
f' b a = f (toEnum b) a
split :: (Enum key) => key -> EnumMap key a -> (EnumMap key a, EnumMap key a)
split k (EnumMap m) = (EnumMap m1', EnumMap m2')
where (m1',m2') = M.split (fromEnum k) m
splitLookup :: (Enum key) => key -> EnumMap key a -> (EnumMap key a, Maybe a, EnumMap key a)
splitLookup k (EnumMap m) = (EnumMap m1', a, EnumMap m2')
where (m1',a,m2') = M.splitLookup (fromEnum k) m
isSubmapOf :: (Enum key,Eq a) => EnumMap key a -> EnumMap key a -> Bool
isSubmapOf (EnumMap m1) (EnumMap m2) = M.isSubmapOf m1 m2
isSubmapOfBy :: (Enum key) => (a -> b -> Bool) -> EnumMap key a -> EnumMap key b -> Bool
isSubmapOfBy f (EnumMap m1) (EnumMap m2) = M.isSubmapOfBy f m1 m2
isProperSubmapOf :: (Enum key,Eq a) => EnumMap key a -> EnumMap key a -> Bool
isProperSubmapOf (EnumMap m1) (EnumMap m2) = M.isProperSubmapOf m1 m2
isProperSubmapOfBy :: (Enum key) => (a -> b -> Bool) -> EnumMap key a -> EnumMap key b -> Bool
isProperSubmapOfBy f (EnumMap m1) (EnumMap m2) = M.isProperSubmapOfBy f m1 m2
showTree :: (Enum key,Show a) => EnumMap key a -> String
showTree (EnumMap m) = M.showTree m
showTreeWith :: (Enum key,Show a) => Bool -> Bool -> EnumMap key a -> String
showTreeWith b1 b2 (EnumMap m) = M.showTreeWith b1 b2 m