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