{-# OPTIONS_HADDOCK not-home #-}
module Data.Map.Merge.Set.Internal
( WhenMatched(..)
, SimpleWhenMatched
, dropMatched
, filterMatched
, filterAMatched
, WhenMissingSet(..)
, SimpleWhenMissingSet
, dropMissingSet
, merge
, mergeA
, runWhenMatched
, runWhenMissingSet
) where
import Control.Applicative (liftA3)
import Data.Functor.Identity (Identity(..))
import Data.Set (Set)
import qualified Data.Set.Internal as S
import Data.Map (Map)
import qualified Data.Map.Internal as M
newtype WhenMatched f k a b = WhenMatched
{ forall (f :: * -> *) k a b.
WhenMatched f k a b -> k -> a -> f (Maybe b)
matchedKey :: k -> a -> f (Maybe b)
}
runWhenMatched :: WhenMatched f k a b -> k -> a -> f (Maybe b)
runWhenMatched :: forall (f :: * -> *) k a b.
WhenMatched f k a b -> k -> a -> f (Maybe b)
runWhenMatched = WhenMatched f k a b -> k -> a -> f (Maybe b)
forall (f :: * -> *) k a b.
WhenMatched f k a b -> k -> a -> f (Maybe b)
matchedKey
type SimpleWhenMatched = WhenMatched Identity
dropMatched :: Applicative f => WhenMatched f k a b
dropMatched :: forall (f :: * -> *) k a b. Applicative f => WhenMatched f k a b
dropMatched = (k -> a -> f (Maybe b)) -> WhenMatched f k a b
forall (f :: * -> *) k a b.
(k -> a -> f (Maybe b)) -> WhenMatched f k a b
WhenMatched (\k
_ a
_ -> Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing)
{-# INLINE dropMatched #-}
filterMatched :: Applicative f => (k -> a -> Bool) -> WhenMatched f k a a
filterMatched :: forall (f :: * -> *) k a.
Applicative f =>
(k -> a -> Bool) -> WhenMatched f k a a
filterMatched k -> a -> Bool
f =
(k -> a -> f (Maybe a)) -> WhenMatched f k a a
forall (f :: * -> *) k a b.
(k -> a -> f (Maybe b)) -> WhenMatched f k a b
WhenMatched (\k
k a
x -> if k -> a -> Bool
f k
k a
x then Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Maybe a
forall a. a -> Maybe a
Just a
x) else Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing)
{-# INLINE filterMatched #-}
filterAMatched :: Functor f => (k -> a -> f Bool) -> WhenMatched f k a a
filterAMatched :: forall (f :: * -> *) k a.
Functor f =>
(k -> a -> f Bool) -> WhenMatched f k a a
filterAMatched k -> a -> f Bool
f =
(k -> a -> f (Maybe a)) -> WhenMatched f k a a
forall (f :: * -> *) k a b.
(k -> a -> f (Maybe b)) -> WhenMatched f k a b
WhenMatched (\k
k a
x -> (\Bool
b -> if Bool
b then a -> Maybe a
forall a. a -> Maybe a
Just a
x else Maybe a
forall a. Maybe a
Nothing) (Bool -> Maybe a) -> f Bool -> f (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> f Bool
f k
k a
x)
{-# INLINE filterAMatched #-}
data WhenMissingSet f k a = WhenMissingSet
{ forall (f :: * -> *) k a.
WhenMissingSet f k a -> Set k -> f (Map k a)
missingSubtree :: Set k -> f (Map k a)
, forall (f :: * -> *) k a. WhenMissingSet f k a -> k -> f (Maybe a)
missingKey :: k -> f (Maybe a)
}
runWhenMissingSet :: WhenMissingSet f k a -> k -> f (Maybe a)
runWhenMissingSet :: forall (f :: * -> *) k a. WhenMissingSet f k a -> k -> f (Maybe a)
runWhenMissingSet = WhenMissingSet f k a -> k -> f (Maybe a)
forall (f :: * -> *) k a. WhenMissingSet f k a -> k -> f (Maybe a)
missingKey
type SimpleWhenMissingSet = WhenMissingSet Identity
dropMissingSet :: Applicative f => WhenMissingSet f k a
dropMissingSet :: forall (f :: * -> *) k a. Applicative f => WhenMissingSet f k a
dropMissingSet = WhenMissingSet
{ missingSubtree :: Set k -> f (Map k a)
missingSubtree = \Set k
_ -> Map k a -> f (Map k a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map k a
forall k a. Map k a
M.empty
, missingKey :: k -> f (Maybe a)
missingKey = \k
_ -> Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
}
{-# INLINE dropMissingSet #-}
merge
:: Ord k
=> M.SimpleWhenMissing k a b
-> SimpleWhenMissingSet k b
-> SimpleWhenMatched k a b
-> Map k a
-> Set k
-> Map k b
merge :: forall k a b.
Ord k =>
SimpleWhenMissing k a b
-> SimpleWhenMissingSet k b
-> SimpleWhenMatched k a b
-> Map k a
-> Set k
-> Map k b
merge SimpleWhenMissing k a b
miss1 SimpleWhenMissingSet k b
miss2 SimpleWhenMatched k a b
match = \Map k a
t1 Set k
t2 -> Identity (Map k b) -> Map k b
forall a. Identity a -> a
runIdentity (SimpleWhenMissing k a b
-> SimpleWhenMissingSet k b
-> SimpleWhenMatched k a b
-> Map k a
-> Set k
-> Identity (Map k b)
forall (f :: * -> *) k a b.
(Applicative f, Ord k) =>
WhenMissing f k a b
-> WhenMissingSet f k b
-> WhenMatched f k a b
-> Map k a
-> Set k
-> f (Map k b)
mergeA SimpleWhenMissing k a b
miss1 SimpleWhenMissingSet k b
miss2 SimpleWhenMatched k a b
match Map k a
t1 Set k
t2)
{-# INLINE merge #-}
mergeA
:: (Applicative f, Ord k)
=> M.WhenMissing f k a b
-> WhenMissingSet f k b
-> WhenMatched f k a b
-> Map k a
-> Set k
-> f (Map k b)
mergeA :: forall (f :: * -> *) k a b.
(Applicative f, Ord k) =>
WhenMissing f k a b
-> WhenMissingSet f k b
-> WhenMatched f k a b
-> Map k a
-> Set k
-> f (Map k b)
mergeA
M.WhenMissing{missingSubtree :: forall (f :: * -> *) k x y.
WhenMissing f k x y -> Map k x -> f (Map k y)
M.missingSubtree = Map k a -> f (Map k b)
g1t, missingKey :: forall (f :: * -> *) k x y.
WhenMissing f k x y -> k -> x -> f (Maybe y)
M.missingKey = k -> a -> f (Maybe b)
g1k}
WhenMissingSet{missingSubtree :: forall (f :: * -> *) k a.
WhenMissingSet f k a -> Set k -> f (Map k a)
missingSubtree = Set k -> f (Map k b)
g2t}
WhenMatched{matchedKey :: forall (f :: * -> *) k a b.
WhenMatched f k a b -> k -> a -> f (Maybe b)
matchedKey = k -> a -> f (Maybe b)
f} = Map k a -> Set k -> f (Map k b)
go
where
go :: Map k a -> Set k -> f (Map k b)
go Map k a
t1 Set k
S.Tip = Map k a -> f (Map k b)
g1t Map k a
t1
go Map k a
M.Tip Set k
t2 = Set k -> f (Map k b)
g2t Set k
t2
go (M.Bin Size
_ k
k1 a
x1 Map k a
l1 Map k a
r1) Set k
t2 = case k -> Set k -> (Set k, Bool, Set k)
forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
S.splitMember k
k1 Set k
t2 of
(Set k
l2, Bool
found, Set k
r2) ->
(Map k b -> Maybe b -> Map k b -> Map k b)
-> f (Map k b) -> f (Maybe b) -> f (Map k b) -> f (Map k b)
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3
(\Map k b
l' Maybe b
mx' Map k b
r' -> (Map k b -> Map k b -> Map k b)
-> (b -> Map k b -> Map k b -> Map k b)
-> Maybe b
-> Map k b
-> Map k b
-> Map k b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map k b -> Map k b -> Map k b
forall k a. Map k a -> Map k a -> Map k a
M.link2 (k -> b -> Map k b -> Map k b -> Map k b
forall k a. k -> a -> Map k a -> Map k a -> Map k a
M.link k
k1) Maybe b
mx' Map k b
l' Map k b
r')
(Map k a -> Set k -> f (Map k b)
go Map k a
l1 Set k
l2)
(if Bool
found then k -> a -> f (Maybe b)
f k
k1 a
x1 else k -> a -> f (Maybe b)
g1k k
k1 a
x1)
(Map k a -> Set k -> f (Map k b)
go Map k a
r1 Set k
r2)
{-# INLINE mergeA #-}