module Data.EnumMap.Base
( EnumMap(..)
, intMapToEnumMap
, enumMapToIntMap
, (!)
, (\\)
, null
, size
, member
, notMember
, lookup
, findWithDefault
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, empty
, singleton
, insert
, insertWith
, insertWithKey
, insertLookupWithKey
, delete
, adjust
, adjustWithKey
, update
, updateWithKey
, updateLookupWithKey
, alter
, union
, unionWith
, unionWithKey
, unions
, unionsWith
, difference
, differenceWith
, differenceWithKey
, intersection
, intersectionWith
, intersectionWithKey
, mergeWithKey
, map
, mapWithKey
, traverseWithKey
, mapAccum
, mapAccumWithKey
, mapAccumRWithKey
, mapKeys
, mapKeysWith
, mapKeysMonotonic
, foldr
, foldl
, foldrWithKey
, foldlWithKey
, foldr'
, foldl'
, foldrWithKey'
, foldlWithKey'
, elems
, keys
, assocs
, keysSet
, fromSet
, toList
, fromList
, fromListWith
, fromListWithKey
, toAscList
, toDescList
, fromAscList
, fromAscListWith
, fromAscListWithKey
, fromDistinctAscList
, filter
, filterWithKey
, partition
, partitionWithKey
, mapMaybe
, mapMaybeWithKey
, mapEither
, mapEitherWithKey
, split
, splitLookup
, isSubmapOf
, isSubmapOfBy
, isProperSubmapOf
, isProperSubmapOfBy
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, updateMin
, updateMax
, updateMinWithKey
, updateMaxWithKey
, minView
, maxView
, minViewWithKey
, maxViewWithKey
) where
import Prelude hiding ( filter, foldr, foldl, lookup, map, null )
import qualified Prelude as P
import Control.Applicative ( Applicative, liftA )
import Data.IntMap.Lazy ( IntMap )
import qualified Data.IntMap.Lazy as I
import Data.EnumSet ( EnumSet )
import qualified Data.EnumSet as EnumSet
import Control.Arrow ( first, second, (***) )
import Control.DeepSeq ( NFData )
import Data.Foldable ( Foldable )
import Data.Monoid ( Monoid )
import Data.Traversable ( Traversable )
import Data.Typeable ( Typeable )
import Text.Read
newtype EnumMap k a = EnumMap { unWrap :: IntMap a }
deriving (Eq, Foldable, Functor, Ord, Monoid, Traversable, Typeable, NFData)
instance (Enum k, Show k, Show a) => Show (EnumMap k a) where
showsPrec p em = showParen (p > 10) $
showString "fromList " . shows (toList em)
instance (Enum k, Read k, Read a) => Read (EnumMap k a) where
readPrec = parens . prec 10 $ do
Ident "fromList" <- lexP
list <- readPrec
return (fromList list)
intMapToEnumMap :: IntMap a -> EnumMap k a
intMapToEnumMap = EnumMap
enumMapToIntMap :: EnumMap k a -> IntMap a
enumMapToIntMap = unWrap
(!) :: (Enum k) => EnumMap k a -> k -> a
(EnumMap im) ! k = im I.! (fromEnum k)
(\\) :: EnumMap k a -> EnumMap k b -> EnumMap k a
(EnumMap im1) \\ (EnumMap im2) = EnumMap $ im1 I.\\ im2
null :: EnumMap k a -> Bool
null = I.null . unWrap
size :: EnumMap k a -> Int
size = I.size . unWrap
member :: (Enum k) => k -> EnumMap k a -> Bool
member k = I.member (fromEnum k) . unWrap
notMember :: (Enum k) => k -> EnumMap k a -> Bool
notMember k = I.notMember (fromEnum k) . unWrap
lookup :: (Enum k) => k -> EnumMap k a -> Maybe a
lookup k = I.lookup (fromEnum k) . unWrap
findWithDefault :: (Enum k) => a -> k -> EnumMap k a -> a
findWithDefault def k = I.findWithDefault def (fromEnum k) . unWrap
lookupLT :: (Enum k) => k -> EnumMap k a -> Maybe (k, a)
lookupLT k = fmap (first toEnum) . I.lookupLT (fromEnum k) . unWrap
lookupGT :: (Enum k) => k -> EnumMap k a -> Maybe (k, a)
lookupGT k = fmap (first toEnum) . I.lookupGT (fromEnum k) . unWrap
lookupLE :: (Enum k) => k -> EnumMap k a -> Maybe (k, a)
lookupLE k = fmap (first toEnum) . I.lookupLE (fromEnum k) . unWrap
lookupGE :: (Enum k) => k -> EnumMap k a -> Maybe (k, a)
lookupGE k = fmap (first toEnum) . I.lookupGE (fromEnum k) . unWrap
empty :: EnumMap k a
empty = EnumMap $ I.empty
singleton :: (Enum k) => k -> a -> EnumMap k a
singleton k = EnumMap . I.singleton (fromEnum k)
insert :: (Enum k) => k -> a -> EnumMap k a -> EnumMap k a
insert k a = EnumMap . I.insert (fromEnum k) a . unWrap
insertWith :: (Enum k) => (a -> a -> a) -> k -> a -> EnumMap k a -> EnumMap k a
insertWith f k a = EnumMap . I.insertWith f (fromEnum k) a . unWrap
insertWithKey :: (Enum k)
=> (k -> a -> a -> a) -> k -> a -> EnumMap k a -> EnumMap k a
insertWithKey f k a = EnumMap . I.insertWithKey (f . toEnum) (fromEnum k) a . unWrap
insertLookupWithKey :: (Enum k)
=> (k -> a -> a -> a) -> k -> a -> EnumMap k a -> (Maybe a, EnumMap k a)
insertLookupWithKey f k a =
second EnumMap . I.insertLookupWithKey (f . toEnum) (fromEnum k) a . unWrap
delete :: (Enum k) => k -> EnumMap k a -> EnumMap k a
delete k = EnumMap . I.delete (fromEnum k) . unWrap
adjust :: (Enum k) => (a -> a) -> k -> EnumMap k a -> EnumMap k a
adjust f k = EnumMap . I.adjust f (fromEnum k) . unWrap
adjustWithKey :: (Enum k) => (k -> a -> a) -> k -> EnumMap k a -> EnumMap k a
adjustWithKey f k = EnumMap . I.adjustWithKey (f . toEnum) (fromEnum k) . unWrap
update :: (Enum k) => (a -> Maybe a) -> k -> EnumMap k a -> EnumMap k a
update f k = EnumMap . I.update f (fromEnum k) . unWrap
updateWithKey :: (Enum k) => (k -> a -> Maybe a) -> k -> EnumMap k a -> EnumMap k a
updateWithKey f k = EnumMap . I.updateWithKey (f . toEnum) (fromEnum k) . unWrap
updateLookupWithKey :: (Enum k)
=> (k -> a -> Maybe a) -> k -> EnumMap k a -> (Maybe a,EnumMap k a)
updateLookupWithKey f k =
second EnumMap . I.updateLookupWithKey (f . toEnum) (fromEnum k) . unWrap
alter :: (Enum k) => (Maybe a -> Maybe a) -> k -> EnumMap k a -> EnumMap k a
alter f k = EnumMap . I.alter f (fromEnum k) . unWrap
unions :: [EnumMap k a] -> EnumMap k a
unions = EnumMap . I.unions . P.map unWrap
unionsWith :: (a -> a -> a) -> [EnumMap k a] -> EnumMap k a
unionsWith f = EnumMap . I.unionsWith f . P.map unWrap
union :: EnumMap k a -> EnumMap k a -> EnumMap k a
union (EnumMap im1) (EnumMap im2) = EnumMap $ I.union im1 im2
unionWith :: (a -> a -> a) -> EnumMap k a -> EnumMap k a -> EnumMap k a
unionWith f (EnumMap im1) (EnumMap im2) = EnumMap $ I.unionWith f im1 im2
unionWithKey :: (Enum k)
=> (k -> a -> a -> a) -> EnumMap k a -> EnumMap k a -> EnumMap k a
unionWithKey f (EnumMap im1) (EnumMap im2) =
EnumMap $ I.unionWithKey (f . toEnum) im1 im2
difference :: EnumMap k a -> EnumMap k b -> EnumMap k a
difference (EnumMap im1) (EnumMap im2) = EnumMap $ I.difference im1 im2
differenceWith :: (a -> b -> Maybe a) -> EnumMap k a -> EnumMap k b -> EnumMap k a
differenceWith f (EnumMap im1) (EnumMap im2) =
EnumMap $ I.differenceWith f im1 im2
differenceWithKey :: (Enum k)
=> (k -> a -> b -> Maybe a) -> EnumMap k a -> EnumMap k b -> EnumMap k a
differenceWithKey f (EnumMap im1) (EnumMap im2) =
EnumMap $ I.differenceWithKey (f . toEnum) im1 im2
intersection :: EnumMap k a -> EnumMap k b -> EnumMap k a
intersection (EnumMap im1) (EnumMap im2) = EnumMap $ I.intersection im1 im2
intersectionWith :: (a -> b -> c) -> EnumMap k a -> EnumMap k b -> EnumMap k c
intersectionWith f (EnumMap im1) (EnumMap im2) =
EnumMap $ I.intersectionWith f im1 im2
intersectionWithKey :: (Enum k)
=> (k -> a -> b -> c) -> EnumMap k a -> EnumMap k b -> EnumMap k c
intersectionWithKey f (EnumMap im1) (EnumMap im2) =
EnumMap $ I.intersectionWithKey (f . toEnum) im1 im2
mergeWithKey :: (Enum k)
=> (k -> a -> b -> Maybe c)
-> (EnumMap k a -> EnumMap k c)
-> (EnumMap k b -> EnumMap k c)
-> EnumMap k a
-> EnumMap k b
-> EnumMap k c
mergeWithKey f ga gb = \ma mb -> EnumMap $
I.mergeWithKey (f . toEnum)
(unWrap . ga . EnumMap)
(unWrap . gb . EnumMap)
(unWrap ma)
(unWrap mb)
updateMinWithKey :: (Enum k) => (k -> a -> Maybe a) -> EnumMap k a -> EnumMap k a
updateMinWithKey f = EnumMap . I.updateMinWithKey (f . toEnum) . unWrap
updateMaxWithKey :: (Enum k) => (k -> a -> Maybe a) -> EnumMap k a -> EnumMap k a
updateMaxWithKey f = EnumMap . I.updateMaxWithKey (f . toEnum) . unWrap
maxViewWithKey :: (Enum k) => EnumMap k a -> Maybe ((k, a), EnumMap k a)
maxViewWithKey = fmap wrap . I.maxViewWithKey . unWrap
where
wrap ((i, a), im) = ((toEnum i, a), EnumMap im)
minViewWithKey :: (Enum k) => EnumMap k a -> Maybe ((k, a), EnumMap k a)
minViewWithKey = fmap wrap . I.minViewWithKey . unWrap
where
wrap ((i, a), imap) = ((toEnum i, a), EnumMap imap)
updateMax :: (a -> Maybe a) -> EnumMap k a -> EnumMap k a
updateMax f = EnumMap . I.updateMax f . unWrap
updateMin :: (a -> Maybe a) -> EnumMap k a -> EnumMap k a
updateMin f = EnumMap . I.updateMin f . unWrap
maxView :: EnumMap k a -> Maybe (a, EnumMap k a)
maxView = fmap (second EnumMap) . I.maxView . unWrap
minView :: EnumMap k a -> Maybe (a, EnumMap k a)
minView = fmap (second EnumMap) . I.minView . unWrap
deleteFindMax :: (Enum k) => EnumMap k a -> ((k, a), EnumMap k a)
deleteFindMax = (first toEnum *** EnumMap) . I.deleteFindMax . unWrap
deleteFindMin :: (Enum k) => EnumMap k a -> ((k, a), EnumMap k a)
deleteFindMin = (first toEnum *** EnumMap) . I.deleteFindMin . unWrap
findMin :: (Enum k) => EnumMap k a -> (k, a)
findMin = first toEnum . I.findMin . unWrap
findMax :: (Enum k) => EnumMap k a -> (k, a)
findMax = first toEnum . I.findMax . unWrap
deleteMin :: EnumMap k a -> EnumMap k a
deleteMin = EnumMap . I.deleteMin . unWrap
deleteMax :: EnumMap k a -> EnumMap k a
deleteMax = EnumMap . I.deleteMax . unWrap
isProperSubmapOf :: (Eq a) => EnumMap k a -> EnumMap k a -> Bool
isProperSubmapOf (EnumMap im1) (EnumMap im2) = I.isProperSubmapOf im1 im2
isProperSubmapOfBy :: (a -> b -> Bool) -> EnumMap k a -> EnumMap k b -> Bool
isProperSubmapOfBy p (EnumMap im1) (EnumMap im2) = I.isProperSubmapOfBy p im1 im2
isSubmapOf :: Eq a => EnumMap k a -> EnumMap k a -> Bool
isSubmapOf (EnumMap im1) (EnumMap im2) = I.isSubmapOf im1 im2
isSubmapOfBy :: (a -> b -> Bool) -> EnumMap k a -> EnumMap k b -> Bool
isSubmapOfBy p (EnumMap im1) (EnumMap im2) = I.isSubmapOfBy p im1 im2
map :: (a -> b) -> EnumMap k a -> EnumMap k b
map f = EnumMap . I.map f . unWrap
mapWithKey :: (Enum k) => (k -> a -> b) -> EnumMap k a -> EnumMap k b
mapWithKey f = EnumMap . I.mapWithKey (f . toEnum) . unWrap
traverseWithKey :: (Applicative t, Enum k)
=> (k -> a -> t b) -> EnumMap k a -> t (EnumMap k b)
traverseWithKey f = liftA EnumMap . I.traverseWithKey (f . toEnum) . unWrap
mapAccum :: (a -> b -> (a, c)) -> a -> EnumMap k b -> (a, EnumMap k c)
mapAccum f a = second EnumMap . I.mapAccum f a . unWrap
mapAccumWithKey :: (Enum k)
=> (a -> k -> b -> (a, c)) -> a -> EnumMap k b -> (a, EnumMap k c)
mapAccumWithKey f a =
second EnumMap . I.mapAccumWithKey (\b -> f b . toEnum) a . unWrap
mapAccumRWithKey :: (Enum k)
=> (a -> k -> b -> (a, c)) -> a -> EnumMap k b -> (a, EnumMap k c)
mapAccumRWithKey f a =
second EnumMap . I.mapAccumRWithKey (\b -> f b . toEnum) a . unWrap
mapKeys :: (Enum k) => (k -> k) -> EnumMap k a -> EnumMap k a
mapKeys f = EnumMap . I.mapKeys (fromEnum . f . toEnum) . unWrap
mapKeysWith :: (Enum k) => (a -> a -> a) -> (k -> k) -> EnumMap k a -> EnumMap k a
mapKeysWith f g = EnumMap . I.mapKeysWith f (fromEnum . g . toEnum) . unWrap
mapKeysMonotonic :: (Enum k) => (k -> k) -> EnumMap k a -> EnumMap k a
mapKeysMonotonic f = EnumMap . I.mapKeysMonotonic (fromEnum . f . toEnum) . unWrap
filter :: (a -> Bool) -> EnumMap k a -> EnumMap k a
filter p = EnumMap . I.filter p . unWrap
filterWithKey :: (Enum k) => (k -> a -> Bool) -> EnumMap k a -> EnumMap k a
filterWithKey p = EnumMap . I.filterWithKey (p . toEnum) . unWrap
partition :: (a -> Bool) -> EnumMap k a -> (EnumMap k a, EnumMap k a)
partition p = (EnumMap *** EnumMap) . I.partition p . unWrap
partitionWithKey :: (Enum k)
=> (k -> a -> Bool) -> EnumMap k a -> (EnumMap k a, EnumMap k a)
partitionWithKey p =
(EnumMap *** EnumMap) . I.partitionWithKey (p . toEnum) . unWrap
mapMaybe :: (a -> Maybe b) -> EnumMap k a -> EnumMap k b
mapMaybe f = EnumMap . I.mapMaybe f . unWrap
mapMaybeWithKey :: (Enum k) => (k -> a -> Maybe b) -> EnumMap k a -> EnumMap k b
mapMaybeWithKey f = EnumMap . I.mapMaybeWithKey (f . toEnum) . unWrap
mapEither :: (a -> Either b c) -> EnumMap k a -> (EnumMap k b, EnumMap k c)
mapEither f = (EnumMap *** EnumMap) . I.mapEither f . unWrap
mapEitherWithKey :: (Enum k)
=> (k -> a -> Either b c) -> EnumMap k a -> (EnumMap k b, EnumMap k c)
mapEitherWithKey f =
(EnumMap *** EnumMap) . I.mapEitherWithKey (f . toEnum) . unWrap
split :: (Enum k) => k -> EnumMap k a -> (EnumMap k a, EnumMap k a)
split k = (EnumMap *** EnumMap) . I.split (fromEnum k) . unWrap
splitLookup :: (Enum k) => k -> EnumMap k a -> (EnumMap k a, Maybe a, EnumMap k a)
splitLookup k = wrap . I.splitLookup (fromEnum k) . unWrap
where
wrap (im1, ma, im2) = (EnumMap im1, ma, EnumMap im2)
foldr :: (a -> b -> b) -> b -> EnumMap k a -> b
foldr f a = I.foldr f a . unWrap
foldl :: (a -> b -> a) -> a -> EnumMap k b -> a
foldl f a = I.foldl f a . unWrap
foldrWithKey :: (Enum k) => (k -> a -> b -> b) -> b -> EnumMap k a -> b
foldrWithKey f a = I.foldrWithKey (f . toEnum) a . unWrap
foldlWithKey :: (Enum k) => (a -> k -> b -> a) -> a -> EnumMap k b -> a
foldlWithKey f a = I.foldlWithKey (\a' -> f a' . toEnum) a . unWrap
foldr' :: (a -> b -> b) -> b -> EnumMap k a -> b
foldr' f a = I.foldr' f a . unWrap
foldl' :: (a -> b -> a) -> a -> EnumMap k b -> a
foldl' f a = I.foldl' f a . unWrap
foldrWithKey' :: (Enum k) => (k -> a -> b -> b) -> b -> EnumMap k a -> b
foldrWithKey' f a = I.foldrWithKey' (f . toEnum) a . unWrap
foldlWithKey' :: (Enum k) => (a -> k -> b -> a) -> a -> EnumMap k b -> a
foldlWithKey' f a = I.foldlWithKey' (\a' -> f a' . toEnum) a . unWrap
elems :: EnumMap k a -> [a]
elems = I.elems . unWrap
keys :: (Enum k) => EnumMap k a -> [k]
keys = P.map toEnum . I.keys . unWrap
keysSet :: (Enum k) => EnumMap k a -> EnumSet k
keysSet = EnumSet.fromDistinctAscList . keys
fromSet :: (Enum k) => (k -> a) -> EnumSet k -> EnumMap k a
fromSet f = EnumMap . I.fromSet (f . toEnum) . EnumSet.enumSetToIntSet
assocs :: (Enum k) => EnumMap k a -> [(k, a)]
assocs = P.map (first toEnum) . I.assocs . unWrap
toList :: (Enum k) => EnumMap k a -> [(k, a)]
toList = P.map (first toEnum) . I.toList . unWrap
toAscList :: (Enum k) => EnumMap k a -> [(k, a)]
toAscList = P.map (first toEnum) . I.toAscList . unWrap
toDescList :: (Enum k) => EnumMap k a -> [(k, a)]
toDescList = P.map (first toEnum) . I.toDescList . unWrap
fromList :: (Enum k) => [(k, a)] -> EnumMap k a
fromList = EnumMap . I.fromList . P.map (first fromEnum)
fromListWith :: (Enum k) => (a -> a -> a) -> [(k, a)] -> EnumMap k a
fromListWith f = EnumMap . I.fromListWith f . P.map (first fromEnum)
fromListWithKey :: (Enum k) => (k -> a -> a -> a) -> [(k, a)] -> EnumMap k a
fromListWithKey f =
EnumMap . I.fromListWithKey (f . toEnum) . P.map (first fromEnum)
fromAscList :: (Enum k) => [(k, a)] -> EnumMap k a
fromAscList = EnumMap . I.fromAscList . P.map (first fromEnum)
fromAscListWith :: (Enum k) => (a -> a -> a) -> [(k, a)] -> EnumMap k a
fromAscListWith f = EnumMap . I.fromAscListWith f . P.map (first fromEnum)
fromAscListWithKey :: (Enum k) => (k -> a -> a -> a) -> [(k, a)] -> EnumMap k a
fromAscListWithKey f =
EnumMap . I.fromAscListWithKey (f . toEnum) . P.map (first fromEnum)
fromDistinctAscList :: (Enum k) => [(k, a)] -> EnumMap k a
fromDistinctAscList = EnumMap . I.fromDistinctAscList . P.map (first fromEnum)