You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
4.2 KiB
183 lines
4.2 KiB
//! Extensions
|
|
use super::*;
|
|
|
|
pub trait VecExt<T>
|
|
{
|
|
/// 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 insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I);
|
|
}
|
|
|
|
|
|
impl<T> VecExt<T> for Vec<T>
|
|
{
|
|
#[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)]
|
|
#[cold]
|
|
fn panic_len(l1: usize, l2: usize) -> !
|
|
{
|
|
panic!("Location must be in range 0..{}, got {}", l1,l2)
|
|
}
|
|
|
|
#[inline(never)]
|
|
#[cold]
|
|
fn inv_sz() -> !
|
|
{
|
|
panic!("ExactSizeIterator returned invalid size");
|
|
}
|
|
|
|
if location >= self.len() {
|
|
panic_len(self.len(), location);
|
|
}
|
|
let mut slice = slice.into_iter();
|
|
|
|
let slen = slice.len();
|
|
match slen {
|
|
0 => return,
|
|
1 => {
|
|
self.insert(location, slice.next().unwrap());
|
|
return
|
|
},
|
|
_ => (),
|
|
};
|
|
self.reserve(slice.len());
|
|
|
|
unsafe {
|
|
let this = self.as_mut_ptr().add(location);
|
|
let len = self.len();
|
|
let rest = std::mem::size_of::<T>() * (location..len).len();
|
|
|
|
libc::memmove(this.add(slen) as *mut libc::c_void, this as *mut libc::c_void, rest);
|
|
let mut sent=0;
|
|
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
let mut this = this;
|
|
for item in slice {
|
|
if sent >= slen {
|
|
inv_sz();
|
|
}
|
|
|
|
this.write(item);
|
|
this = this.add(1);
|
|
sent+=1;
|
|
}
|
|
if sent != slen {
|
|
inv_sz();
|
|
}
|
|
})) {
|
|
Err(e) => {
|
|
// memory at (location+sent)..slen is now invalid, move the old one back before allowing unwind to contine
|
|
libc::memmove(this.add(sent) as *mut libc::c_void, this.add(slen) as *mut libc::c_void, rest);
|
|
std::panic::resume_unwind(e)
|
|
},
|
|
_ => (),
|
|
}
|
|
self.set_len(len + sent);
|
|
}
|
|
}
|
|
fn insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I)
|
|
{
|
|
let slice = slice.into_iter();
|
|
match slice.size_hint() {
|
|
(0, Some(0)) | (0, None) => (),
|
|
(_, Some(bound)) | (bound, _) => self.reserve(bound),
|
|
};
|
|
// shift everything across, replacing with the new values
|
|
let splice: Vec<_> = self.splice(location.., slice).collect();
|
|
// add tail back
|
|
self.extend(splice);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests
|
|
{
|
|
use super::*;
|
|
#[test]
|
|
fn vec_insert_exact()
|
|
{
|
|
let mut vec = vec![0,1,2,8,9,10];
|
|
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_insert_exact_nt()
|
|
{
|
|
macro_rules! string {
|
|
($str:literal) => (String::from($str));
|
|
}
|
|
let mut vec = vec![
|
|
string!("Hello"),
|
|
string!("world"),
|
|
string!("foo"),
|
|
string!("uhh"),
|
|
];
|
|
let vec2 = vec![
|
|
string!("Hello"),
|
|
string!("world"),
|
|
string!("hi"),
|
|
string!("hello"),
|
|
string!("foo"),
|
|
string!("uhh"),
|
|
];
|
|
|
|
vec.insert_exact(2, vec![string!("hi"), string!("hello")]);
|
|
|
|
assert_eq!(&vec[..], &vec2[..]);
|
|
}
|
|
|
|
#[cfg(nightly)]
|
|
mod benchmatks
|
|
{
|
|
use super::super::*;
|
|
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)
|
|
{
|
|
let mut vec = vec![0,10,11,12];
|
|
let span = [0,1,2,3];
|
|
|
|
b.iter(|| {
|
|
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.insert_exact(2, span.iter().copied()));
|
|
});
|
|
}
|
|
}
|
|
}
|