Documentation ¶
Overview ¶
Package bit implements a Binary Indexed Tree.
Index ¶
- func Copy(dst, src Tree) int
- func Len(t Tree) int
- type Tree
- func (t Tree) Add(i int, value int32)
- func (t Tree) Mul(i int, value int32) int32
- func (t Tree) Number(i int) int32
- func (t Tree) Numbers(numbers []int32) int
- func (t Tree) RangeAdd(i int, numbers []int32)
- func (t Tree) RangeMul(i int, factors []int32)
- func (t Tree) RangeNumbers(lo int, buf []int32) int
- func (t Tree) RangeScale(lo, hi int, multiplier int32)
- func (t Tree) RangeSet(i int, numbers []int32)
- func (t Tree) RangeShift(lo, hi int, value int32)
- func (t Tree) RangeSum(lo, hi int) int32
- func (t *Tree) Reset()
- func (t Tree) Scale(value int32)
- func (t Tree) SearchSum(value int32) (int, int32)
- func (t Tree) Set(i int, number int32)
- func (t Tree) Shift(value int32)
- func (t Tree) Sum(i int) int32
- func (t Tree) Sums(sums []int32) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Tree ¶
type Tree []int32
Tree represents a Binary Indexed Tree (BIT) type.
func From ¶
From creates a Binary Indexed Tree from a slice of numbers. When the reUse option is set, the tree will use the numbers slice as its backing store, avoiding new allocations. Default behavior for the tree is to allocate its own backing store.
func New ¶
New creates a Binary Indexed Tree of n elements. If n is not provided, the tree length defaults to zero.
func (Tree) Add ¶
Add adds the given vanlue to the number in the tree at index i. If the index is outside of the tree boundaries, no value is added.
func (Tree) Mul ¶
Mul multiplies the number at index i with the given value. If the index is outside of the tree boundaries, no modifications are done.
func (Tree) Number ¶
Number returns the element at index i. If i is outside of the tree, 0 will be returned.
func (Tree) Numbers ¶
Numbers returns all numbers in the tree. The caller provides the array to store the numbers. If the numbers slice is too short, only numbers up to the length of the slice will be returned.
func (Tree) RangeAdd ¶
RangeAdd adds a slice of numbers to the numbers in the tree at index i and subsequent indices.
func (Tree) RangeMul ¶
RangeMul multiplies a slice of numbers with the respective numbers in the tree, starting at index i.
func (Tree) RangeNumbers ¶
RangeNumbers returns in the buf variable a slice of numbers, as defined by the given boundaries. The upper bound is not included. If the lo index is out of boundaries, zero will be returned.
func (Tree) RangeScale ¶
RangeScale scales all numbers in the [lo, hi) range of the tree with the given multiplier. If lo/hi are outside the boundaries of the tree, the [lo, hi) range will be intersected with the tree range.
func (Tree) RangeShift ¶
RangeShift adds the given value to all numbers in the [lo, hi) index range of the tree. If lo/hi are outside the boundaries of the tree, the [lo, hi) range will be intersected with the tree range.
func (Tree) RangeSum ¶
RangeSum returns the prefix sum of the [lo, hi) range. In case of a partial overlap of the range with the tree, RangeSum will return the prefix sum of the intersection of the given interval with the interval of the tree.
func (*Tree) Reset ¶
func (t *Tree) Reset()
Reset initializes the length of the tree to zero, but keeps the backing store. After Reset, the tree can be re-used with Append.
func (Tree) SearchSum ¶
SearchSum returns the largest index and corresponding prefix sum that is smaller than or equal to the given value. In case the tree is empty, -1 is returned.This operation assumes the prefix sums to increase monotonically.
func (Tree) Set ¶
Set sets a number at a given index. If the index is outside of the tree, no updates are made.