From 640379baf779d93abbfcd7567af996bab75dabf7 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Wed, 10 Apr 2019 01:46:19 +0530 Subject: [PATCH] Add documentation for FenwickTree (#492) Add documentation for FenwickTree --- README.md | 1 + docs/make.jl | 1 + docs/src/fenwick.md | 25 +++++++++++++++++++++++++ src/fenwick.jl | 12 ++++++------ 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 docs/src/fenwick.md diff --git a/README.md b/README.md index 70c77072e..183b57225 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ This package implements a variety of data structures, including - Sorted Dict, Sorted Multi-Dict and Sorted Set - DataStructures.IntSet - Priority Queue +- Fenwick Tree Resources --------- diff --git a/docs/make.jl b/docs/make.jl index 394cb90e7..26e857385 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -12,6 +12,7 @@ makedocs( "circ_deque.md", "stack_and_queue.md", "priority-queue.md", + "fenwick.md", "accumulators.md", "disjoint_sets.md", "heaps.md", diff --git a/docs/src/fenwick.md b/docs/src/fenwick.md new file mode 100644 index 000000000..a6bd628c7 --- /dev/null +++ b/docs/src/fenwick.md @@ -0,0 +1,25 @@ +# Fenwick Tree + +The `FenwickTree` type is data structures which is used for handling increment and decrement to prefix-sums of an array efficiently. + +Usage: + +```julia +FenwickTree{T}(n) # Constructs a Fenwick Tree of length `n` +FenwickTree{T}(counts) # Constructs a Fenwick Tree from an array of `counts` +inc!(ft, ind, val) # Increases the value of the FenwickTree `ft` by `val` from the index `ind` upto the length of `ft` +dec!(ft, ind, val) # Decreases the value of the FenwickTree `ft` by `val` from the index `ind` upto the length of `ft` +incdec!(ft, left, right, val) # Increases the value of the FenwickTree `ft` by `val` from the indices from `left` and decreases it from the `right` +prefixsum(ft, ind) # Return the cumulative sum from index 1 upto `ind` of the FenwickTree `ft` +``` + +Examples: + +```julia +julia> f = FenwickTree{Int}(6) +julia> inc!(f, 2, 5) +julia> prefixsum(f, 1) + 0 +julia> prefixsum(f, 3) + 5 +``` diff --git a/src/fenwick.jl b/src/fenwick.jl index 4ce221941..30a359a81 100644 --- a/src/fenwick.jl +++ b/src/fenwick.jl @@ -1,18 +1,18 @@ +struct FenwickTree{T} + bi_tree::Vector{T} #bi_tree is shorthand for Binary Indexed Tree, an alternative name for Fenwick Tree + n::Int +end + """ FenwickTree{T}(n) Constructs a [`FenwickTree`](https://en.wikipedia.org/wiki/Fenwick_tree) of length `n`. """ -struct FenwickTree{T} - bi_tree::Vector{T} #bi_tree is shorthand for Binary Indexed Tree, an alternative name for Fenwick Tree - n::Int -end - FenwickTree{T}(n::Integer) where T = FenwickTree{T}(zeros(T, n), n) """ - FenwickTree(arr::AbstractArray) + FenwickTree(counts::AbstractArray) Constructs a [`FenwickTree`](https://en.wikipedia.org/wiki/Fenwick_tree) from an array of `counts`