| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
AtCoder.Extra.Seq
Description
Dynamic sequence of monoid values with monoid actions on them through the SegAct instance.
Performance
This module is slow as an ordinary dynamic sequence. Consider using another module if you don't need monoid products.
Example
Create a Seq storage of length \(10\):
>>>import AtCoder.Extra.Monoid.RangeAdd qualified as RangeAdd>>>import AtCoder.Extra.Seq qualified as Seq>>>import Data.Semigroup (Sum (..))>>>import Data.Vector.Unboxed qualified as VU>>>seq <- Seq.new @_ @(RangeAdd.RangeAdd (Sum Int)) @(Sum Int) 10
Allocate a sequence of \(0, 1, 2, 3\):
>>>handle <- Seq.newSeq seq (VU.fromList [0, 1, 2, 3])
Get monoid products:
>>>Seq.prodAll seq handleSum {getSum = 6}
>>>Seq.prod seq handle 1 3Sum {getSum = 3}
read, write, modify and exchange are available:
>>>-- [0, 1, 2, 3] -> [0, 10, 2, 30]>>>Seq.write seq handle 3 30>>>Seq.modify seq handle (* 10) 1
Actions can be performed with SegAct instances:
>>>-- [0, 10, 2, 30] -> [0, 20, 12, 40]>>>Seq.applyIn seq handle 1 4 (RangeAdd.new 10)>>>Seq.prod seq handle 1 4Sum {getSum = 72}
The sequence can be reversed if the action type is commutative:
>>>Seq.reverse seq handle 0 4>>>VU.map getSum <$> Seq.freeze seq handle[40,12,20,0]
The sequence is dynamic and new elements can be inserted or deleted:
>>>-- [40, 12, 20, 0] -> [40, 33, 12, 20, 0]>>>Seq.insert seq handle 1 (Sum 33)>>>-- [40, 33, 12, 20, 0] -> [40, 33, 12, 0]>>>Seq.delete seq handle 3Sum {getSum = 20}
>>>VU.map getSum <$> Seq.freeze seq handle[40,33,12,0]
The Seq storage can store multiple sequences:
>>>handle2 <- Seq.newSeq seq (VU.generate 2 Sum)>>>VU.map getSum <$> Seq.freeze seq handle2[0,1]
Merge/split operations are available. merge functions mutate the given handle to be the
merged sequence handle:
>>>Seq.merge seq handle handle2>>>VU.map getSum <$> Seq.freeze seq handle[40,33,12,0,0,1]
split functions mutate the given handle to be the leftmost one and returns right sequence
handles:
>>>(handleM, handleR) <- Seq.split3 seq handle 2 4>>>VU.map getSum <$> Seq.freeze seq handle[40,33]
>>>VU.map getSum <$> Seq.freeze seq handleM[12,0]
>>>VU.map getSum <$> Seq.freeze seq handleR[0,1]
Bisection methods are available for monoid values and their products:
>>>Seq.reset seq>>>handle <- Seq.newSeq seq $ VU.generate 10 Sum>>>Seq.ilowerBound seq handle (\_ x -> x < 5)5
>>>Seq.ilowerBoundProd seq handle (\_ x -> x < 5)3
Since: 1.2.0.0
Synopsis
- data Seq s f a = Seq {}
- newtype Handle s = Handle {}
- newHandle :: PrimMonad m => Index -> m (Handle (PrimState m))
- nullHandle :: PrimMonad m => Handle (PrimState m) -> m Bool
- invalidateHandle :: PrimMonad m => Handle (PrimState m) -> m ()
- class Monoid f => SegAct f a where
- segAct :: f -> a -> a
- segActWithLength :: Int -> f -> a -> a
- new :: (PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) => Int -> m (Seq (PrimState m) f a)
- reset :: PrimMonad m => Seq (PrimState m) f a -> m ()
- free :: (PrimMonad m, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m ()
- newNode :: (HasCallStack, PrimMonad m, Monoid f, Unbox f, Unbox a) => Seq (PrimState m) f a -> a -> m (Handle (PrimState m))
- newSeq :: (HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Vector a -> m (Handle (PrimState m))
- capacity :: Seq s f a -> Int
- length :: PrimMonad m => Seq (PrimState m) f a -> Handle (PrimState m) -> m Int
- merge :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> m ()
- merge3 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> m ()
- merge4 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> m ()
- split :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Handle (PrimState m))
- split3 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m (Handle (PrimState m), Handle (PrimState m))
- split4 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> Int -> m (Handle (PrimState m), Handle (PrimState m), Handle (PrimState m))
- splitLr :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m (Handle (PrimState m), Handle (PrimState m))
- read :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m a
- readMaybe :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Maybe a)
- write :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m ()
- modify :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (a -> a) -> Int -> m ()
- exchange :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m a
- prod :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m a
- prodMaybe :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m (Maybe a)
- prodAll :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m a
- applyIn :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> f -> m ()
- applyToRoot :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> f -> m ()
- reverse :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m ()
- insert :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m ()
- delete :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m a
- delete_ :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m ()
- detach :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Handle (PrimState m))
- ilowerBound :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> Bool) -> m Int
- ilowerBoundM :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> m Bool) -> m Int
- ilowerBoundProd :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> Bool) -> m Int
- ilowerBoundProdM :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> m Bool) -> m Int
- isplitMaxRight :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> Bool) -> m (Handle (PrimState m))
- isplitMaxRightM :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> m Bool) -> m (Handle (PrimState m))
- isplitMaxRightProd :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> Bool) -> m (Handle (PrimState m))
- isplitMaxRightProdM :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (Int -> a -> m Bool) -> m (Handle (PrimState m))
- freeze :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m (Vector a)
Seq
Storages of dynamic sequences of monoid values with monoid actions on them through the SegAct
instance.
Since: 1.2.0.0
Constructors
| Seq | |
Fields
| |
Handle (re-exports)
newHandle :: PrimMonad m => Index -> m (Handle (PrimState m)) Source #
\(O(1)\) Creates a new sequence Handle from a root node index.
Since: 1.2.0.0
nullHandle :: PrimMonad m => Handle (PrimState m) -> m Bool Source #
\(O(1)\) Returns whether the sequence is empty.
Since: 1.2.0.0
invalidateHandle :: PrimMonad m => Handle (PrimState m) -> m () Source #
\(O(1)\) Invalidates a sequence handle. Note that it does not change or free the sequence.
Since: 1.2.0.0
Re-exports
class Monoid f => SegAct f a where Source #
Typeclass reprentation of the LazySegTree properties. User can implement either segAct or
segActWithLength.
Instances should satisfy the follwing properties:
- Left monoid action
segAct(f2<>f1) x =segActf2 (segActf1 x)- Identity map
segActmemptyx = x- Endomorphism
segActf (x1<>x2) = (segActf x1)<>(segActf x2)
If you implement SegAct via segActWithLength, satisfy one more propety:
- Linear left monoid action
.segActWithLengthlen f (stimes len a) =stimeslen (segActf a)
Invariant
In SegAct instances, new semigroup values are always given from the left: new . The
order is important for non-commutative monoid implementations.<> old
Example instance
Take Affine1 as an example of type \(F\).
{-# LANGUAGE TypeFamilies #-}
import AtCoder.LazySegTree qualified as LST
import AtCoder.LazySegTree (SegAct (..))
import Data.Monoid
import Data.Vector.Generic qualified as VG
import Data.Vector.Generic.Mutable qualified as VGM
import Data.Vector.Unboxed qualified as VU
import Data.Vector.Unboxed.Mutable qualified as VUM
-- | f x = a * x + b. It's implemented as a newtype of `(a, a)` for easy Unbox deriving.
newtype Affine1 a = Affine1 (Affine1 a)
deriving newtype (Eq, Ord, Show)
-- | This type alias makes the Unbox deriving easier, described velow.
type Affine1Repr a = (a, a)
instance (Num a) => Semigroup (Affine1 a) where
{-# INLINE (<>) #-}
(Affine1 (!a1, !b1)) <> (Affine1 (!a2, !b2)) = Affine1 (a1 * a2, a1 * b2 + b1)
instance (Num a) => Monoid (Affine1 a) where
{-# INLINE mempty #-}
mempty = Affine1 (1, 0)
instance (Num a) => SegAct (Affine1 a) (Sum a) where
{-# INLINE segActWithLength #-}
segActWithLength len (Affine1 (!a, !b)) !x = a * x + b * fromIntegral len
Deriving Unbox is very easy for such a newtype (though the efficiency is
not the maximum):
newtype instance VU.MVector s (Affine1a) = MV_Affine1 (VU.MVector s (Affine1a)) newtype instance VU.Vector (Affine1a) = V_Affine1 (VU.Vector (Affine1a)) deriving instance (VU.Unbox a) => VGM.MVector VUM.MVector (Affine1a) deriving instance (VU.Unbox a) => VG.Vector VU.Vector (Affine1a) instance (VU.Unbox a) => VU.Unbox (Affine1a)
Example contest template
Define your monoid action F and your acted monoid X:
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeFamilies #-}
import AtCoder.LazySegTree qualified as LST
import AtCoder.LazySegTree (SegAct (..))
import Data.Vector.Generic qualified as VG
import Data.Vector.Generic.Mutable qualified as VGM
import Data.Vector.Unboxed qualified as VU
import Data.Vector.Unboxed.Mutable qualified as VUM
{- ORMOLU_DISABLE -}
-- | F is a custom monoid action, defined as a newtype of FRepr.
newtype F = F FRepr deriving newtype (Eq, Ord, Show) ; unF :: F -> FRepr ; unF (F x) = x ; newtype instance VU.MVector s F = MV_F (VU.MVector s FRepr) ; newtype instance VU.Vector F = V_F (VU.Vector FRepr) ; deriving instance VGM.MVector VUM.MVector F ; deriving instance VG.Vector VU.Vector F ; instance VU.Unbox F ;
{- ORMOLU_ENABLE -}
-- | Affine: f x = a * x + b
type FRepr = (Int, Int)
instance Semigroup F where
-- new <> old
{-# INLINE (<>) #-}
(F (!a1, !b1)) <> (F (!a2, !b2)) = F (a1 * a2, a1 * b2 + b1)
instance Monoid F where
{-# INLINE mempty #-}
mempty = F (1, 0)
{- ORMOLU_DISABLE -}
-- | X is a custom acted monoid, defined as a newtype of XRepr.
newtype X = X XRepr deriving newtype (Eq, Ord, Show) ; unX :: X -> XRepr ; unX (X x) = x; newtype instance VU.MVector s X = MV_X (VU.MVector s XRepr) ; newtype instance VU.Vector X = V_X (VU.Vector XRepr) ; deriving instance VGM.MVector VUM.MVector X ; deriving instance VG.Vector VU.Vector X ; instance VU.Unbox X ;
{- ORMOLU_ENABLE -}
-- | Acted Int (same as `Sum Int`).
type XRepr = Int
deriving instance Num X; -- in our case X is a Num.
instance Semigroup X where
{-# INLINE (<>) #-}
(X x1) <> (X x2) = X $! x1 + x2
instance Monoid X where
{-# INLINE mempty #-}
mempty = X 0
instance SegAct F X where
-- {-# INLINE segAct #-}
-- segAct len (F (!a, !b)) (X x) = X $! a * x + b
{-# INLINE segActWithLength #-}
segActWithLength len (F (!a, !b)) (X x) = X $! a * x + len * b
It's tested as below:
expect :: (Eq a, Show a) => String -> a -> a -> ()
expect msg a b
| a == b = ()
| otherwise = error $ msg ++ ": expected " ++ show a ++ ", found " ++ show b
main :: IO ()
main = do
seg <- LST.build _ F @X $ VU.map X $ VU.fromList [1, 2, 3, 4]
LST.applyIn seg 1 3 $ F (2, 1) -- [1, 5, 7, 4]
LST.write seg 3 $ X 10 -- [1, 5, 7, 10]
LST.modify seg (+ (X 1)) 0 -- [2, 5, 7, 10]
!_ <- (expect "test 1" (X 5)) <$> LST.read seg 1
!_ <- (expect "test 2" (X 14)) <$> LST.prod seg 0 3 -- reads an interval [0, 3)
!_ <- (expect "test 3" (X 24)) <$> LST.allProd seg
!_ <- (expect "test 4" 2) <$> LST.maxRight seg 0 (<= (X 10)) -- sum [0, 2) = 7 <= 10
!_ <- (expect "test 5" 3) <$> LST.minLeft seg 4 (<= (X 10)) -- sum [3, 4) = 10 <= 10
putStrLn "=> test passed!"
Since: 1.0.0.0
Minimal complete definition
Nothing
Methods
segAct :: f -> a -> a Source #
Lazy segment tree action \(f(x)\).
Since: 1.0.0.0
segActWithLength :: Int -> f -> a -> a Source #
Lazy segment tree action \(f(x)\) with the target monoid's length.
If you implement SegAct with this function, you don't have to store the monoid's length,
since it's given externally.
Since: 1.0.0.0
Instances
| SegAct () a Source # | Since: 1.2.0.0 |
Defined in AtCoder.LazySegTree | |
| Monoid a => SegAct (RangeSet a) a Source # | Since: 1.0.0.0 |
Defined in AtCoder.Extra.Monoid.RangeSet | |
| Num a => SegAct (Affine1 (Sum a)) (Sum a) Source # | Since: 1.0.0.0 |
| Num a => SegAct (Affine1 a) (V2 a) Source # | Since: 1.2.2.0 |
| Num a => SegAct (Affine1 a) (Sum a) Source # | Since: 1.0.0.0 |
| Num a => SegAct (Mat2x2 a) (V2 a) Source # | Since: 1.1.0.0 |
| (Num a, Monoid (Max a)) => SegAct (RangeAdd (Max a)) (Max a) Source # | Since: 1.1.0.0 |
| (Num a, Monoid (Min a)) => SegAct (RangeAdd (Min a)) (Min a) Source # | Since: 1.1.0.0 |
| Num a => SegAct (RangeAdd (Sum a)) (Sum a) Source # | Since: 1.2.0.0 |
| Num a => SegAct (Dual (Affine1 (Sum a))) (Sum a) Source # | Since: 1.0.0.0 |
| Num a => SegAct (Dual (Affine1 a)) (V2 a) Source # | Since: 1.2.2.0 |
| Num a => SegAct (Dual (Affine1 a)) (Sum a) Source # | Since: 1.0.0.0 |
| Num a => SegAct (Dual (Mat2x2 a)) (V2 a) Source # | Since: 1.1.0.0 |
Constructors
new :: (PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) => Int -> m (Seq (PrimState m) f a) Source #
\(O(n)\) Creates a new Seq of length \(n\).
Since: 1.2.0.0
reset :: PrimMonad m => Seq (PrimState m) f a -> m () Source #
\(O(1)\) Clears the sequence storage. All the handles must not be used again.
Since: 1.2.0.0
free :: (PrimMonad m, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m () Source #
\(O(n)\) Frees a sequence and invalidates the handle.
Since: 1.2.0.0
newNode :: (HasCallStack, PrimMonad m, Monoid f, Unbox f, Unbox a) => Seq (PrimState m) f a -> a -> m (Handle (PrimState m)) Source #
\(O(1)\) Allocates a new sequence of length \(1\).
Since: 1.2.0.0
newSeq :: (HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Vector a -> m (Handle (PrimState m)) Source #
\(O(n)\) Allocates a new sequence.
Since: 1.2.0.0
Metadata
capacity :: Seq s f a -> Int Source #
\(O(1)\) Returns the capacity of the sequence storage.
Since: 1.2.1.0
length :: PrimMonad m => Seq (PrimState m) f a -> Handle (PrimState m) -> m Int Source #
\(O(1)\) Returns the length of the sequence.
Since: 1.2.1.0
Merge/split
merge :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> m () Source #
Amortized \(O(\log n)\). Merges two sequences \(l, r\) into one in the given order, ignoring empty sequences. The right sequence handle will be invalidated.
Since: 1.2.0.0
merge3 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> m () Source #
Amortized \(O(\log n)\). Merges three sequences \(l, m, r\) into one in the given order, ignoring empty sequences. All handles, except for the leftmost one, will be invalidated.
Since: 1.2.0.0
merge4 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> Handle (PrimState m) -> m () Source #
Amortized \(O(\log n)\). Merges four sequences \(a, b, c, d\) into one in the given order, ignoring empty sequences. All handles, except for the leftmost one, will be invalidated.
Constraints
- The vertices must be roots.
Since: 1.2.0.0
split :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Handle (PrimState m)) Source #
Amortized \(O(\log n)\). Splits a sequences into two: \([0, k), [k, n)\). The handle will point to the left sequence. Returns the right sequence handle.
Constraints
- \(0 \le k \le n\).
Since: 1.2.0.0
split3 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m (Handle (PrimState m), Handle (PrimState m)) Source #
Amortized \(O(\log n)\). Splits a sequences into three: \([0, l), [l, r), [r, n)\). The handle will point to the leftmost sequence. Returns the middle and the right sequence handles.
Constraints
- \(0 \le l \le r \le n\).
Since: 1.2.0.0
split4 :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> Int -> m (Handle (PrimState m), Handle (PrimState m), Handle (PrimState m)) Source #
Amortized \(O(\log n)\). Splits a sequences into four: \([0, i), [i, j), [j, k), [k, n)\). The handle will point to the leftmost sequence. Returns the non-leftmost sequence handles.
Constraints
- \(0 \le i \le j \le k \le n\).
Since: 1.2.0.0
splitLr :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m (Handle (PrimState m), Handle (PrimState m)) Source #
Amortized \(O(\log n)\). Splits a sequence into three: \([0, \mathrm{root}), \mathrm{root}, [\mathrm{root} + 1, n)\).
Constraints
- The sequence must be non-empty.
Since: 1.2.0.0
Read/write
read :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m a Source #
Amortized \(O(\log n)\). Reads the \(k\)-th node's monoid value.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
readMaybe :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Maybe a) Source #
Amortized \(O(\log n)\). Reads the \(k\)-th node's monoid value.
Since: 1.2.0.0
write :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m () Source #
Amortized \(O(\log n)\). Writes to the \(k\)-th node's monoid value.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
modify :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> (a -> a) -> Int -> m () Source #
Amortized \(O(\log n)\). Modifies the \(k\)-th node's monoid value.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
exchange :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m a Source #
Amortized \(O(\log n)\). Exchanges the \(k\)-th node's monoid value.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
Products
prod :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m a Source #
Amortized \(O(\log n)\). Returns the monoid product in an interval \([l, r)\).
Constraints
- \(0 \le l \le r \le n\)
Since: 1.2.0.0
prodMaybe :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m (Maybe a) Source #
Amortized \(O(\log n)\). Returns the monoid product in an interval \([l, r)\). Returns
Nothing if the interval is invalid.
Since: 1.2.0.0
prodAll :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> m a Source #
Amortized \(O(\log n)\). Returns the monoid product of the whole sequence.
Since: 1.2.0.0
Applications
applyIn :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> f -> m () Source #
Amortized \(O(\log n)\). Given an interval \([l, r)\), applies a monoid action \(f\).
Constraints
- \(0 \le l \le r \le n\)
Since: 1.2.0.0
applyToRoot :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> f -> m () Source #
\(O(1)\) Applies a monoid action \(f\) to the root of a sequence.
Since: 1.2.0.0
reverse :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> Int -> m () Source #
Amortized \(O(\log n)\). Reverses the sequence in \([l, r)\).
Constraints
- The monoid action \(f\) must be commutative.
- The monoid value \(v\) must be commutative.
Since: 1.2.0.0
Insert/delete
insert :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> a -> m () Source #
Amortized \(O(\log n)\). Inserts a new node at \(k\) with initial monoid value \(v\). This function works for an empty sequence handle.
Constraints
- \(0 \le k \le n\)
Since: 1.2.0.0
delete :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m a Source #
Amortized \(O(\log n)\). Frees the \(k\)-th node and returns the monoid value of it.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
delete_ :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m () Source #
Amortized \(O(\log n)\). Frees the \(k\)-th node.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
detach :: (HasCallStack, PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) => Seq (PrimState m) f a -> Handle (PrimState m) -> Int -> m (Handle (PrimState m)) Source #
Amortized \(O(\log n)\). Detaches the \(k\)-th node and returns a handle for it.
Constraints
- \(0 \le k \lt n\)
Since: 1.2.0.0
Bisection methods
C++-like
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> Bool) | User predicate \(f(i, v_i)\) that takes the index and the monoid value |
| -> m Int | Maximum \(r\), where \(f(i, v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\).
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> m Bool) | User predicate \(f(i, v_i)\) that takes the index and the monoid value |
| -> m Int | Maximum \(r\), where \(f(i, v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\).
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> Bool) | User predicate \(f(i, v_0 \dots v_i)\) that takes the index and the monoid product |
| -> m Int | Maximum \(r\), where \(f(i, v_0 \dots v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\).
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> m Bool) | User predicate \(f(i, v_0 \dots v_i)\) that takes the index and the monoid product |
| -> m Int | Maximum \(r\), where \(f(i, v_0 \dots v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\).
Since: 1.2.0.0
Splits
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> Bool) | User predicate \(f(i, v_i)\) that takes the index and the monoid value |
| -> m (Handle (PrimState m)) | Handle of the right sequence \([r, n)\), where \(r\) is the maximum \(r\) such that \(f(i, v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\). Splits a sequence into two with the user predicate and returns the right sequence handle.
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> m Bool) | User predicate \(f(i, v_i)\) that takes the index and the monoid value |
| -> m (Handle (PrimState m)) | Handle of the right sequence \([r, n)\), where \(r\) is the maximum \(r\) such that \(f(i, v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\). Splits a sequence into two with the user predicate and returns the right sequence handle.
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> Bool) | User predicate \(f(i, v_0 \dots v_i)\) that takes the index and the monoid value |
| -> m (Handle (PrimState m)) | Handle of the right sequence \([r, n)\), where \(r\) is the maximum \(r\) such that \(f(i, v_0 \dots v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\). Splits a sequence into two with the user predicate and returns the right sequence handle.
Since: 1.2.0.0
Arguments
| :: (PrimMonad m, SegAct f a, Eq f, Monoid f, Unbox f, Monoid a, Unbox a) | |
| => Seq (PrimState m) f a | Sequence storage |
| -> Handle (PrimState m) | Sequence handle |
| -> (Int -> a -> m Bool) | User predicate \(f(i, v_0 \dots v_i)\) that takes the index and the monoid value |
| -> m (Handle (PrimState m)) | Handle of the right sequence \([r, n)\), where \(r\) is the maximum \(r\) such that \(f(i, v_0 \dots v_i)\) holds for \(i \in [0, r)\) |
Amortized \(O(\log n)\). Splits a sequence into two with the user predicate and returns the right sequence handle.
Since: 1.2.0.0