Skip to content

Commit 3b6aa25

Browse files
author
maxtremblay
committed
Add an iterator for non trivial positions
1 parent 4d0ba2f commit 3b6aa25

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sparse-bin-mat"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["maxtremblay <maxtremblay>"]
55
edition = "2018"
66
description = "A sparse implementation of a binary matrix optimized for row operations"

src/vector/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,11 @@ impl<T: Deref<Target = [usize]>> SparseBinVecBase<T> {
226226
/// assert_eq!(iter.next(), Some(3));
227227
/// assert_eq!(iter.next(), None);
228228
/// ```
229-
pub fn non_trivial_positions<'a>(&'a self) -> impl Iterator<Item = usize> + 'a {
230-
self.positions.iter().cloned()
229+
pub fn non_trivial_positions<'a>(&'a self) -> NonTrivialPositions<'a> {
230+
NonTrivialPositions {
231+
positions: &self.positions,
232+
index: 0,
233+
}
231234
}
232235

233236
/// Returns the concatenation of two vectors.
@@ -424,6 +427,22 @@ impl<T: Deref<Target = [usize]>> fmt::Display for SparseBinVecBase<T> {
424427
}
425428
}
426429

430+
pub struct NonTrivialPositions<'vec> {
431+
positions: &'vec [usize],
432+
index: usize,
433+
}
434+
435+
impl<'vec> Iterator for NonTrivialPositions<'vec> {
436+
type Item = usize;
437+
438+
fn next(&mut self) -> Option<Self::Item> {
439+
self.positions.get(self.index).map(|position| {
440+
self.index += 1;
441+
*position
442+
})
443+
}
444+
}
445+
427446
#[cfg(test)]
428447
mod test {
429448
use super::*;

0 commit comments

Comments
 (0)