Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Strict segment tree.
Synopsis
- data SegmentTree s a = SegmentTree {
- unSegmentTree :: !(MVector s a)
- nValidLeavesSegmentTree :: !Int
- newSTree :: (Unbox a, Monoid a, PrimMonad m) => Int -> m (SegmentTree (PrimState m) a)
- buildSTree :: (Unbox a, Monoid a, PrimMonad m) => Vector a -> m (SegmentTree (PrimState m) a)
- readSTree :: (HasCallStack, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> m a
- _unsafeUpdateParentNodes :: (Unbox a, Monoid a, PrimMonad m) => MVector (PrimState m) a -> Int -> m ()
- writeSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> a -> m ()
- exchangeSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> a -> m a
- modifySTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> (a -> a) -> Int -> m ()
- foldSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> m a
- foldMaySTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> m (Maybe a)
- foldAllSTree :: (HasCallStack, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m a
- bsearchSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int, Maybe Int)
- bsearchSTreeL :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int)
- bsearchSTreeR :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int)
- freezeLeavesSTree :: (Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m (Vector a)
- unsafeFreezeLeavesSTree :: (Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m (Vector a)
Documentation
data SegmentTree s a #
A mutable segment tree backed by a complete binary tree.
Overview
A segment tree is a complete binary tree of monoid folding results. Each vertex corresponds to a folding range and the result.
Typical monoids
(Internal) 1-based indices
Use 1-based indices for super handy vertex indices:
1 | 2 3 | height = 4 = log_2 16 4 5 6 7 | 08 09 10 11 12 13 14 15 v ^ +-- nVerts / 2 0 1 2 3 4 5 6 7 -- iLeaf is given by user and uses zero-based indices. - parent = v .>>. 1 - childL = v .<<. 1 - childR = v .<<. 1 .|. 1
Bottom-up vs top-down folding implementation
Most of the time, the top-down approach has to retrieve the values of the bottom leaves. So bottom-up implementation is almost always faster.
SegmentTree | |
|
newSTree :: (Unbox a, Monoid a, PrimMonad m) => Int -> m (SegmentTree (PrimState m) a) #
\(O(\log N)\) Creates a segment tree for n
leaves.
buildSTree :: (Unbox a, Monoid a, PrimMonad m) => Vector a -> m (SegmentTree (PrimState m) a) #
\(\Theta(N)\) Creates a segment tree from the given leaf values.
readSTree :: (HasCallStack, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> m a #
\(O(\log N)\) Reads a leaf value.
_unsafeUpdateParentNodes :: (Unbox a, Monoid a, PrimMonad m) => MVector (PrimState m) a -> Int -> m () #
\(O(\log N)\) (Internal) Updates parent nodes after modifying a leaf.
writeSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> a -> m () #
\(\Theta(\log N)\) Writes a leaf value.
exchangeSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> a -> m a #
\(\Theta(\log N)\) Writes a leaf value and returns the old value.
modifySTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> (a -> a) -> Int -> m () #
\(\Theta(\log N)\) Modifies a leaf value.
foldSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> m a #
\(O(\log N)\) Folds a non-empty [l, r]
span. Returns a broken avlue when given invalid range
(so this is actually unsafeFoldSTree
).
foldMaySTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> m (Maybe a) #
\(O(\log N)\) Folds a non-empty [l, r]
span. Returns Nothing
when given invalid range.
foldAllSTree :: (HasCallStack, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m a #
\(O(1)\) Reads the whole span segment.
bsearchSTree :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int, Maybe Int) #
bsearchSTreeL :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int) #
\(O(\log^2 N)\)
bsearchSTreeR :: (HasCallStack, Monoid a, Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> Int -> Int -> (a -> Bool) -> m (Maybe Int) #
\(O(\log^2 N)\)
freezeLeavesSTree :: (Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m (Vector a) #
\(\Theta(N)\) Freezes the leaf values making a copy.
unsafeFreezeLeavesSTree :: (Unbox a, PrimMonad m) => SegmentTree (PrimState m) a -> m (Vector a) #
\(\Theta(1)\) Freezes the leaf values without making a copy.