Add an in-place rotate method for slices to libcore

A helpful primitive for moving chunks of data around inside a slice.
In particular, adding elements to the end of a Vec then moving them
somewhere else, as a way to do efficient multiple-insert.  (There's
drain for efficient block-remove, but no easy way to block-insert.)

Talk with another example: <https://youtu.be/qH6sSOr-yk8?t=560>
This commit is contained in:
Scott McMurray
2017-04-30 23:50:59 -07:00
parent 0bd9e1f5e6
commit c05676b97f
12 changed files with 327 additions and 0 deletions

View File

@@ -51,6 +51,7 @@ use mem;
use marker::{Copy, Send, Sync, Sized, self};
use iter_private::TrustedRandomAccess;
mod rotate;
mod sort;
#[repr(C)]
@@ -202,6 +203,9 @@ pub trait SliceExt {
#[stable(feature = "core", since = "1.6.0")]
fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
#[unstable(feature = "slice_rotate", issue = "123456789")]
fn rotate(&mut self, mid: usize) -> usize;
#[stable(feature = "clone_from_slice", since = "1.7.0")]
fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone;
@@ -635,6 +639,18 @@ impl<T> SliceExt for [T] {
self.binary_search_by(|p| p.borrow().cmp(x))
}
fn rotate(&mut self, mid: usize) -> usize {
assert!(mid <= self.len());
let k = self.len() - mid;
unsafe {
let p = self.as_mut_ptr();
rotate::ptr_rotate(mid, p.offset(mid as isize), k);
}
k
}
#[inline]
fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
assert!(self.len() == src.len(),