haccepted-0.1.0.0: Data structures and algorithms
Safe HaskellNone
LanguageHaskell2010

Fenwick

Description

Fenwick tree, or binary indexed tree

A data structure supporting point updates and range queries, or the opposite. Large ranges, beyond typical memory limits, are supported. See FenwickMut.hs for a mutable (and more commonly seen) version.

Sources:

4 / / 2 6 1 3 5 7

Let n = r - l + 1 where (l, r) is the range of the Fenwick tree. The complexities assume (<>) takes O(1) time.

Synopsis

Documentation

data FTree a Source #

Instances

Instances details
Show a => Show (FTree a) Source # 
Instance details

Defined in Fenwick

Methods

showsPrec :: Int -> FTree a -> ShowS #

show :: FTree a -> String #

showList :: [FTree a] -> ShowS #

NFData a => NFData (FTree a) Source # 
Instance details

Defined in Fenwick

Methods

rnf :: FTree a -> () #

emptyF :: Monoid a => (Int, Int) -> FTree a Source #

Builds a Fenwick tree on range (l, r) where each element is mempty. O(log n).

fromListF :: Monoid a => (Int, Int) -> [a] -> FTree a Source #

Builds a Fenwick tree on (l, r) where the elements are taken from a list. If the list is shorter than the range, the remaining elements are mempty. O(n).

mappendF :: Commutative a => Int -> a -> FTree a -> FTree a Source #

mappends to the element at an index. O(log n).

foldPrefixF :: Monoid a => Int -> FTree a -> a Source #

The result of folding the prefix upto the given index. Indices outside the tree range are allowed, it is assumed elements there are mempty. O(log n).

foldRangeF :: (Commutative a, Group a) => Int -> Int -> FTree a -> a Source #

Folds the elements in the range (l, r). O(log n).

mappendRangeF :: (Commutative a, Group a) => Int -> Int -> a -> FTree a -> FTree a Source #

mappends to all elements in the range (l, r). Can be used with foldPrefixF for point queries. O(log n).

binSearchF :: Monoid a => (a -> Bool) -> FTree a -> Maybe (Int, a) Source #

Binary searches for the shortest prefix such that the fold of all values in it satisfies the given monotonic predicate. Returns the end index and the fold of the found prefix. O(log n).

toScanl1F :: Monoid a => FTree a -> [a] Source #

Converts to a list of prefix accumulated values. O(n).