slice.rs 43 KB

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