Skip to content

Commit a043814

Browse files
committed
collections: Implement FromIterator/Extendable for BitvSet
1 parent 3737c53 commit a043814

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/libcollections/bitv.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,21 @@ impl Default for BitvSet {
978978
fn default() -> BitvSet { BitvSet::new() }
979979
}
980980

981+
impl FromIterator<bool> for BitvSet {
982+
fn from_iter<I:Iterator<bool>>(iterator: I) -> BitvSet {
983+
let mut ret = BitvSet::new();
984+
ret.extend(iterator);
985+
ret
986+
}
987+
}
988+
989+
impl Extendable<bool> for BitvSet {
990+
#[inline]
991+
fn extend<I: Iterator<bool>>(&mut self, iterator: I) {
992+
self.get_mut_ref().extend(iterator);
993+
}
994+
}
995+
981996
impl BitvSet {
982997
/// Create a new bit vector set with initially no contents.
983998
///
@@ -1958,6 +1973,17 @@ mod tests {
19581973
assert_eq!(bitv.to_string().as_slice(), "1011");
19591974
}
19601975

1976+
#[test]
1977+
fn test_bitv_set_from_bools() {
1978+
let bools = vec![true, false, true, true];
1979+
let a: BitvSet = bools.iter().map(|n| *n).collect();
1980+
let mut b = BitvSet::new();
1981+
b.insert(0);
1982+
b.insert(2);
1983+
b.insert(3);
1984+
assert_eq!(a, b);
1985+
}
1986+
19611987
#[test]
19621988
fn test_to_bools() {
19631989
let bools = vec!(false, false, true, false, false, true, true, false);
@@ -1977,7 +2003,7 @@ mod tests {
19772003
#[test]
19782004
fn test_bitv_set_iterator() {
19792005
let bools = [true, false, true, true];
1980-
let bitv = BitvSet::from_bitv(bools.iter().map(|n| *n).collect());
2006+
let bitv: BitvSet = bools.iter().map(|n| *n).collect();
19812007

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

0 commit comments

Comments
 (0)