module Data.UnionMap.Internal where
import Control.Applicative hiding (empty)
import Control.Monad
import qualified Data.List as L
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe
import Data.Monoid
import Data.OpenUnion
import Prelude hiding (lookup)
newtype UnionMap k r = UnionMap (Map k (Union r))
deriving (Monoid, Show)
null :: UnionMap k r -> Bool
null (UnionMap m) = M.null m
empty :: UnionMap k r
empty = UnionMap M.empty
size :: UnionMap k r -> Int
size (UnionMap m) = M.size m
member :: Ord k => k -> UnionMap k r -> Bool
member k (UnionMap m) = M.member k m
notMember :: Ord k => k -> UnionMap k r -> Bool
notMember k = not . member k
singleton :: (Ord k, Member a as) => k -> a -> UnionMap k as
singleton k x = insert k x empty
liftUM :: (Map k (Union r) -> Map k (Union s)) -> UnionMap k r -> UnionMap k s
liftUM f (UnionMap m) = UnionMap $ f m
liftUM2 :: (Map k (Union r) -> Map k (Union s) -> Map k (Union t)) -> UnionMap k r -> UnionMap k s -> UnionMap k t
liftUM2 f (UnionMap m1) (UnionMap m2) = UnionMap $ f m1 m2
lookup :: (Ord k, Member a as) => k -> UnionMap k as -> Maybe a
lookup k (UnionMap m) = M.lookup k m >>= retractU
lookupU :: Ord k => k -> UnionMap k r -> Maybe (Union r)
lookupU k (UnionMap m) = M.lookup k m
find :: (Ord k, Member a as) => k -> UnionMap k as -> a
find k um = flip fromMaybe (lookup k um)
$ error "UnionMap.find: given key is not an element in the map, or is not associated a value of the type expected."
findU :: Ord k => k -> UnionMap k r -> Union r
findU k um = flip fromMaybe (lookupU k um)
$ error "UnionMap.findU: given key is not an element in the map, or is not associated a value of the type expected."
findWithDefault :: (Ord k, Member a as) => a -> k -> UnionMap k as -> a
findWithDefault def k um = fromMaybe def $ lookup k um
(!) :: (Ord k, Member a as) => UnionMap k as -> k -> a
um ! k = flip fromMaybe (lookup k um)
$ error "UnionMap.!: given key is not an element in the map, or is not associated a value of the type expected."
insert :: (Ord k, Member a as) => k -> a -> UnionMap k as -> UnionMap k as
insert k x = liftUM (M.insert k (liftU x))
insertWith :: (Ord k, Member a as) => (a -> a -> a) -> k -> a -> UnionMap k as -> UnionMap k as
insertWith f = insertWithKey (\_ y z -> f y z)
insertWithKey :: (Ord k, Member a as) => (k -> a -> a -> a) -> k -> a -> UnionMap k as -> UnionMap k as
insertWithKey f k x = liftUM (M.insertWithKey go k (liftU x))
where
go k' _ s = maybe (liftU x) liftU $ f <$> pure k' <*> return x <*> retractU s
delete :: Ord k => k -> UnionMap k as -> UnionMap k as
delete = liftUM . M.delete
adjust :: (Ord k, Member a as) => (a -> a) -> k -> UnionMap k as -> UnionMap k as
adjust f = adjustWithKey (\_ x -> f x)
adjustWithKey :: (Ord k, Member a as) => (k -> a -> a) -> k -> UnionMap k as -> UnionMap k as
adjustWithKey f k = liftUM (M.adjust (hoistU (f k)) k)
update :: (Ord k, Member a as) => (a -> Maybe a) -> k -> UnionMap k as -> UnionMap k as
update f = updateWithKey (\_ x -> f x)
updateWithKey :: (Ord k, Member a as) => (k -> a -> Maybe a) -> k -> UnionMap k as -> UnionMap k as
updateWithKey f k = liftUM (M.update (retractU >=> f k >=> return . liftU) k)
union :: Ord k => UnionMap k r -> UnionMap k r -> UnionMap k r
union = liftUM2 M.union
unions :: Ord k => [UnionMap k r] -> UnionMap k r
unions = L.foldl' union empty
difference :: Ord k => UnionMap k r -> UnionMap k r -> UnionMap k r
difference = liftUM2 M.difference
(\\) :: Ord k => UnionMap k r -> UnionMap k r -> UnionMap k r
(\\) = difference
intersection :: Ord k => UnionMap k r -> UnionMap k r -> UnionMap k r
intersection = liftUM2 M.intersection
keys :: UnionMap k r -> [k]
keys (UnionMap m) = M.keys m
map :: Member a as => (a -> a) -> UnionMap k as -> UnionMap k as
map f = mapWithKey (\_ x -> f x)
mapWithKey :: Member a as => (k -> a -> a) -> UnionMap k as -> UnionMap k as
mapWithKey f = liftUM (M.mapWithKey (hoistU . f))
mapU :: (Union r -> Union s) -> UnionMap k r -> UnionMap k s
mapU f = mapWithKeyU (\_ u -> f u)
mapWithKeyU :: (k -> Union r -> Union s) -> UnionMap k r -> UnionMap k s
mapWithKeyU = liftUM . M.mapWithKey
mapU' :: (Union r -> a) -> UnionMap k r -> Map k a
mapU' f = mapWithKeyU' (\_ u -> f u)
mapWithKeyU' :: (k -> Union r -> a) -> UnionMap k r -> Map k a
mapWithKeyU' f (UnionMap m) = M.mapWithKey f m
rebuild :: Include r s => UnionMap k r -> UnionMap k s
rebuild = mapU reunion
filterU :: (Union r -> Bool) -> UnionMap k r -> UnionMap k r
filterU p = filterWithKeyU (\_ u -> p u)
filterWithKeyU :: (k -> Union r -> Bool) -> UnionMap k r -> UnionMap k r
filterWithKeyU = liftUM . M.filterWithKey
foldrU :: (Union r -> b -> b) -> b -> UnionMap k r -> b
foldrU f = foldrWithKeyU (\_ u z -> f u z)
foldrWithKeyU :: (k -> Union r -> b -> b) -> b -> UnionMap k r -> b
foldrWithKeyU f z (UnionMap m) = M.foldrWithKey f z m
foldlU' :: (a -> Union r -> a) -> a -> UnionMap k r -> a
foldlU' f = foldlWithKeyU' (\z _ u -> f z u)
foldlWithKeyU' :: (a -> k -> Union r -> a) -> a -> UnionMap k r -> a
foldlWithKeyU' f z (UnionMap m) = M.foldlWithKey' f z m
showTree :: (Show k, Show (Union r)) => UnionMap k r -> String
showTree (UnionMap m) = M.showTree m