Skip to content

Commit

Permalink
Implement insert_slice for SmallVec
Browse files Browse the repository at this point in the history
Add benchmark for insert_many
  • Loading branch information
nipunn1313 committed Sep 12, 2016
1 parent 1714fbd commit a9fc702
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ impl<A: Array> SmallVec<A> {
self.set_len(len + 1);
}
}

pub fn insert_many<I: IntoIterator<Item=A::Item>>(&mut self, index: usize, iterable: I) {
let iter = iterable.into_iter();
let (lower_size_bound, _) = iter.size_hint();
self.reserve(lower_size_bound);

for (off, elem) in iter.enumerate() {
self.insert(index + off, elem);
}
}
}

impl<A: Array> ops::Deref for SmallVec<A> {
Expand Down Expand Up @@ -869,6 +879,17 @@ pub mod tests {
assert_eq!(&v.iter().map(|v| **v).collect::<Vec<_>>(), &[0, 3, 2]);
}

#[test]
fn test_insert_many() {
let mut v: SmallVec<[u8; 8]> = SmallVec::new();
for x in 0..4 {
v.push(x);
}
assert_eq!(v.len(), 4);
v.insert_many(1, [5, 6].iter().cloned());
assert_eq!(&v.iter().map(|v| *v).collect::<Vec<_>>(), &[0, 5, 6, 1, 2, 3]);
}

#[test]
#[should_panic]
fn test_drop_panic_smallvec() {
Expand Down Expand Up @@ -1039,6 +1060,14 @@ mod bench {
});
}

#[bench]
fn bench_insert_many(b: &mut Bencher) {
b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
vec.insert_many(0, 0..100);
});
}

#[bench]
fn bench_extend(b: &mut Bencher) {
b.iter(|| {
Expand Down

0 comments on commit a9fc702

Please sign in to comment.