]> git.lizzy.rs Git - rust.git/blob - src/liballoc/slice.rs
Stabilize slice::rchunks(), rchunks_mut(), rchunks_exact(), rchunk_exact_mut()
[rust.git] / src / liballoc / slice.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A dynamically-sized view into a contiguous sequence, `[T]`.
12 //!
13 //! *[See also the slice primitive type](../../std/primitive.slice.html).*
14 //!
15 //! Slices are a view into a block of memory represented as a pointer and a
16 //! length.
17 //!
18 //! ```
19 //! // slicing a Vec
20 //! let vec = vec![1, 2, 3];
21 //! let int_slice = &vec[..];
22 //! // coercing an array to a slice
23 //! let str_slice: &[&str] = &["one", "two", "three"];
24 //! ```
25 //!
26 //! Slices are either mutable or shared. The shared slice type is `&[T]`,
27 //! while the mutable slice type is `&mut [T]`, where `T` represents the element
28 //! type. For example, you can mutate the block of memory that a mutable slice
29 //! points to:
30 //!
31 //! ```
32 //! let x = &mut [1, 2, 3];
33 //! x[1] = 7;
34 //! assert_eq!(x, &[1, 7, 3]);
35 //! ```
36 //!
37 //! Here are some of the things this module contains:
38 //!
39 //! ## Structs
40 //!
41 //! There are several structs that are useful for slices, such as [`Iter`], which
42 //! represents iteration over a slice.
43 //!
44 //! ## Trait Implementations
45 //!
46 //! There are several implementations of common traits for slices. Some examples
47 //! include:
48 //!
49 //! * [`Clone`]
50 //! * [`Eq`], [`Ord`] - for slices whose element type are [`Eq`] or [`Ord`].
51 //! * [`Hash`] - for slices whose element type is [`Hash`].
52 //!
53 //! ## Iteration
54 //!
55 //! The slices implement `IntoIterator`. The iterator yields references to the
56 //! slice elements.
57 //!
58 //! ```
59 //! let numbers = &[0, 1, 2];
60 //! for n in numbers {
61 //!     println!("{} is a number!", n);
62 //! }
63 //! ```
64 //!
65 //! The mutable slice yields mutable references to the elements:
66 //!
67 //! ```
68 //! let mut scores = [7, 8, 9];
69 //! for score in &mut scores[..] {
70 //!     *score += 1;
71 //! }
72 //! ```
73 //!
74 //! This iterator yields mutable references to the slice's elements, so while
75 //! the element type of the slice is `i32`, the element type of the iterator is
76 //! `&mut i32`.
77 //!
78 //! * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default
79 //!   iterators.
80 //! * Further methods that return iterators are [`.split`], [`.splitn`],
81 //!   [`.chunks`], [`.windows`] and more.
82 //!
83 //! [`Clone`]: ../../std/clone/trait.Clone.html
84 //! [`Eq`]: ../../std/cmp/trait.Eq.html
85 //! [`Ord`]: ../../std/cmp/trait.Ord.html
86 //! [`Iter`]: struct.Iter.html
87 //! [`Hash`]: ../../std/hash/trait.Hash.html
88 //! [`.iter`]: ../../std/primitive.slice.html#method.iter
89 //! [`.iter_mut`]: ../../std/primitive.slice.html#method.iter_mut
90 //! [`.split`]: ../../std/primitive.slice.html#method.split
91 //! [`.splitn`]: ../../std/primitive.slice.html#method.splitn
92 //! [`.chunks`]: ../../std/primitive.slice.html#method.chunks
93 //! [`.windows`]: ../../std/primitive.slice.html#method.windows
94 #![stable(feature = "rust1", since = "1.0.0")]
95
96 // Many of the usings in this module are only used in the test configuration.
97 // It's cleaner to just turn off the unused_imports warning than to fix them.
98 #![cfg_attr(test, allow(unused_imports, dead_code))]
99
100 use core::cmp::Ordering::{self, Less};
101 use core::mem::size_of;
102 use core::mem;
103 use core::ptr;
104 use core::{u8, u16, u32};
105
106 use borrow::{Borrow, BorrowMut, ToOwned};
107 use boxed::Box;
108 use vec::Vec;
109
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub use core::slice::{Chunks, Windows};
112 #[stable(feature = "rust1", since = "1.0.0")]
113 pub use core::slice::{Iter, IterMut};
114 #[stable(feature = "rust1", since = "1.0.0")]
115 pub use core::slice::{SplitMut, ChunksMut, Split};
116 #[stable(feature = "rust1", since = "1.0.0")]
117 pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut};
118 #[stable(feature = "slice_rsplit", since = "1.27.0")]
119 pub use core::slice::{RSplit, RSplitMut};
120 #[stable(feature = "rust1", since = "1.0.0")]
121 pub use core::slice::{from_raw_parts, from_raw_parts_mut};
122 #[stable(feature = "from_ref", since = "1.28.0")]
123 pub use core::slice::{from_ref, from_mut};
124 #[stable(feature = "slice_get_slice", since = "1.28.0")]
125 pub use core::slice::SliceIndex;
126 #[stable(feature = "chunks_exact", since = "1.31.0")]
127 pub use core::slice::{ChunksExact, ChunksExactMut};
128 #[stable(feature = "rchunks", since = "1.31.0")]
129 pub use core::slice::{RChunks, RChunksMut, RChunksExact, RChunksExactMut};
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // Basic slice extension methods
133 ////////////////////////////////////////////////////////////////////////////////
134
135 // HACK(japaric) needed for the implementation of `vec!` macro during testing
136 // NB see the hack module in this file for more details
137 #[cfg(test)]
138 pub use self::hack::into_vec;
139
140 // HACK(japaric) needed for the implementation of `Vec::clone` during testing
141 // NB see the hack module in this file for more details
142 #[cfg(test)]
143 pub use self::hack::to_vec;
144
145 // HACK(japaric): With cfg(test) `impl [T]` is not available, these three
146 // functions are actually methods that are in `impl [T]` but not in
147 // `core::slice::SliceExt` - we need to supply these functions for the
148 // `test_permutations` test
149 mod hack {
150     use boxed::Box;
151     use core::mem;
152
153     #[cfg(test)]
154     use string::ToString;
155     use vec::Vec;
156
157     pub fn into_vec<T>(mut b: Box<[T]>) -> Vec<T> {
158         unsafe {
159             let xs = Vec::from_raw_parts(b.as_mut_ptr(), b.len(), b.len());
160             mem::forget(b);
161             xs
162         }
163     }
164
165     #[inline]
166     pub fn to_vec<T>(s: &[T]) -> Vec<T>
167         where T: Clone
168     {
169         let mut vector = Vec::with_capacity(s.len());
170         vector.extend_from_slice(s);
171         vector
172     }
173 }
174
175 #[lang = "slice_alloc"]
176 #[cfg(not(test))]
177 impl<T> [T] {
178     /// Sorts the slice.
179     ///
180     /// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
181     ///
182     /// When applicable, unstable sorting is preferred because it is generally faster than stable
183     /// sorting and it doesn't allocate auxiliary memory.
184     /// See [`sort_unstable`](#method.sort_unstable).
185     ///
186     /// # Current implementation
187     ///
188     /// The current algorithm is an adaptive, iterative merge sort inspired by
189     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
190     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
191     /// two or more sorted sequences concatenated one after another.
192     ///
193     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
194     /// non-allocating insertion sort is used instead.
195     ///
196     /// # Examples
197     ///
198     /// ```
199     /// let mut v = [-5, 4, 1, -3, 2];
200     ///
201     /// v.sort();
202     /// assert!(v == [-5, -3, 1, 2, 4]);
203     /// ```
204     #[stable(feature = "rust1", since = "1.0.0")]
205     #[inline]
206     pub fn sort(&mut self)
207         where T: Ord
208     {
209         merge_sort(self, |a, b| a.lt(b));
210     }
211
212     /// Sorts the slice with a comparator function.
213     ///
214     /// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
215     ///
216     /// When applicable, unstable sorting is preferred because it is generally faster than stable
217     /// sorting and it doesn't allocate auxiliary memory.
218     /// See [`sort_unstable_by`](#method.sort_unstable_by).
219     ///
220     /// # Current implementation
221     ///
222     /// The current algorithm is an adaptive, iterative merge sort inspired by
223     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
224     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
225     /// two or more sorted sequences concatenated one after another.
226     ///
227     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
228     /// non-allocating insertion sort is used instead.
229     ///
230     /// # Examples
231     ///
232     /// ```
233     /// let mut v = [5, 4, 1, 3, 2];
234     /// v.sort_by(|a, b| a.cmp(b));
235     /// assert!(v == [1, 2, 3, 4, 5]);
236     ///
237     /// // reverse sorting
238     /// v.sort_by(|a, b| b.cmp(a));
239     /// assert!(v == [5, 4, 3, 2, 1]);
240     /// ```
241     #[stable(feature = "rust1", since = "1.0.0")]
242     #[inline]
243     pub fn sort_by<F>(&mut self, mut compare: F)
244         where F: FnMut(&T, &T) -> Ordering
245     {
246         merge_sort(self, |a, b| compare(a, b) == Less);
247     }
248
249     /// Sorts the slice with a key extraction function.
250     ///
251     /// This sort is stable (i.e. does not reorder equal elements) and `O(m n log(m n))`
252     /// worst-case, where the key function is `O(m)`.
253     ///
254     /// When applicable, unstable sorting is preferred because it is generally faster than stable
255     /// sorting and it doesn't allocate auxiliary memory.
256     /// See [`sort_unstable_by_key`](#method.sort_unstable_by_key).
257     ///
258     /// # Current implementation
259     ///
260     /// The current algorithm is an adaptive, iterative merge sort inspired by
261     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
262     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
263     /// two or more sorted sequences concatenated one after another.
264     ///
265     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
266     /// non-allocating insertion sort is used instead.
267     ///
268     /// # Examples
269     ///
270     /// ```
271     /// let mut v = [-5i32, 4, 1, -3, 2];
272     ///
273     /// v.sort_by_key(|k| k.abs());
274     /// assert!(v == [1, 2, -3, 4, -5]);
275     /// ```
276     #[stable(feature = "slice_sort_by_key", since = "1.7.0")]
277     #[inline]
278     pub fn sort_by_key<K, F>(&mut self, mut f: F)
279         where F: FnMut(&T) -> K, K: Ord
280     {
281         merge_sort(self, |a, b| f(a).lt(&f(b)));
282     }
283
284     /// Sorts the slice with a key extraction function.
285     ///
286     /// During sorting, the key function is called only once per element.
287     ///
288     /// This sort is stable (i.e. does not reorder equal elements) and `O(m n + n log n)`
289     /// worst-case, where the key function is `O(m)`.
290     ///
291     /// For simple key functions (e.g. functions that are property accesses or
292     /// basic operations), [`sort_by_key`](#method.sort_by_key) is likely to be
293     /// faster.
294     ///
295     /// # Current implementation
296     ///
297     /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
298     /// which combines the fast average case of randomized quicksort with the fast worst case of
299     /// heapsort, while achieving linear time on slices with certain patterns. It uses some
300     /// randomization to avoid degenerate cases, but with a fixed seed to always provide
301     /// deterministic behavior.
302     ///
303     /// In the worst case, the algorithm allocates temporary storage in a `Vec<(K, usize)>` the
304     /// length of the slice.
305     ///
306     /// # Examples
307     ///
308     /// ```
309     /// #![feature(slice_sort_by_cached_key)]
310     /// let mut v = [-5i32, 4, 32, -3, 2];
311     ///
312     /// v.sort_by_cached_key(|k| k.to_string());
313     /// assert!(v == [-3, -5, 2, 32, 4]);
314     /// ```
315     ///
316     /// [pdqsort]: https://github.com/orlp/pdqsort
317     #[unstable(feature = "slice_sort_by_cached_key", issue = "34447")]
318     #[inline]
319     pub fn sort_by_cached_key<K, F>(&mut self, f: F)
320         where F: FnMut(&T) -> K, K: Ord
321     {
322         // Helper macro for indexing our vector by the smallest possible type, to reduce allocation.
323         macro_rules! sort_by_key {
324             ($t:ty, $slice:ident, $f:ident) => ({
325                 let mut indices: Vec<_> =
326                     $slice.iter().map($f).enumerate().map(|(i, k)| (k, i as $t)).collect();
327                 // The elements of `indices` are unique, as they are indexed, so any sort will be
328                 // stable with respect to the original slice. We use `sort_unstable` here because
329                 // it requires less memory allocation.
330                 indices.sort_unstable();
331                 for i in 0..$slice.len() {
332                     let mut index = indices[i].1;
333                     while (index as usize) < i {
334                         index = indices[index as usize].1;
335                     }
336                     indices[i].1 = index;
337                     $slice.swap(i, index as usize);
338                 }
339             })
340         }
341
342         let sz_u8    = mem::size_of::<(K, u8)>();
343         let sz_u16   = mem::size_of::<(K, u16)>();
344         let sz_u32   = mem::size_of::<(K, u32)>();
345         let sz_usize = mem::size_of::<(K, usize)>();
346
347         let len = self.len();
348         if len < 2 { return }
349         if sz_u8  < sz_u16   && len <= ( u8::MAX as usize) { return sort_by_key!( u8, self, f) }
350         if sz_u16 < sz_u32   && len <= (u16::MAX as usize) { return sort_by_key!(u16, self, f) }
351         if sz_u32 < sz_usize && len <= (u32::MAX as usize) { return sort_by_key!(u32, self, f) }
352         sort_by_key!(usize, self, f)
353     }
354
355     /// Copies `self` into a new `Vec`.
356     ///
357     /// # Examples
358     ///
359     /// ```
360     /// let s = [10, 40, 30];
361     /// let x = s.to_vec();
362     /// // Here, `s` and `x` can be modified independently.
363     /// ```
364     #[rustc_conversion_suggestion]
365     #[stable(feature = "rust1", since = "1.0.0")]
366     #[inline]
367     pub fn to_vec(&self) -> Vec<T>
368         where T: Clone
369     {
370         // NB see hack module in this file
371         hack::to_vec(self)
372     }
373
374     /// Converts `self` into a vector without clones or allocation.
375     ///
376     /// The resulting vector can be converted back into a box via
377     /// `Vec<T>`'s `into_boxed_slice` method.
378     ///
379     /// # Examples
380     ///
381     /// ```
382     /// let s: Box<[i32]> = Box::new([10, 40, 30]);
383     /// let x = s.into_vec();
384     /// // `s` cannot be used anymore because it has been converted into `x`.
385     ///
386     /// assert_eq!(x, vec![10, 40, 30]);
387     /// ```
388     #[stable(feature = "rust1", since = "1.0.0")]
389     #[inline]
390     pub fn into_vec(self: Box<Self>) -> Vec<T> {
391         // NB see hack module in this file
392         hack::into_vec(self)
393     }
394
395     /// Creates a vector by repeating a slice `n` times.
396     ///
397     /// # Panics
398     ///
399     /// This function will panic if the capacity would overflow.
400     ///
401     /// # Examples
402     ///
403     /// Basic usage:
404     ///
405     /// ```
406     /// #![feature(repeat_generic_slice)]
407     ///
408     /// fn main() {
409     ///     assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]);
410     /// }
411     /// ```
412     ///
413     /// A panic upon overflow:
414     ///
415     /// ```should_panic
416     /// #![feature(repeat_generic_slice)]
417     /// fn main() {
418     ///     // this will panic at runtime
419     ///     b"0123456789abcdef".repeat(usize::max_value());
420     /// }
421     /// ```
422     #[unstable(feature = "repeat_generic_slice",
423                reason = "it's on str, why not on slice?",
424                issue = "48784")]
425     pub fn repeat(&self, n: usize) -> Vec<T> where T: Copy {
426         if n == 0 {
427             return Vec::new();
428         }
429
430         // If `n` is larger than zero, it can be split as
431         // `n = 2^expn + rem (2^expn > rem, expn >= 0, rem >= 0)`.
432         // `2^expn` is the number represented by the leftmost '1' bit of `n`,
433         // and `rem` is the remaining part of `n`.
434
435         // Using `Vec` to access `set_len()`.
436         let mut buf = Vec::with_capacity(self.len().checked_mul(n).expect("capacity overflow"));
437
438         // `2^expn` repetition is done by doubling `buf` `expn`-times.
439         buf.extend(self);
440         {
441             let mut m = n >> 1;
442             // If `m > 0`, there are remaining bits up to the leftmost '1'.
443             while m > 0 {
444                 // `buf.extend(buf)`:
445                 unsafe {
446                     ptr::copy_nonoverlapping(
447                         buf.as_ptr(),
448                         (buf.as_mut_ptr() as *mut T).add(buf.len()),
449                         buf.len(),
450                     );
451                     // `buf` has capacity of `self.len() * n`.
452                     let buf_len = buf.len();
453                     buf.set_len(buf_len * 2);
454                 }
455
456                 m >>= 1;
457             }
458         }
459
460         // `rem` (`= n - 2^expn`) repetition is done by copying
461         // first `rem` repetitions from `buf` itself.
462         let rem_len = self.len() * n - buf.len(); // `self.len() * rem`
463         if rem_len > 0 {
464             // `buf.extend(buf[0 .. rem_len])`:
465             unsafe {
466                 // This is non-overlapping since `2^expn > rem`.
467                 ptr::copy_nonoverlapping(
468                     buf.as_ptr(),
469                     (buf.as_mut_ptr() as *mut T).add(buf.len()),
470                     rem_len,
471                 );
472                 // `buf.len() + rem_len` equals to `buf.capacity()` (`= self.len() * n`).
473                 let buf_cap = buf.capacity();
474                 buf.set_len(buf_cap);
475             }
476         }
477         buf
478     }
479 }
480
481 #[lang = "slice_u8_alloc"]
482 #[cfg(not(test))]
483 impl [u8] {
484     /// Returns a vector containing a copy of this slice where each byte
485     /// is mapped to its ASCII upper case equivalent.
486     ///
487     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
488     /// but non-ASCII letters are unchanged.
489     ///
490     /// To uppercase the value in-place, use [`make_ascii_uppercase`].
491     ///
492     /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
493     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
494     #[inline]
495     pub fn to_ascii_uppercase(&self) -> Vec<u8> {
496         let mut me = self.to_vec();
497         me.make_ascii_uppercase();
498         me
499     }
500
501     /// Returns a vector containing a copy of this slice where each byte
502     /// is mapped to its ASCII lower case equivalent.
503     ///
504     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
505     /// but non-ASCII letters are unchanged.
506     ///
507     /// To lowercase the value in-place, use [`make_ascii_lowercase`].
508     ///
509     /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
510     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
511     #[inline]
512     pub fn to_ascii_lowercase(&self) -> Vec<u8> {
513         let mut me = self.to_vec();
514         me.make_ascii_lowercase();
515         me
516     }
517 }
518
519 ////////////////////////////////////////////////////////////////////////////////
520 // Extension traits for slices over specific kinds of data
521 ////////////////////////////////////////////////////////////////////////////////
522 #[unstable(feature = "slice_concat_ext",
523            reason = "trait should not have to exist",
524            issue = "27747")]
525 /// An extension trait for concatenating slices
526 ///
527 /// While this trait is unstable, the methods are stable. `SliceConcatExt` is
528 /// included in the [standard library prelude], so you can use [`join()`] and
529 /// [`concat()`] as if they existed on `[T]` itself.
530 ///
531 /// [standard library prelude]: ../../std/prelude/index.html
532 /// [`join()`]: #tymethod.join
533 /// [`concat()`]: #tymethod.concat
534 pub trait SliceConcatExt<T: ?Sized> {
535     #[unstable(feature = "slice_concat_ext",
536                reason = "trait should not have to exist",
537                issue = "27747")]
538     /// The resulting type after concatenation
539     type Output;
540
541     /// Flattens a slice of `T` into a single value `Self::Output`.
542     ///
543     /// # Examples
544     ///
545     /// ```
546     /// assert_eq!(["hello", "world"].concat(), "helloworld");
547     /// assert_eq!([[1, 2], [3, 4]].concat(), [1, 2, 3, 4]);
548     /// ```
549     #[stable(feature = "rust1", since = "1.0.0")]
550     fn concat(&self) -> Self::Output;
551
552     /// Flattens a slice of `T` into a single value `Self::Output`, placing a
553     /// given separator between each.
554     ///
555     /// # Examples
556     ///
557     /// ```
558     /// assert_eq!(["hello", "world"].join(" "), "hello world");
559     /// assert_eq!([[1, 2], [3, 4]].join(&0), [1, 2, 0, 3, 4]);
560     /// ```
561     #[stable(feature = "rename_connect_to_join", since = "1.3.0")]
562     fn join(&self, sep: &T) -> Self::Output;
563
564     #[stable(feature = "rust1", since = "1.0.0")]
565     #[rustc_deprecated(since = "1.3.0", reason = "renamed to join")]
566     fn connect(&self, sep: &T) -> Self::Output;
567 }
568
569 #[unstable(feature = "slice_concat_ext",
570            reason = "trait should not have to exist",
571            issue = "27747")]
572 impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
573     type Output = Vec<T>;
574
575     fn concat(&self) -> Vec<T> {
576         let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
577         let mut result = Vec::with_capacity(size);
578         for v in self {
579             result.extend_from_slice(v.borrow())
580         }
581         result
582     }
583
584     fn join(&self, sep: &T) -> Vec<T> {
585         let mut iter = self.iter();
586         let first = match iter.next() {
587             Some(first) => first,
588             None => return vec![],
589         };
590         let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
591         let mut result = Vec::with_capacity(size + self.len());
592         result.extend_from_slice(first.borrow());
593
594         for v in iter {
595             result.push(sep.clone());
596             result.extend_from_slice(v.borrow())
597         }
598         result
599     }
600
601     fn connect(&self, sep: &T) -> Vec<T> {
602         self.join(sep)
603     }
604 }
605
606 ////////////////////////////////////////////////////////////////////////////////
607 // Standard trait implementations for slices
608 ////////////////////////////////////////////////////////////////////////////////
609
610 #[stable(feature = "rust1", since = "1.0.0")]
611 impl<T> Borrow<[T]> for Vec<T> {
612     fn borrow(&self) -> &[T] {
613         &self[..]
614     }
615 }
616
617 #[stable(feature = "rust1", since = "1.0.0")]
618 impl<T> BorrowMut<[T]> for Vec<T> {
619     fn borrow_mut(&mut self) -> &mut [T] {
620         &mut self[..]
621     }
622 }
623
624 #[stable(feature = "rust1", since = "1.0.0")]
625 impl<T: Clone> ToOwned for [T] {
626     type Owned = Vec<T>;
627     #[cfg(not(test))]
628     fn to_owned(&self) -> Vec<T> {
629         self.to_vec()
630     }
631
632     #[cfg(test)]
633     fn to_owned(&self) -> Vec<T> {
634         hack::to_vec(self)
635     }
636
637     fn clone_into(&self, target: &mut Vec<T>) {
638         // drop anything in target that will not be overwritten
639         target.truncate(self.len());
640         let len = target.len();
641
642         // reuse the contained values' allocations/resources.
643         target.clone_from_slice(&self[..len]);
644
645         // target.len <= self.len due to the truncate above, so the
646         // slice here is always in-bounds.
647         target.extend_from_slice(&self[len..]);
648     }
649 }
650
651 ////////////////////////////////////////////////////////////////////////////////
652 // Sorting
653 ////////////////////////////////////////////////////////////////////////////////
654
655 /// Inserts `v[0]` into pre-sorted sequence `v[1..]` so that whole `v[..]` becomes sorted.
656 ///
657 /// This is the integral subroutine of insertion sort.
658 fn insert_head<T, F>(v: &mut [T], is_less: &mut F)
659     where F: FnMut(&T, &T) -> bool
660 {
661     if v.len() >= 2 && is_less(&v[1], &v[0]) {
662         unsafe {
663             // There are three ways to implement insertion here:
664             //
665             // 1. Swap adjacent elements until the first one gets to its final destination.
666             //    However, this way we copy data around more than is necessary. If elements are big
667             //    structures (costly to copy), this method will be slow.
668             //
669             // 2. Iterate until the right place for the first element is found. Then shift the
670             //    elements succeeding it to make room for it and finally place it into the
671             //    remaining hole. This is a good method.
672             //
673             // 3. Copy the first element into a temporary variable. Iterate until the right place
674             //    for it is found. As we go along, copy every traversed element into the slot
675             //    preceding it. Finally, copy data from the temporary variable into the remaining
676             //    hole. This method is very good. Benchmarks demonstrated slightly better
677             //    performance than with the 2nd method.
678             //
679             // All methods were benchmarked, and the 3rd showed best results. So we chose that one.
680             let mut tmp = mem::ManuallyDrop::new(ptr::read(&v[0]));
681
682             // Intermediate state of the insertion process is always tracked by `hole`, which
683             // serves two purposes:
684             // 1. Protects integrity of `v` from panics in `is_less`.
685             // 2. Fills the remaining hole in `v` in the end.
686             //
687             // Panic safety:
688             //
689             // If `is_less` panics at any point during the process, `hole` will get dropped and
690             // fill the hole in `v` with `tmp`, thus ensuring that `v` still holds every object it
691             // initially held exactly once.
692             let mut hole = InsertionHole {
693                 src: &mut *tmp,
694                 dest: &mut v[1],
695             };
696             ptr::copy_nonoverlapping(&v[1], &mut v[0], 1);
697
698             for i in 2..v.len() {
699                 if !is_less(&v[i], &*tmp) {
700                     break;
701                 }
702                 ptr::copy_nonoverlapping(&v[i], &mut v[i - 1], 1);
703                 hole.dest = &mut v[i];
704             }
705             // `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
706         }
707     }
708
709     // When dropped, copies from `src` into `dest`.
710     struct InsertionHole<T> {
711         src: *mut T,
712         dest: *mut T,
713     }
714
715     impl<T> Drop for InsertionHole<T> {
716         fn drop(&mut self) {
717             unsafe { ptr::copy_nonoverlapping(self.src, self.dest, 1); }
718         }
719     }
720 }
721
722 /// Merges non-decreasing runs `v[..mid]` and `v[mid..]` using `buf` as temporary storage, and
723 /// stores the result into `v[..]`.
724 ///
725 /// # Safety
726 ///
727 /// The two slices must be non-empty and `mid` must be in bounds. Buffer `buf` must be long enough
728 /// to hold a copy of the shorter slice. Also, `T` must not be a zero-sized type.
729 unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
730     where F: FnMut(&T, &T) -> bool
731 {
732     let len = v.len();
733     let v = v.as_mut_ptr();
734     let v_mid = v.add(mid);
735     let v_end = v.add(len);
736
737     // The merge process first copies the shorter run into `buf`. Then it traces the newly copied
738     // run and the longer run forwards (or backwards), comparing their next unconsumed elements and
739     // copying the lesser (or greater) one into `v`.
740     //
741     // As soon as the shorter run is fully consumed, the process is done. If the longer run gets
742     // consumed first, then we must copy whatever is left of the shorter run into the remaining
743     // hole in `v`.
744     //
745     // Intermediate state of the process is always tracked by `hole`, which serves two purposes:
746     // 1. Protects integrity of `v` from panics in `is_less`.
747     // 2. Fills the remaining hole in `v` if the longer run gets consumed first.
748     //
749     // Panic safety:
750     //
751     // If `is_less` panics at any point during the process, `hole` will get dropped and fill the
752     // hole in `v` with the unconsumed range in `buf`, thus ensuring that `v` still holds every
753     // object it initially held exactly once.
754     let mut hole;
755
756     if mid <= len - mid {
757         // The left run is shorter.
758         ptr::copy_nonoverlapping(v, buf, mid);
759         hole = MergeHole {
760             start: buf,
761             end: buf.add(mid),
762             dest: v,
763         };
764
765         // Initially, these pointers point to the beginnings of their arrays.
766         let left = &mut hole.start;
767         let mut right = v_mid;
768         let out = &mut hole.dest;
769
770         while *left < hole.end && right < v_end {
771             // Consume the lesser side.
772             // If equal, prefer the left run to maintain stability.
773             let to_copy = if is_less(&*right, &**left) {
774                 get_and_increment(&mut right)
775             } else {
776                 get_and_increment(left)
777             };
778             ptr::copy_nonoverlapping(to_copy, get_and_increment(out), 1);
779         }
780     } else {
781         // The right run is shorter.
782         ptr::copy_nonoverlapping(v_mid, buf, len - mid);
783         hole = MergeHole {
784             start: buf,
785             end: buf.add(len - mid),
786             dest: v_mid,
787         };
788
789         // Initially, these pointers point past the ends of their arrays.
790         let left = &mut hole.dest;
791         let right = &mut hole.end;
792         let mut out = v_end;
793
794         while v < *left && buf < *right {
795             // Consume the greater side.
796             // If equal, prefer the right run to maintain stability.
797             let to_copy = if is_less(&*right.offset(-1), &*left.offset(-1)) {
798                 decrement_and_get(left)
799             } else {
800                 decrement_and_get(right)
801             };
802             ptr::copy_nonoverlapping(to_copy, decrement_and_get(&mut out), 1);
803         }
804     }
805     // Finally, `hole` gets dropped. If the shorter run was not fully consumed, whatever remains of
806     // it will now be copied into the hole in `v`.
807
808     unsafe fn get_and_increment<T>(ptr: &mut *mut T) -> *mut T {
809         let old = *ptr;
810         *ptr = ptr.offset(1);
811         old
812     }
813
814     unsafe fn decrement_and_get<T>(ptr: &mut *mut T) -> *mut T {
815         *ptr = ptr.offset(-1);
816         *ptr
817     }
818
819     // When dropped, copies the range `start..end` into `dest..`.
820     struct MergeHole<T> {
821         start: *mut T,
822         end: *mut T,
823         dest: *mut T,
824     }
825
826     impl<T> Drop for MergeHole<T> {
827         fn drop(&mut self) {
828             // `T` is not a zero-sized type, so it's okay to divide by its size.
829             let len = (self.end as usize - self.start as usize) / mem::size_of::<T>();
830             unsafe { ptr::copy_nonoverlapping(self.start, self.dest, len); }
831         }
832     }
833 }
834
835 /// This merge sort borrows some (but not all) ideas from TimSort, which is described in detail
836 /// [here](http://svn.python.org/projects/python/trunk/Objects/listsort.txt).
837 ///
838 /// The algorithm identifies strictly descending and non-descending subsequences, which are called
839 /// natural runs. There is a stack of pending runs yet to be merged. Each newly found run is pushed
840 /// onto the stack, and then some pairs of adjacent runs are merged until these two invariants are
841 /// satisfied:
842 ///
843 /// 1. for every `i` in `1..runs.len()`: `runs[i - 1].len > runs[i].len`
844 /// 2. for every `i` in `2..runs.len()`: `runs[i - 2].len > runs[i - 1].len + runs[i].len`
845 ///
846 /// The invariants ensure that the total running time is `O(n log n)` worst-case.
847 fn merge_sort<T, F>(v: &mut [T], mut is_less: F)
848     where F: FnMut(&T, &T) -> bool
849 {
850     // Slices of up to this length get sorted using insertion sort.
851     const MAX_INSERTION: usize = 20;
852     // Very short runs are extended using insertion sort to span at least this many elements.
853     const MIN_RUN: usize = 10;
854
855     // Sorting has no meaningful behavior on zero-sized types.
856     if size_of::<T>() == 0 {
857         return;
858     }
859
860     let len = v.len();
861
862     // Short arrays get sorted in-place via insertion sort to avoid allocations.
863     if len <= MAX_INSERTION {
864         if len >= 2 {
865             for i in (0..len-1).rev() {
866                 insert_head(&mut v[i..], &mut is_less);
867             }
868         }
869         return;
870     }
871
872     // Allocate a buffer to use as scratch memory. We keep the length 0 so we can keep in it
873     // shallow copies of the contents of `v` without risking the dtors running on copies if
874     // `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
875     // which will always have length at most `len / 2`.
876     let mut buf = Vec::with_capacity(len / 2);
877
878     // In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
879     // strange decision, but consider the fact that merges more often go in the opposite direction
880     // (forwards). According to benchmarks, merging forwards is slightly faster than merging
881     // backwards. To conclude, identifying runs by traversing backwards improves performance.
882     let mut runs = vec![];
883     let mut end = len;
884     while end > 0 {
885         // Find the next natural run, and reverse it if it's strictly descending.
886         let mut start = end - 1;
887         if start > 0 {
888             start -= 1;
889             unsafe {
890                 if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
891                     while start > 0 && is_less(v.get_unchecked(start),
892                                                v.get_unchecked(start - 1)) {
893                         start -= 1;
894                     }
895                     v[start..end].reverse();
896                 } else {
897                     while start > 0 && !is_less(v.get_unchecked(start),
898                                                 v.get_unchecked(start - 1)) {
899                         start -= 1;
900                     }
901                 }
902             }
903         }
904
905         // Insert some more elements into the run if it's too short. Insertion sort is faster than
906         // merge sort on short sequences, so this significantly improves performance.
907         while start > 0 && end - start < MIN_RUN {
908             start -= 1;
909             insert_head(&mut v[start..end], &mut is_less);
910         }
911
912         // Push this run onto the stack.
913         runs.push(Run {
914             start,
915             len: end - start,
916         });
917         end = start;
918
919         // Merge some pairs of adjacent runs to satisfy the invariants.
920         while let Some(r) = collapse(&runs) {
921             let left = runs[r + 1];
922             let right = runs[r];
923             unsafe {
924                 merge(&mut v[left.start .. right.start + right.len], left.len, buf.as_mut_ptr(),
925                       &mut is_less);
926             }
927             runs[r] = Run {
928                 start: left.start,
929                 len: left.len + right.len,
930             };
931             runs.remove(r + 1);
932         }
933     }
934
935     // Finally, exactly one run must remain in the stack.
936     debug_assert!(runs.len() == 1 && runs[0].start == 0 && runs[0].len == len);
937
938     // Examines the stack of runs and identifies the next pair of runs to merge. More specifically,
939     // if `Some(r)` is returned, that means `runs[r]` and `runs[r + 1]` must be merged next. If the
940     // algorithm should continue building a new run instead, `None` is returned.
941     //
942     // TimSort is infamous for its buggy implementations, as described here:
943     // http://envisage-project.eu/timsort-specification-and-verification/
944     //
945     // The gist of the story is: we must enforce the invariants on the top four runs on the stack.
946     // Enforcing them on just top three is not sufficient to ensure that the invariants will still
947     // hold for *all* runs in the stack.
948     //
949     // This function correctly checks invariants for the top four runs. Additionally, if the top
950     // run starts at index 0, it will always demand a merge operation until the stack is fully
951     // collapsed, in order to complete the sort.
952     #[inline]
953     fn collapse(runs: &[Run]) -> Option<usize> {
954         let n = runs.len();
955         if n >= 2 && (runs[n - 1].start == 0 ||
956                       runs[n - 2].len <= runs[n - 1].len ||
957                       (n >= 3 && runs[n - 3].len <= runs[n - 2].len + runs[n - 1].len) ||
958                       (n >= 4 && runs[n - 4].len <= runs[n - 3].len + runs[n - 2].len)) {
959             if n >= 3 && runs[n - 3].len < runs[n - 1].len {
960                 Some(n - 3)
961             } else {
962                 Some(n - 2)
963             }
964         } else {
965             None
966         }
967     }
968
969     #[derive(Clone, Copy)]
970     struct Run {
971         start: usize,
972         len: usize,
973     }
974 }