#sum #prefix #tree #fenwick #no-std

no-std ftree

A very fast fenwick tree implementation

6 releases (stable)

1.2.0 Nov 9, 2024
1.1.0 May 24, 2024
1.0.1 Feb 17, 2024
1.0.0 Jul 14, 2023
0.1.1 Jul 13, 2023

#514 in Data structures

Download history 161/week @ 2024-07-29 26/week @ 2024-08-05 101/week @ 2024-08-12 232/week @ 2024-08-19 289/week @ 2024-08-26 187/week @ 2024-09-02 266/week @ 2024-09-09 402/week @ 2024-09-16 440/week @ 2024-09-23 349/week @ 2024-09-30 369/week @ 2024-10-07 386/week @ 2024-10-14 362/week @ 2024-10-21 281/week @ 2024-10-28 305/week @ 2024-11-04 401/week @ 2024-11-11

1,365 downloads per month
Used in 3 crates (2 directly)

Apache-2.0 OR MIT

17KB
276 lines

ftree

crates.io docs

A pure-rust(with zero dependencies, no-std) fenwick tree, for the efficient computation of dynamic prefix sums.

Background

Did you ever have to keep track of a sum, and update it at the same time?

Let's say that you have an array that represents the lengths of some other containers: [1, 6, 3, 9, 2]

What if you want to get the sum up until the n-th element? In the worst-case, this will take O(n) time. Updating on the other hand, is simply a matter of incrementing at the specified index, at O(1).

A fenwick tree allows you to both get the sum and do updates in O(log n) time.

Moreover, let's assume that you want to get the index of the first value such that <= sum.

Without using a Fenwick tree, this would take (n * log n) time (a binary search with the sum being computed during each iteration). Using one however, only takes O(log n) time. This might seem like a very niche need, and it is. It is utilized in the indexset crate, a two-level B-Tree, to very efficiently support vector-like indexing by position.

Performance

It's very performant. I have searched all over codeforces for all competitive programming fenwick tree performance tricks that there are, and put them all in this crate.

Naming

This library is called ftree, because the base data structure is FenwickTree.

Changelog

See CHANGELOG.md.

Dependencies

~155KB