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

SmallBitVec::range method (alternative to impl Index<Range<usize>>) #11

Merged
merged 4 commits into from
Apr 17, 2018
Merged
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
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,18 @@ impl SmallBitVec {
}
}

/// Returns an immutable view of a range of bits from this vec.
/// ```
/// #[macro_use] extern crate smallbitvec;
/// let v = sbvec![true, false, true];
/// let r = v.range(1..3);
/// assert_eq!(r[1], true);
/// ```
pub fn range(&self, range: Range<usize>) -> VecRange {
assert!(range.end <= self.len(), "range out of bounds");
VecRange { vec: &self, range }
}

/// Returns true if all the bits in the vec are set to zero/false.
pub fn all_false(&self) -> bool {
let mut len = self.len();
Expand Down Expand Up @@ -806,3 +818,34 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
}

impl<'a> ExactSizeIterator for Iter<'a> {}

/// An immutable view of a range of bits from a borrowed SmallBitVec.
///
/// Returned from [`SmallBitVec::range`][1].
///
/// [1]: struct.SmallBitVec.html#method.range
#[derive(Debug, Clone)]
pub struct VecRange<'a> {
vec: &'a SmallBitVec,
range: Range<usize>,
}

impl<'a> VecRange<'a> {
pub fn iter(&self) -> Iter<'a> {
Iter {
vec: self.vec,
range: self.range.clone(),
}
}
}

impl<'a> Index<usize> for VecRange<'a> {
type Output = bool;

#[inline]
fn index(&self, i: usize) -> &bool {
let vec_i = i + self.range.start;
assert!(vec_i < self.range.end, "index out of range");
&self.vec[vec_i]
}
}
37 changes: 37 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@ fn iter_back() {
assert_eq!(i.next(), None);
}

#[test]
fn range() {
let mut v = SmallBitVec::new();
v.push(true);
v.push(false);
v.push(false);
v.push(true);

let r = v.range(0..2);
let mut ri = r.iter();
assert_eq!(ri.next(), Some(true));
assert_eq!(ri.next(), Some(false));
assert_eq!(ri.next(), None);
assert_eq!(r[0], true);
}

#[test]
#[should_panic(expected = "range out of bounds")]
fn range_oob() {
let mut v = SmallBitVec::new();
v.push(true);

v.range(0..2);
}

#[test]
#[should_panic(expected = "index out of range")]
fn range_index_oob() {
let mut v = SmallBitVec::new();
v.push(true);
v.push(false);
v.push(true);

let r = v.range(1..2);
r[1];
}

#[test]
fn debug() {
let mut v = SmallBitVec::new();
Expand Down