Slighly over-engineered Fenwick Tree implmentation.
Allows efficient prefix sum calculation.
Created for training purposes to test:
- rust typesystem, default trait implmentation, enums as a way for polymorphism
- memory management and consumption of value
- cargo tools, docs, tests, clippy and benchmarks, build and publish.
Code is free to do whatever you feel like.
Provides abstraction for Fenwick tree data structure and 2 implmentations:
- [
prelude::FixedSizeFenwickTree
] - [
prelude::GrowingFenwickTree
]
Key space for a tree lies within [usize
] range. Tree support any value that
implements [FenwickTreeValue
] trait. [FenwickTreeValue
] is automatically
implmented for all primitive numeric types that support [std::ops::AddAssign
],
[std::ops::Sub
], [core::cmp::PartialEq
] and [Copy
] traits.
Installation
Test
Benchmarks
Basic usage:
use *;
// Create the tree with capacity for 32 aggregated [`i32`] data points.
// One can use whole usize range to store datapoints for unicode timestamps
let mut tree = new;
// Add values
tree.update;
tree.update; // Will aggregate value at index 0 so it would be 5
tree.update;
tree.update;
tree.update;
// Now you can query data.
// NOTE: FixedSizeFenwickTree will raise error when query goes out of bounds.
// GrowingFenwickTree will automatically truncate the range to the rightmost index.
assert_eq!;
assert_eq!;
assert_eq!;
// Also allows making range queries
let val = tree.range_query.unwrap; // Will return aggregated sum of all values between those keys.
assert_eq!;
Current version: 2.0.1
License: MIT OR Apache-2.0