{-# LANGUAGE DerivingStrategies #-}
module AtCoder.Extra.IntervalMap
(
IntervalMap,
new,
build,
buildM,
capacity,
contains,
intersects,
lookup,
read,
readMaybe,
insert,
insertM,
delete,
deleteM,
overwrite,
overwriteM,
freeze,
)
where
import AtCoder.Extra.IntMap qualified as IM
import Control.Monad (foldM_)
import Control.Monad.Primitive (PrimMonad, PrimState)
import Data.Vector.Generic qualified as G
import Data.Vector.Unboxed qualified as VU
import GHC.Stack (HasCallStack)
import Prelude hiding (lookup, read)
newtype IntervalMap s a = IntervalMap
{
forall s a. IntervalMap s a -> IntMap s (Int, a)
unITM :: IM.IntMap s (Int, a)
}
new :: (PrimMonad m, VU.Unbox a) => Int -> m (IntervalMap (PrimState m) a)
new :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (IntervalMap (PrimState m) a)
new = (IntMap (PrimState m) (Int, a) -> IntervalMap (PrimState m) a)
-> m (IntMap (PrimState m) (Int, a))
-> m (IntervalMap (PrimState m) a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap IntMap (PrimState m) (Int, a) -> IntervalMap (PrimState m) a
forall s a. IntMap s (Int, a) -> IntervalMap s a
IntervalMap (m (IntMap (PrimState m) (Int, a))
-> m (IntervalMap (PrimState m) a))
-> (Int -> m (IntMap (PrimState m) (Int, a)))
-> Int
-> m (IntervalMap (PrimState m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> m (IntMap (PrimState m) (Int, a))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (IntMap (PrimState m) a)
IM.new
build :: (PrimMonad m, Eq a, VU.Unbox a) => VU.Vector a -> m (IntervalMap (PrimState m) a)
build :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
Vector a -> m (IntervalMap (PrimState m) a)
build Vector a
xs = Vector a
-> (Int -> Int -> a -> m ()) -> m (IntervalMap (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
Vector a
-> (Int -> Int -> a -> m ()) -> m (IntervalMap (PrimState m) a)
buildM Vector a
xs Int -> Int -> a -> m ()
forall {f :: * -> *} {p} {p} {p}.
Applicative f =>
p -> p -> p -> f ()
onAdd
where
onAdd :: p -> p -> p -> f ()
onAdd p
_ p
_ p
_ = () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
buildM ::
(PrimMonad m, Eq a, VU.Unbox a) =>
VU.Vector a ->
(Int -> Int -> a -> m ()) ->
m (IntervalMap (PrimState m) a)
buildM :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
Vector a
-> (Int -> Int -> a -> m ()) -> m (IntervalMap (PrimState m) a)
buildM Vector a
xs Int -> Int -> a -> m ()
onAdd = do
IntMap (PrimState m) (Int, a)
dim <- Int -> m (IntMap (PrimState m) (Int, a))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (IntMap (PrimState m) a)
IM.new (Vector a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length Vector a
xs)
(Int -> Vector a -> m Int) -> Int -> [Vector a] -> m ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ (IntMap (PrimState m) (Int, a) -> Int -> Vector a -> m Int
step IntMap (PrimState m) (Int, a)
dim) (Int
0 :: Int) ([Vector a] -> m ()) -> [Vector a] -> m ()
forall a b. (a -> b) -> a -> b
$ Vector a -> [Vector a]
forall (v :: * -> *) a. (Vector v a, Eq a) => v a -> [v a]
G.group Vector a
xs
IntervalMap (PrimState m) a -> m (IntervalMap (PrimState m) a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IntervalMap (PrimState m) a -> m (IntervalMap (PrimState m) a))
-> IntervalMap (PrimState m) a -> m (IntervalMap (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ IntMap (PrimState m) (Int, a) -> IntervalMap (PrimState m) a
forall s a. IntMap s (Int, a) -> IntervalMap s a
IntervalMap IntMap (PrimState m) (Int, a)
dim
where
step :: IntMap (PrimState m) (Int, a) -> Int -> Vector a -> m Int
step IntMap (PrimState m) (Int, a)
dim !Int
l !Vector a
xs' = do
let !l' :: Int
l' = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Vector a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length Vector a
xs'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l (Int
l', Vector a -> a
forall (v :: * -> *) a. Vector v a => v a -> a
G.head Vector a
xs')
Int -> Int -> a -> m ()
onAdd Int
l Int
l' (Vector a -> a
forall (v :: * -> *) a. Vector v a => v a -> a
G.head Vector a
xs')
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
l'
{-# INLINE capacity #-}
capacity :: IntervalMap s a -> Int
capacity :: forall s a. IntervalMap s a -> Int
capacity = IntMap s (Int, a) -> Int
forall s a. IntMap s a -> Int
IM.capacity (IntMap s (Int, a) -> Int)
-> (IntervalMap s a -> IntMap s (Int, a)) -> IntervalMap s a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntervalMap s a -> IntMap s (Int, a)
forall s a. IntervalMap s a -> IntMap s (Int, a)
unITM
contains :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> m Bool
contains :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> m Bool
contains IntervalMap (PrimState m) a
itm Int
i = IntervalMap (PrimState m) a -> Int -> Int -> m Bool
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m Bool
intersects IntervalMap (PrimState m) a
itm Int
i (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
intersects :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> m Bool
intersects :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m Bool
intersects (IntervalMap IntMap (PrimState m) (Int, a)
dim) Int
l Int
r
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r = Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
| Bool
otherwise = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupLE IntMap (PrimState m) (Int, a)
dim Int
l
Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ case Maybe (Int, (Int, a))
res of
Just (!Int
_, (!Int
r', !a
_)) -> Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r'
Maybe (Int, (Int, a))
_ -> Bool
False
lookup :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> m (Maybe (Int, Int, a))
lookup :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a
-> Int -> Int -> m (Maybe (Int, Int, a))
lookup (IntervalMap IntMap (PrimState m) (Int, a)
im) Int
l Int
r
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r = Maybe (Int, Int, a) -> m (Maybe (Int, Int, a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Int, Int, a)
forall a. Maybe a
Nothing
| Bool
otherwise = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupLE IntMap (PrimState m) (Int, a)
im Int
l
Maybe (Int, Int, a) -> m (Maybe (Int, Int, a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Int, Int, a) -> m (Maybe (Int, Int, a)))
-> Maybe (Int, Int, a) -> m (Maybe (Int, Int, a))
forall a b. (a -> b) -> a -> b
$ case Maybe (Int, (Int, a))
res of
Just (!Int
l', (!Int
r', !a
a))
| Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r' -> (Int, Int, a) -> Maybe (Int, Int, a)
forall a. a -> Maybe a
Just (Int
l', Int
r', a
a)
Maybe (Int, (Int, a))
_ -> Maybe (Int, Int, a)
forall a. Maybe a
Nothing
read :: (HasCallStack, PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> m a
read :: forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m a
read IntervalMap (PrimState m) a
itm Int
l Int
r = do
Maybe a
res <- IntervalMap (PrimState m) a -> Int -> Int -> m (Maybe a)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m (Maybe a)
readMaybe IntervalMap (PrimState m) a
itm Int
l Int
r
a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ case Maybe a
res of
Just !a
a -> a
a
Maybe a
Nothing -> [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"[read] not a member: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (Int, Int) -> [Char]
forall a. Show a => a -> [Char]
show (Int
l, Int
r)
readMaybe :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> m (Maybe a)
readMaybe :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m (Maybe a)
readMaybe (IntervalMap IntMap (PrimState m) (Int, a)
dim) Int
l Int
r
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r = Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
| Bool
otherwise = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupLE IntMap (PrimState m) (Int, a)
dim Int
l
Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ case Maybe (Int, (Int, a))
res of
Just (!Int
_, (!Int
r', !a
a))
| Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r' -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
Maybe (Int, (Int, a))
_ -> Maybe a
forall a. Maybe a
Nothing
insert :: (PrimMonad m, Eq a, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
insert :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
insert IntervalMap (PrimState m) a
itm Int
l Int
r a
x = IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
insertM IntervalMap (PrimState m) a
itm Int
l Int
r a
x Int -> Int -> a -> m ()
forall {f :: * -> *} {p} {p} {p}.
Applicative f =>
p -> p -> p -> f ()
onAdd Int -> Int -> a -> m ()
forall {f :: * -> *} {p} {p} {p}.
Applicative f =>
p -> p -> p -> f ()
onDel
where
onAdd :: p -> p -> p -> f ()
onAdd p
_ p
_ p
_ = () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
onDel :: p -> p -> p -> f ()
onDel p
_ p
_ p
_ = () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
insertM ::
(PrimMonad m, Eq a, VU.Unbox a) =>
IntervalMap (PrimState m) a ->
Int ->
Int ->
a ->
(Int -> Int -> a -> m ()) ->
(Int -> Int -> a -> m ()) ->
m ()
insertM :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
insertM (IntervalMap IntMap (PrimState m) (Int, a)
dim) Int
l0 Int
r0 a
x Int -> Int -> a -> m ()
onAdd Int -> Int -> a -> m ()
onDel
| Int
l0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r0 = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
!Int
r <- Int -> Int -> m Int
handleRight Int
l0 Int
r0
(!Int
l', !Int
r') <- Int -> Int -> m (Int, Int)
handleLeft Int
l0 Int
r
Int -> Int -> a -> m ()
onAdd Int
l' Int
r' a
x
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l' (Int
r', a
x)
where
handleRight :: Int -> Int -> m Int
handleRight Int
l Int
r = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupGE IntMap (PrimState m) (Int, a)
dim Int
l
case Maybe (Int, (Int, a))
res of
Just interval0 :: (Int, (Int, a))
interval0@(!Int
_, (!Int
_, !a
_)) -> (Int, (Int, a)) -> Int -> Int -> m Int
run (Int, (Int, a))
interval0 Int
l Int
r
Maybe (Int, (Int, a))
Nothing -> Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r
run :: (Int, (Int, a)) -> Int -> Int -> m Int
run (!Int
l', (!Int
r', !a
x')) Int
l Int
r
| Int
l' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
r = do
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r
| Int
l' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
r Bool -> Bool -> Bool
&& a
x' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r'
| Int
l' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
r = do
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupGT IntMap (PrimState m) (Int, a)
dim Int
l'
case Maybe (Int, (Int, a))
res of
Just (Int, (Int, a))
rng -> (Int, (Int, a)) -> Int -> Int -> m Int
run (Int, (Int, a))
rng Int
l Int
r
Maybe (Int, (Int, a))
Nothing -> Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r
| a
x' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r'
| Bool
otherwise = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
r Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
r (Int
r', a
x')
Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
r
handleLeft :: Int -> Int -> m (Int, Int)
handleLeft Int
l Int
r = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupLT IntMap (PrimState m) (Int, a)
dim Int
l
case Maybe (Int, (Int, a))
res of
Maybe (Int, (Int, a))
Nothing -> (Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l, Int
r)
Just (!Int
l', (!Int
r', !a
x'))
| Int
r' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
l Bool -> Bool -> Bool
&& a
x' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l', Int
r)
| Int
r' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
l -> do
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l, Int
r)
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
l -> do
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l, Int
r)
| a
x' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
l Int
l', Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
r Int
r')
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
r -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
l' Int
l a
x'
Int -> Int -> a -> m ()
onAdd Int
r Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l' (Int
l, a
x')
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
r (Int
r', a
x')
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l, Int
r)
| Bool
otherwise -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
l' Int
l a
x'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l' (Int
l, a
x')
(Int, Int) -> m (Int, Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
l, Int
r)
delete :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> m ()
delete :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> m ()
delete IntervalMap (PrimState m) a
itm Int
l Int
r = IntervalMap (PrimState m) a
-> Int
-> Int
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
deleteM IntervalMap (PrimState m) a
itm Int
l Int
r Int -> Int -> a -> m ()
forall {f :: * -> *} {p} {p} {p}.
Applicative f =>
p -> p -> p -> f ()
onAdd Int -> Int -> a -> m ()
forall {f :: * -> *} {p} {p} {p}.
Applicative f =>
p -> p -> p -> f ()
onDel
where
onAdd :: p -> p -> p -> f ()
onAdd p
_ p
_ p
_ = () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
onDel :: p -> p -> p -> f ()
onDel p
_ p
_ p
_ = () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
deleteM ::
(PrimMonad m, VU.Unbox a) =>
IntervalMap (PrimState m) a ->
Int ->
Int ->
(Int -> Int -> a -> m ()) ->
(Int -> Int -> a -> m ()) ->
m ()
deleteM :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
deleteM (IntervalMap IntMap (PrimState m) (Int, a)
dim) Int
l0 Int
r0 Int -> Int -> a -> m ()
onAdd Int -> Int -> a -> m ()
onDel
| Int
l0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r0 = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
Int -> Int -> m ()
handleRight Int
l0 Int
r0
Int -> Int -> m ()
handleLeft Int
l0 Int
r0
where
handleRight :: Int -> Int -> m ()
handleRight Int
l Int
r = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupGE IntMap (PrimState m) (Int, a)
dim Int
l
case Maybe (Int, (Int, a))
res of
Just interval0 :: (Int, (Int, a))
interval0@(!Int
_, (!Int
_, !a
_)) -> (Int, (Int, a)) -> Int -> Int -> m ()
run (Int, (Int, a))
interval0 Int
l Int
r
Maybe (Int, (Int, a))
Nothing -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
run :: (Int, (Int, a)) -> Int -> Int -> m ()
run (!Int
l', (!Int
r', !a
x')) Int
l Int
r
| Int
l' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
r = do
() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupGT IntMap (PrimState m) (Int, a)
dim Int
l'
case Maybe (Int, (Int, a))
res of
Just (Int, (Int, a))
rng -> (Int, (Int, a)) -> Int -> Int -> m ()
run (Int, (Int, a))
rng Int
l Int
r
Maybe (Int, (Int, a))
Nothing -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
r Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
IntMap (PrimState m) a -> Int -> m ()
IM.delete_ IntMap (PrimState m) (Int, a)
dim Int
l'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
r (Int
r', a
x')
() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
handleLeft :: Int -> Int -> m ()
handleLeft Int
l Int
r = do
Maybe (Int, (Int, a))
res <- IntMap (PrimState m) (Int, a) -> Int -> m (Maybe (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> m (Maybe (Int, a))
IM.lookupLT IntMap (PrimState m) (Int, a)
dim Int
l
case Maybe (Int, (Int, a))
res of
Maybe (Int, (Int, a))
Nothing -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just (!Int
l', (!Int
r', !a
x'))
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
l -> do
() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Int
r' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
r -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
l' Int
l a
x'
Int -> Int -> a -> m ()
onAdd Int
r Int
r' a
x'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l' (Int
l, a
x')
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
r (Int
r', a
x')
| Bool
otherwise -> do
Int -> Int -> a -> m ()
onDel Int
l' Int
r' a
x'
Int -> Int -> a -> m ()
onAdd Int
l' Int
l a
x'
IntMap (PrimState m) (Int, a) -> Int -> (Int, a) -> m ()
forall (m :: * -> *) a.
(HasCallStack, PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> Int -> a -> m ()
IM.insert IntMap (PrimState m) (Int, a)
dim Int
l' (Int
l, a
x')
overwrite :: (PrimMonad m, Eq a, VU.Unbox a) => IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
overwrite :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
overwrite IntervalMap (PrimState m) a
itm Int
l Int
r a
x = do
Maybe (Int, Int, a)
res <- IntervalMap (PrimState m) a
-> Int -> Int -> m (Maybe (Int, Int, a))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a
-> Int -> Int -> m (Maybe (Int, Int, a))
lookup IntervalMap (PrimState m) a
itm Int
l Int
r
case Maybe (Int, Int, a)
res of
Just (!Int
l', !Int
r', !a
_) -> IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a -> Int -> Int -> a -> m ()
insert IntervalMap (PrimState m) a
itm Int
l' Int
r' a
x
Maybe (Int, Int, a)
Nothing -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
overwriteM ::
(PrimMonad m, Eq a, VU.Unbox a) =>
IntervalMap (PrimState m) a ->
Int ->
Int ->
a ->
(Int -> Int -> a -> m ()) ->
(Int -> Int -> a -> m ()) ->
m ()
overwriteM :: forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
overwriteM IntervalMap (PrimState m) a
itm Int
l Int
r a
x Int -> Int -> a -> m ()
onAdd Int -> Int -> a -> m ()
onDel = do
Maybe (Int, Int, a)
res <- IntervalMap (PrimState m) a
-> Int -> Int -> m (Maybe (Int, Int, a))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a
-> Int -> Int -> m (Maybe (Int, Int, a))
lookup IntervalMap (PrimState m) a
itm Int
l Int
r
case Maybe (Int, Int, a)
res of
Just (!Int
l', !Int
r', !a
_) -> IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
forall (m :: * -> *) a.
(PrimMonad m, Eq a, Unbox a) =>
IntervalMap (PrimState m) a
-> Int
-> Int
-> a
-> (Int -> Int -> a -> m ())
-> (Int -> Int -> a -> m ())
-> m ()
insertM IntervalMap (PrimState m) a
itm Int
l' Int
r' a
x Int -> Int -> a -> m ()
onAdd Int -> Int -> a -> m ()
onDel
Maybe (Int, Int, a)
Nothing -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
freeze :: (PrimMonad m, VU.Unbox a) => IntervalMap (PrimState m) a -> m (VU.Vector (Int, (Int, a)))
freeze :: forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntervalMap (PrimState m) a -> m (Vector (Int, (Int, a)))
freeze = IntMap (PrimState m) (Int, a) -> m (Vector (Int, (Int, a)))
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
IntMap (PrimState m) a -> m (Vector (Int, a))
IM.assocs (IntMap (PrimState m) (Int, a) -> m (Vector (Int, (Int, a))))
-> (IntervalMap (PrimState m) a -> IntMap (PrimState m) (Int, a))
-> IntervalMap (PrimState m) a
-> m (Vector (Int, (Int, a)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntervalMap (PrimState m) a -> IntMap (PrimState m) (Int, a)
forall s a. IntervalMap s a -> IntMap s (Int, a)
unITM