|
|
|
@ -3,18 +3,26 @@ use super::*;
|
|
|
|
|
|
|
|
|
|
pub trait VecExt<T>
|
|
|
|
|
{
|
|
|
|
|
/// Insert many elements
|
|
|
|
|
fn move_insert<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
|
|
|
|
|
/// Insert many elements with exact size iterator
|
|
|
|
|
fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
|
|
|
|
|
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe;
|
|
|
|
|
|
|
|
|
|
/// Insert many elements
|
|
|
|
|
fn splice_insert<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I);
|
|
|
|
|
fn insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> VecExt<T> for Vec<T>
|
|
|
|
|
{
|
|
|
|
|
fn move_insert<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
|
|
|
|
|
#[cfg(not(feature="experimental_inserter"))]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
|
|
|
|
|
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe
|
|
|
|
|
{
|
|
|
|
|
self.insert_many(location, slice)
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature="experimental_inserter")]
|
|
|
|
|
fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
|
|
|
|
|
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe,
|
|
|
|
|
{
|
|
|
|
|
#[inline(never)]
|
|
|
|
@ -79,7 +87,7 @@ impl<T> VecExt<T> for Vec<T>
|
|
|
|
|
self.set_len(len + sent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn splice_insert<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I)
|
|
|
|
|
fn insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I)
|
|
|
|
|
{
|
|
|
|
|
let slice = slice.into_iter();
|
|
|
|
|
match slice.size_hint() {
|
|
|
|
@ -98,17 +106,17 @@ mod tests
|
|
|
|
|
{
|
|
|
|
|
use super::*;
|
|
|
|
|
#[test]
|
|
|
|
|
fn vec_move_insert()
|
|
|
|
|
fn vec_insert_exact()
|
|
|
|
|
{
|
|
|
|
|
let mut vec = vec![0,1,2,8,9,10];
|
|
|
|
|
vec.move_insert(3, [3,4,5,6, 7].iter().copied());
|
|
|
|
|
vec.insert_exact(3, [3,4,5,6, 7].iter().copied());
|
|
|
|
|
|
|
|
|
|
assert_eq!(&vec[..],
|
|
|
|
|
&[0,1,2,3,4,5,6,7,8,9,10]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn vec_move_insert_nt()
|
|
|
|
|
fn vec_insert_exact_nt()
|
|
|
|
|
{
|
|
|
|
|
macro_rules! string {
|
|
|
|
|
($str:literal) => (String::from($str));
|
|
|
|
@ -128,7 +136,7 @@ mod tests
|
|
|
|
|
string!("uhh"),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
vec.move_insert(2, vec![string!("hi"), string!("hello")]);
|
|
|
|
|
vec.insert_exact(2, vec![string!("hi"), string!("hello")]);
|
|
|
|
|
|
|
|
|
|
assert_eq!(&vec[..], &vec2[..]);
|
|
|
|
|
}
|
|
|
|
@ -140,6 +148,16 @@ mod tests
|
|
|
|
|
use test::{
|
|
|
|
|
Bencher, black_box,
|
|
|
|
|
};
|
|
|
|
|
#[cfg(not(feature="experimental_inserter"))]
|
|
|
|
|
#[bench]
|
|
|
|
|
fn move_exact(b: &mut Bencher)
|
|
|
|
|
{
|
|
|
|
|
let mut vec = vec![0,10,11,12];
|
|
|
|
|
let span = [0,1,2,3];
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
black_box(vec.insert_exact(2, span.iter().copied()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#[bench]
|
|
|
|
|
fn move_via_splice(b: &mut Bencher)
|
|
|
|
|
{
|
|
|
|
@ -147,18 +165,18 @@ mod tests
|
|
|
|
|
let span = [0,1,2,3];
|
|
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
black_box(vec.splice_insert(2, span.iter().copied()));
|
|
|
|
|
black_box(vec.insert_many(2, span.iter().copied()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature="experimental_inserter")]
|
|
|
|
|
#[bench]
|
|
|
|
|
fn move_via_unsafe(b: &mut Bencher)
|
|
|
|
|
{
|
|
|
|
|
let mut vec = vec![0,10,11,12];
|
|
|
|
|
let span = [0,1,2,3];
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
black_box(vec.move_insert(2, span.iter().copied()));
|
|
|
|
|
black_box(vec.insert_exact(2, span.iter().copied()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|