Skip to content

Commit

Permalink
collections: Implement FromIterator/Extendable for BitvSet
Browse files Browse the repository at this point in the history
  • Loading branch information
nham committed Aug 1, 2014
1 parent 3737c53 commit a043814
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,21 @@ impl Default for BitvSet {
fn default() -> BitvSet { BitvSet::new() }
}

impl FromIterator<bool> for BitvSet {
fn from_iter<I:Iterator<bool>>(iterator: I) -> BitvSet {
let mut ret = BitvSet::new();
ret.extend(iterator);
ret
}
}

impl Extendable<bool> for BitvSet {
#[inline]
fn extend<I: Iterator<bool>>(&mut self, iterator: I) {
self.get_mut_ref().extend(iterator);
}
}

impl BitvSet {
/// Create a new bit vector set with initially no contents.
///
Expand Down Expand Up @@ -1958,6 +1973,17 @@ mod tests {
assert_eq!(bitv.to_string().as_slice(), "1011");
}

#[test]
fn test_bitv_set_from_bools() {
let bools = vec![true, false, true, true];
let a: BitvSet = bools.iter().map(|n| *n).collect();
let mut b = BitvSet::new();
b.insert(0);
b.insert(2);
b.insert(3);
assert_eq!(a, b);
}

#[test]
fn test_to_bools() {
let bools = vec!(false, false, true, false, false, true, true, false);
Expand All @@ -1977,7 +2003,7 @@ mod tests {
#[test]
fn test_bitv_set_iterator() {
let bools = [true, false, true, true];
let bitv = BitvSet::from_bitv(bools.iter().map(|n| *n).collect());
let bitv: BitvSet = bools.iter().map(|n| *n).collect();

let idxs: Vec<uint> = bitv.iter().collect();
assert_eq!(idxs, vec!(0, 2, 3));
Expand Down

0 comments on commit a043814

Please sign in to comment.