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

Implement insert_many<IntoIterator> for SmallVec #28

Merged
merged 2 commits into from
Jan 19, 2017
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
16 changes: 16 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ fn bench_insert(b: &mut Bencher) {
});
}

#[bench]
fn bench_insert_many(b: &mut Bencher) {
#[inline(never)]
fn insert_many_noinline<I: IntoIterator<Item=u64>>(
vec: &mut SmallVec<[u64; 16]>, index: usize, iterable: I) {
vec.insert_many(index, iterable)
}

b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
insert_many_noinline(&mut vec, 0, 0..100);
insert_many_noinline(&mut vec, 0, 0..100);
vec
});
}

#[bench]
fn bench_extend(b: &mut Bencher) {
b.iter(|| {
Expand Down
70 changes: 70 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,36 @@ 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();
assert!(lower_size_bound <= std::isize::MAX as usize); // Ensure offset is indexable
assert!(index + lower_size_bound >= index); // Protect against overflow
self.reserve(lower_size_bound);

unsafe {
let old_len = self.len;
assert!(index <= old_len);
let ptr = self.as_mut_ptr().offset(index as isize);
ptr::copy(ptr, ptr.offset(lower_size_bound as isize), old_len - index);
Copy link
Member

@jdm jdm Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to assert index <= old_len or this is unsafe.

for (off, element) in iter.enumerate() {
if off < lower_size_bound {
ptr::write(ptr.offset(off as isize), element);
self.len = self.len + 1;
} else {
// Iterator provided more elements than the hint.
assert!(index + off >= index); // Protect against overflow.
self.insert(index + off, element);
}
}
let num_added = self.len - old_len;
if num_added < lower_size_bound {
// Iterator provided fewer elements than the hint
ptr::copy(ptr.offset(lower_size_bound as isize), ptr.offset(num_added as isize), old_len - index);
}
}
}
}

impl<A: Array> ops::Deref for SmallVec<A> {
Expand Down Expand Up @@ -867,6 +897,46 @@ 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]);
}

struct MockHintIter<T: Iterator>{x: T, hint: usize}
impl<T: Iterator> Iterator for MockHintIter<T> {
type Item = T::Item;
fn next(&mut self) -> Option<Self::Item> {self.x.next()}
fn size_hint(&self) -> (usize, Option<usize>) {(self.hint, None)}
}

#[test]
fn test_insert_many_short_hint() {
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, MockHintIter{x: [5, 6].iter().cloned(), hint: 5});
assert_eq!(&v.iter().map(|v| *v).collect::<Vec<_>>(), &[0, 5, 6, 1, 2, 3]);
}

#[test]
fn test_insert_many_long_hint() {
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, MockHintIter{x: [5, 6].iter().cloned(), hint: 1});
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