Skip to content

Commit

Permalink
Implement Extend
Browse files Browse the repository at this point in the history
  • Loading branch information
haxelion committed Jul 8, 2024
1 parent 480f59f commit 489ace7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ impl FromIterator<Bit> for BV {
}
}

impl Extend<Bit> for BV {
fn extend<T: IntoIterator<Item = Bit>>(&mut self, iter: T) {
let iter = iter.into_iter();
self.reserve(iter.size_hint().0);
iter.for_each(|b| self.push(b));
}
}

// ------------------------------------------------------------------------------------------------
// BV - Formatting traits
// ------------------------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ impl FromIterator<Bit> for BVD {
}
}

impl Extend<Bit> for BVD {
fn extend<T: IntoIterator<Item = Bit>>(&mut self, iter: T) {
let iter = iter.into_iter();
self.reserve(iter.size_hint().0);
iter.for_each(|b| self.push(b));
}
}

// ------------------------------------------------------------------------------------------------
// BVD - Formatting traits
// ------------------------------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ where
}
}

impl<I: Integer, const N: usize> Extend<Bit> for BVF<I, N> {
fn extend<T: IntoIterator<Item = Bit>>(&mut self, iter: T) {
iter.into_iter().for_each(|b| self.push(b));
}
}

// ------------------------------------------------------------------------------------------------
// BVF - Formatting traits
// ------------------------------------------------------------------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions src/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,31 @@ fn from_iter_bv() {
from_iter_inner::<BV>(64);
from_iter_inner::<BV>(256);
}

fn extend_inner<B: BitVector + Extend<Bit>>(capacity: usize) {
let mut rng = thread_rng();
let bits: Vec<Bit> = (0..capacity)
.map(|_| Bit::from(rng.gen::<bool>()))
.collect();
let mut bv = B::with_capacity(capacity / 2);
bv.extend(bits.iter().copied());
for (b1, &b2) in bv.iter().zip(bits.iter()) {
assert_eq!(b1, b2);
}
}

#[test]
fn extend_bvf() {
bvf_inner_unroll_cap!(extend_inner, {u8, u16, u32, u64, u128}, {1, 2, 3, 4, 5});
}

#[test]
fn extend_bvd() {
extend_inner::<BVD>(256);
}

#[test]
fn extend_bv() {
extend_inner::<BV>(64);
extend_inner::<BV>(256);
}

0 comments on commit 489ace7

Please sign in to comment.