Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add len and is_empty to fenwicktree, segtree, and lazysegtree #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/fenwicktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ impl<T: Clone + std::ops::AddAssign<T>> FenwickTree<T> {
};
self.accum(r) - self.accum(l)
}
/// Returns the number of elements.
pub fn len(&self) -> usize {
self.n
}
/// Returns if the container is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

#[cfg(test)]
Expand All @@ -67,6 +75,10 @@ mod tests {
for i in 0..5 {
bit.add(i, i as i64 + 1);
}

assert_eq!(bit.len(), 5);
assert!(!bit.is_empty());

assert_eq!(bit.sum(0..5), 15);
assert_eq!(bit.sum(0..4), 10);
assert_eq!(bit.sum(1..3), 5);
Expand Down
12 changes: 12 additions & 0 deletions src/lazysegtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ impl<F: MapMonoid> LazySegtree<F> {
} {}
0
}

/// Returns the number of elements.
pub fn len(&self) -> usize {
self.n
}
/// Returns if the container is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

pub struct LazySegtree<F>
Expand Down Expand Up @@ -381,6 +390,9 @@ mod tests {
let mut segtree: LazySegtree<MaxAdd> = base.clone().into();
check_segtree(&base, &mut segtree);

assert_eq!(segtree.len(), n);
assert!(!segtree.is_empty());

let mut segtree = LazySegtree::<MaxAdd>::new(n);
let mut internal = vec![i32::min_value(); n];
for i in 0..n {
Expand Down
12 changes: 12 additions & 0 deletions src/segtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ impl<M: Monoid> Segtree<M> {
fn update(&mut self, k: usize) {
self.d[k] = M::binary_operation(&self.d[2 * k], &self.d[2 * k + 1]);
}

/// Returns the number of elements.
pub fn len(&self) -> usize {
self.n
}
/// Returns if the container is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

// Maybe we can use this someday
Expand Down Expand Up @@ -269,6 +278,9 @@ mod tests {
let segtree: Segtree<Max<_>> = base.clone().into();
check_segtree(&base, &segtree);

assert_eq!(segtree.len(), n);
assert!(!segtree.is_empty());

let mut segtree = Segtree::<Max<_>>::new(n);
let mut internal = vec![i32::min_value(); n];
for i in 0..n {
Expand Down