Skip to content

Commit

Permalink
ref: refactor implementation
Browse files Browse the repository at this point in the history
- Clarify the documentation to emphasize the distinction between 1-based indexing (internally) and 0-based indexing (externally)
- Simplify the lowbit function to avoid casting between `isize` and `usize`
  • Loading branch information
sozelfist committed Sep 8, 2024
1 parent a3fbaf4 commit f354d29
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/data_structures/fenwick_tree.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{Add, AddAssign, Sub, SubAssign};

/// A Fenwick Tree (also known as a Binary Indexed Tree) that supports efficient
/// prefix sum and range sum queries, as well as point updates.
/// prefix sum, range sum and point queries, as well as point updates.
pub struct FenwickTree<T>
where
T: Add<Output = T> + AddAssign + Sub<Output = T> + SubAssign + Copy + Default,
Expand Down Expand Up @@ -46,6 +46,9 @@ where
///
/// This operation also propagates the update to subsequent elements in the tree.
///
/// **Note**: The `index` parameter is zero-based from the user's perspective,
/// while the tree uses one-based indexing internally.
///
/// # Arguments
///
/// * `index` - The zero-based index where the value should be added.
Expand Down Expand Up @@ -73,6 +76,9 @@ where
///
/// This operation efficiently calculates the prefix sum using the tree structure.
///
/// **Note**: The `index` parameter is zero-based, while the tree uses one-based
/// indexing internally.
///
/// # Arguments
///
/// * `index` - The zero-based index up to which the sum should be computed.
Expand Down Expand Up @@ -100,16 +106,17 @@ where
///
/// This operation calculates the range sum by performing two prefix sum queries.
///
/// **Note**: The `left` and `right` parameters are zero-based.
///
/// # Arguments
///
/// * `left` - The zero-based starting index of the range.
/// * `right` - The zero-based ending index of the range.
///
/// # Returns
///
/// A `Result` containing the range sum (`Ok(sum)`) or an error:
/// * `FenwickTreeError::IndexOutOfBounds` if the right index is out of bounds.
/// * `FenwickTreeError::InvalidRange` if the left index is greater than the right index.
/// A `Result` containing the range sum (`Ok(sum)`) or an error (`FenwickTreeError::InvalidRange`)
/// if the left index is greater than the right index or the right index is out of bounds.
pub fn range_query(&self, left: usize, right: usize) -> Result<T, FenwickTreeError> {
if left > right || right >= self.data.len() - 1 {
return Err(FenwickTreeError::InvalidRange);
Expand All @@ -130,6 +137,8 @@ where
/// This operation determines the value at `index` by subtracting the prefix sum up to `index - 1`
/// from the prefix sum up to `index`.
///
/// **Note**: The `index` parameter is zero-based.
///
/// # Arguments
///
/// * `index` - The zero-based index of the element to retrieve.
Expand Down Expand Up @@ -158,6 +167,8 @@ where
/// This operation updates the value at `index` by computing the difference between the
/// desired value and the current value, then applying that difference using `update`.
///
/// **Note**: The `index` parameter is zero-based.
///
/// # Arguments
///
/// * `index` - The zero-based index of the element to set.
Expand Down Expand Up @@ -185,8 +196,7 @@ where
///
/// The value of the lowest set bit in `x`.
const fn lowbit(x: usize) -> usize {
let x = x as isize;
(x & (-x)) as usize
x & (!x + 1)
}

#[cfg(test)]
Expand Down

0 comments on commit f354d29

Please sign in to comment.