]> git.lizzy.rs Git - rust.git/blob - src/libcore/slice/sort.rs
libsyntax/parse: improve associated item error reporting
[rust.git] / src / libcore / slice / sort.rs
1 // Copyright 2017 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 //! Slice sorting
12 //!
13 //! This module contains an sort algorithm based on Orson Peters' pattern-defeating quicksort,
14 //! published at: https://github.com/orlp/pdqsort
15 //!
16 //! Unstable sorting is compatible with libcore because it doesn't allocate memory, unlike our
17 //! stable sorting implementation.
18
19 use cmp;
20 use mem;
21 use ptr;
22
23 /// Holds a value, but never drops it.
24 #[allow(unions_with_drop_fields)]
25 union NoDrop<T> {
26     value: T
27 }
28
29 /// When dropped, copies from `src` into `dest`.
30 struct CopyOnDrop<T> {
31     src: *mut T,
32     dest: *mut T,
33 }
34
35 impl<T> Drop for CopyOnDrop<T> {
36     fn drop(&mut self) {
37         unsafe { ptr::copy_nonoverlapping(self.src, self.dest, 1); }
38     }
39 }
40
41 /// Shifts the first element to the right until it encounters a greater or equal element.
42 fn shift_head<T, F>(v: &mut [T], is_less: &mut F)
43     where F: FnMut(&T, &T) -> bool
44 {
45     let len = v.len();
46     unsafe {
47         // If the first two elements are out-of-order...
48         if len >= 2 && is_less(v.get_unchecked(1), v.get_unchecked(0)) {
49             // Read the first element into a stack-allocated variable. If a following comparison
50             // operation panics, `hole` will get dropped and automatically write the element back
51             // into the slice.
52             let mut tmp = NoDrop { value: ptr::read(v.get_unchecked(0)) };
53             let mut hole = CopyOnDrop {
54                 src: &mut tmp.value,
55                 dest: v.get_unchecked_mut(1),
56             };
57             ptr::copy_nonoverlapping(v.get_unchecked(1), v.get_unchecked_mut(0), 1);
58
59             for i in 2..len {
60                 if !is_less(v.get_unchecked(i), &tmp.value) {
61                     break;
62                 }
63
64                 // Move `i`-th element one place to the left, thus shifting the hole to the right.
65                 ptr::copy_nonoverlapping(v.get_unchecked(i), v.get_unchecked_mut(i - 1), 1);
66                 hole.dest = v.get_unchecked_mut(i);
67             }
68             // `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
69         }
70     }
71 }
72
73 /// Shifts the last element to the left until it encounters a smaller or equal element.
74 fn shift_tail<T, F>(v: &mut [T], is_less: &mut F)
75     where F: FnMut(&T, &T) -> bool
76 {
77     let len = v.len();
78     unsafe {
79         // If the last two elements are out-of-order...
80         if len >= 2 && is_less(v.get_unchecked(len - 1), v.get_unchecked(len - 2)) {
81             // Read the last element into a stack-allocated variable. If a following comparison
82             // operation panics, `hole` will get dropped and automatically write the element back
83             // into the slice.
84             let mut tmp = NoDrop { value: ptr::read(v.get_unchecked(len - 1)) };
85             let mut hole = CopyOnDrop {
86                 src: &mut tmp.value,
87                 dest: v.get_unchecked_mut(len - 2),
88             };
89             ptr::copy_nonoverlapping(v.get_unchecked(len - 2), v.get_unchecked_mut(len - 1), 1);
90
91             for i in (0..len-2).rev() {
92                 if !is_less(&tmp.value, v.get_unchecked(i)) {
93                     break;
94                 }
95
96                 // Move `i`-th element one place to the right, thus shifting the hole to the left.
97                 ptr::copy_nonoverlapping(v.get_unchecked(i), v.get_unchecked_mut(i + 1), 1);
98                 hole.dest = v.get_unchecked_mut(i);
99             }
100             // `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
101         }
102     }
103 }
104
105 /// Partially sorts a slice by shifting several out-of-order elements around.
106 ///
107 /// Returns `true` if the slice is sorted at the end. This function is `O(n)` worst-case.
108 #[cold]
109 fn partial_insertion_sort<T, F>(v: &mut [T], is_less: &mut F) -> bool
110     where F: FnMut(&T, &T) -> bool
111 {
112     // Maximum number of adjacent out-of-order pairs that will get shifted.
113     const MAX_STEPS: usize = 5;
114     // If the slice is shorter than this, don't shift any elements.
115     const SHORTEST_SHIFTING: usize = 50;
116
117     let len = v.len();
118     let mut i = 1;
119
120     for _ in 0..MAX_STEPS {
121         unsafe {
122             // Find the next pair of adjacent out-of-order elements.
123             while i < len && !is_less(v.get_unchecked(i), v.get_unchecked(i - 1)) {
124                 i += 1;
125             }
126         }
127
128         // Are we done?
129         if i == len {
130             return true;
131         }
132
133         // Don't shift elements on short arrays, that has a performance cost.
134         if len < SHORTEST_SHIFTING {
135             return false;
136         }
137
138         // Swap the found pair of elements. This puts them in correct order.
139         v.swap(i - 1, i);
140
141         // Shift the smaller element to the left.
142         shift_tail(&mut v[..i], is_less);
143         // Shift the greater element to the right.
144         shift_head(&mut v[i..], is_less);
145     }
146
147     // Didn't manage to sort the slice in the limited number of steps.
148     false
149 }
150
151 /// Sorts a slice using insertion sort, which is `O(n^2)` worst-case.
152 fn insertion_sort<T, F>(v: &mut [T], is_less: &mut F)
153     where F: FnMut(&T, &T) -> bool
154 {
155     for i in 1..v.len() {
156         shift_tail(&mut v[..i+1], is_less);
157     }
158 }
159
160 /// Sorts `v` using heapsort, which guarantees `O(n log n)` worst-case.
161 #[cold]
162 pub fn heapsort<T, F>(v: &mut [T], is_less: &mut F)
163     where F: FnMut(&T, &T) -> bool
164 {
165     // This binary heap respects the invariant `parent >= child`.
166     let mut sift_down = |v: &mut [T], mut node| {
167         loop {
168             // Children of `node`:
169             let left = 2 * node + 1;
170             let right = 2 * node + 2;
171
172             // Choose the greater child.
173             let greater = if right < v.len() && is_less(&v[left], &v[right]) {
174                 right
175             } else {
176                 left
177             };
178
179             // Stop if the invariant holds at `node`.
180             if greater >= v.len() || !is_less(&v[node], &v[greater]) {
181                 break;
182             }
183
184             // Swap `node` with the greater child, move one step down, and continue sifting.
185             v.swap(node, greater);
186             node = greater;
187         }
188     };
189
190     // Build the heap in linear time.
191     for i in (0 .. v.len() / 2).rev() {
192         sift_down(v, i);
193     }
194
195     // Pop maximal elements from the heap.
196     for i in (1 .. v.len()).rev() {
197         v.swap(0, i);
198         sift_down(&mut v[..i], 0);
199     }
200 }
201
202 /// Partitions `v` into elements smaller than `pivot`, followed by elements greater than or equal
203 /// to `pivot`.
204 ///
205 /// Returns the number of elements smaller than `pivot`.
206 ///
207 /// Partitioning is performed block-by-block in order to minimize the cost of branching operations.
208 /// This idea is presented in the [BlockQuicksort][pdf] paper.
209 ///
210 /// [pdf]: http://drops.dagstuhl.de/opus/volltexte/2016/6389/pdf/LIPIcs-ESA-2016-38.pdf
211 fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
212     where F: FnMut(&T, &T) -> bool
213 {
214     // Number of elements in a typical block.
215     const BLOCK: usize = 128;
216
217     // The partitioning algorithm repeats the following steps until completion:
218     //
219     // 1. Trace a block from the left side to identify elements greater than or equal to the pivot.
220     // 2. Trace a block from the right side to identify elements smaller than the pivot.
221     // 3. Exchange the identified elements between the left and right side.
222     //
223     // We keep the following variables for a block of elements:
224     //
225     // 1. `block` - Number of elements in the block.
226     // 2. `start` - Start pointer into the `offsets` array.
227     // 3. `end` - End pointer into the `offsets` array.
228     // 4. `offsets - Indices of out-of-order elements within the block.
229
230     // The current block on the left side (from `l` to `l.offset(block_l)`).
231     let mut l = v.as_mut_ptr();
232     let mut block_l = BLOCK;
233     let mut start_l = ptr::null_mut();
234     let mut end_l = ptr::null_mut();
235     let mut offsets_l: [u8; BLOCK] = unsafe { mem::uninitialized() };
236
237     // The current block on the right side (from `r.offset(-block_r)` to `r`).
238     let mut r = unsafe { l.offset(v.len() as isize) };
239     let mut block_r = BLOCK;
240     let mut start_r = ptr::null_mut();
241     let mut end_r = ptr::null_mut();
242     let mut offsets_r: [u8; BLOCK] = unsafe { mem::uninitialized() };
243
244     // FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
245     // than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.
246
247     // Returns the number of elements between pointers `l` (inclusive) and `r` (exclusive).
248     fn width<T>(l: *mut T, r: *mut T) -> usize {
249         assert!(mem::size_of::<T>() > 0);
250         (r as usize - l as usize) / mem::size_of::<T>()
251     }
252
253     loop {
254         // We are done with partitioning block-by-block when `l` and `r` get very close. Then we do
255         // some patch-up work in order to partition the remaining elements in between.
256         let is_done = width(l, r) <= 2 * BLOCK;
257
258         if is_done {
259             // Number of remaining elements (still not compared to the pivot).
260             let mut rem = width(l, r);
261             if start_l < end_l || start_r < end_r {
262                 rem -= BLOCK;
263             }
264
265             // Adjust block sizes so that the left and right block don't overlap, but get perfectly
266             // aligned to cover the whole remaining gap.
267             if start_l < end_l {
268                 block_r = rem;
269             } else if start_r < end_r {
270                 block_l = rem;
271             } else {
272                 block_l = rem / 2;
273                 block_r = rem - block_l;
274             }
275             debug_assert!(block_l <= BLOCK && block_r <= BLOCK);
276             debug_assert!(width(l, r) == block_l + block_r);
277         }
278
279         if start_l == end_l {
280             // Trace `block_l` elements from the left side.
281             start_l = offsets_l.as_mut_ptr();
282             end_l = offsets_l.as_mut_ptr();
283             let mut elem = l;
284
285             for i in 0..block_l {
286                 unsafe {
287                     // Branchless comparison.
288                     *end_l = i as u8;
289                     end_l = end_l.offset(!is_less(&*elem, pivot) as isize);
290                     elem = elem.offset(1);
291                 }
292             }
293         }
294
295         if start_r == end_r {
296             // Trace `block_r` elements from the right side.
297             start_r = offsets_r.as_mut_ptr();
298             end_r = offsets_r.as_mut_ptr();
299             let mut elem = r;
300
301             for i in 0..block_r {
302                 unsafe {
303                     // Branchless comparison.
304                     elem = elem.offset(-1);
305                     *end_r = i as u8;
306                     end_r = end_r.offset(is_less(&*elem, pivot) as isize);
307                 }
308             }
309         }
310
311         // Number of out-of-order elements to swap between the left and right side.
312         let count = cmp::min(width(start_l, end_l), width(start_r, end_r));
313
314         if count > 0 {
315             macro_rules! left { () => { l.offset(*start_l as isize) } }
316             macro_rules! right { () => { r.offset(-(*start_r as isize) - 1) } }
317
318             // Instead of swapping one pair at the time, it is more efficient to perform a cyclic
319             // permutation. This is not strictly equivalent to swapping, but produces a similar
320             // result using fewer memory operations.
321             unsafe {
322                 let tmp = ptr::read(left!());
323                 ptr::copy_nonoverlapping(right!(), left!(), 1);
324
325                 for _ in 1..count {
326                     start_l = start_l.offset(1);
327                     ptr::copy_nonoverlapping(left!(), right!(), 1);
328                     start_r = start_r.offset(1);
329                     ptr::copy_nonoverlapping(right!(), left!(), 1);
330                 }
331
332                 ptr::copy_nonoverlapping(&tmp, right!(), 1);
333                 mem::forget(tmp);
334                 start_l = start_l.offset(1);
335                 start_r = start_r.offset(1);
336             }
337         }
338
339         if start_l == end_l {
340             // All out-of-order elements in the left block were moved. Move to the next block.
341             l = unsafe { l.offset(block_l as isize) };
342         }
343
344         if start_r == end_r {
345             // All out-of-order elements in the right block were moved. Move to the previous block.
346             r = unsafe { r.offset(-(block_r as isize)) };
347         }
348
349         if is_done {
350             break;
351         }
352     }
353
354     // All that remains now is at most one block (either the left or the right) with out-of-order
355     // elements that need to be moved. Such remaining elements can be simply shifted to the end
356     // within their block.
357
358     if start_l < end_l {
359         // The left block remains.
360         // Move it's remaining out-of-order elements to the far right.
361         debug_assert_eq!(width(l, r), block_l);
362         while start_l < end_l {
363             unsafe {
364                 end_l = end_l.offset(-1);
365                 ptr::swap(l.offset(*end_l as isize), r.offset(-1));
366                 r = r.offset(-1);
367             }
368         }
369         width(v.as_mut_ptr(), r)
370     } else if start_r < end_r {
371         // The right block remains.
372         // Move it's remaining out-of-order elements to the far left.
373         debug_assert_eq!(width(l, r), block_r);
374         while start_r < end_r {
375             unsafe {
376                 end_r = end_r.offset(-1);
377                 ptr::swap(l, r.offset(-(*end_r as isize) - 1));
378                 l = l.offset(1);
379             }
380         }
381         width(v.as_mut_ptr(), l)
382     } else {
383         // Nothing else to do, we're done.
384         width(v.as_mut_ptr(), l)
385     }
386 }
387
388 /// Partitions `v` into elements smaller than `v[pivot]`, followed by elements greater than or
389 /// equal to `v[pivot]`.
390 ///
391 /// Returns a tuple of:
392 ///
393 /// 1. Number of elements smaller than `v[pivot]`.
394 /// 2. True if `v` was already partitioned.
395 fn partition<T, F>(v: &mut [T], pivot: usize, is_less: &mut F) -> (usize, bool)
396     where F: FnMut(&T, &T) -> bool
397 {
398     let (mid, was_partitioned) = {
399         // Place the pivot at the beginning of slice.
400         v.swap(0, pivot);
401         let (pivot, v) = v.split_at_mut(1);
402         let pivot = &mut pivot[0];
403
404         // Read the pivot into a stack-allocated variable for efficiency. If a following comparison
405         // operation panics, the pivot will be automatically written back into the slice.
406         let mut tmp = NoDrop { value: unsafe { ptr::read(pivot) } };
407         let _pivot_guard = CopyOnDrop {
408             src: unsafe { &mut tmp.value },
409             dest: pivot,
410         };
411         let pivot = unsafe { &tmp.value };
412
413         // Find the first pair of out-of-order elements.
414         let mut l = 0;
415         let mut r = v.len();
416         unsafe {
417             // Find the first element greater then or equal to the pivot.
418             while l < r && is_less(v.get_unchecked(l), pivot) {
419                 l += 1;
420             }
421
422             // Find the last element smaller that the pivot.
423             while l < r && !is_less(v.get_unchecked(r - 1), pivot) {
424                 r -= 1;
425             }
426         }
427
428         (l + partition_in_blocks(&mut v[l..r], pivot, is_less), l >= r)
429
430         // `_pivot_guard` goes out of scope and writes the pivot (which is a stack-allocated
431         // variable) back into the slice where it originally was. This step is critical in ensuring
432         // safety!
433     };
434
435     // Place the pivot between the two partitions.
436     v.swap(0, mid);
437
438     (mid, was_partitioned)
439 }
440
441 /// Partitions `v` into elements equal to `v[pivot]` followed by elements greater than `v[pivot]`.
442 ///
443 /// Returns the number of elements equal to the pivot. It is assumed that `v` does not contain
444 /// elements smaller than the pivot.
445 fn partition_equal<T, F>(v: &mut [T], pivot: usize, is_less: &mut F) -> usize
446     where F: FnMut(&T, &T) -> bool
447 {
448     // Place the pivot at the beginning of slice.
449     v.swap(0, pivot);
450     let (pivot, v) = v.split_at_mut(1);
451     let pivot = &mut pivot[0];
452
453     // Read the pivot into a stack-allocated variable for efficiency. If a following comparison
454     // operation panics, the pivot will be automatically written back into the slice.
455     let mut tmp = NoDrop { value: unsafe { ptr::read(pivot) } };
456     let _pivot_guard = CopyOnDrop {
457         src: unsafe { &mut tmp.value },
458         dest: pivot,
459     };
460     let pivot = unsafe { &tmp.value };
461
462     // Now partition the slice.
463     let mut l = 0;
464     let mut r = v.len();
465     loop {
466         unsafe {
467             // Find the first element greater that the pivot.
468             while l < r && !is_less(pivot, v.get_unchecked(l)) {
469                 l += 1;
470             }
471
472             // Find the last element equal to the pivot.
473             while l < r && is_less(pivot, v.get_unchecked(r - 1)) {
474                 r -= 1;
475             }
476
477             // Are we done?
478             if l >= r {
479                 break;
480             }
481
482             // Swap the found pair of out-of-order elements.
483             r -= 1;
484             ptr::swap(v.get_unchecked_mut(l), v.get_unchecked_mut(r));
485             l += 1;
486         }
487     }
488
489     // We found `l` elements equal to the pivot. Add 1 to account for the pivot itself.
490     l + 1
491
492     // `_pivot_guard` goes out of scope and writes the pivot (which is a stack-allocated variable)
493     // back into the slice where it originally was. This step is critical in ensuring safety!
494 }
495
496 /// Scatters some elements around in an attempt to break patterns that might cause imbalanced
497 /// partitions in quicksort.
498 #[cold]
499 fn break_patterns<T>(v: &mut [T]) {
500     let len = v.len();
501     if len >= 8 {
502         // Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
503         let mut random = len as u32;
504         let mut gen_u32 = || {
505             random ^= random << 13;
506             random ^= random >> 17;
507             random ^= random << 5;
508             random
509         };
510         let mut gen_usize = || {
511             if mem::size_of::<usize>() <= 4 {
512                 gen_u32() as usize
513             } else {
514                 (((gen_u32() as u64) << 32) | (gen_u32() as u64)) as usize
515             }
516         };
517
518         // Take random numbers modulo this number.
519         // The number fits into `usize` because `len` is not greater than `isize::MAX`.
520         let modulus = len.next_power_of_two();
521
522         // Some pivot candidates will be in the nearby of this index. Let's randomize them.
523         let pos = len / 4 * 2;
524
525         for i in 0..3 {
526             // Generate a random number modulo `len`. However, in order to avoid costly operations
527             // we first take it modulo a power of two, and then decrease by `len` until it fits
528             // into the range `[0, len - 1]`.
529             let mut other = gen_usize() & (modulus - 1);
530
531             // `other` is guaranteed to be less than `2 * len`.
532             if other >= len {
533                 other -= len;
534             }
535
536             v.swap(pos - 1 + i, other);
537         }
538     }
539 }
540
541 /// Chooses a pivot in `v` and returns the index and `true` if the slice is likely already sorted.
542 ///
543 /// Elements in `v` might be reordered in the process.
544 fn choose_pivot<T, F>(v: &mut [T], is_less: &mut F) -> (usize, bool)
545     where F: FnMut(&T, &T) -> bool
546 {
547     // Minimum length to choose the median-of-medians method.
548     // Shorter slices use the simple median-of-three method.
549     const SHORTEST_MEDIAN_OF_MEDIANS: usize = 50;
550     // Maximum number of swaps that can be performed in this function.
551     const MAX_SWAPS: usize = 4 * 3;
552
553     let len = v.len();
554
555     // Three indices near which we are going to choose a pivot.
556     let mut a = len / 4 * 1;
557     let mut b = len / 4 * 2;
558     let mut c = len / 4 * 3;
559
560     // Counts the total number of swaps we are about to perform while sorting indices.
561     let mut swaps = 0;
562
563     if len >= 8 {
564         // Swaps indices so that `v[a] <= v[b]`.
565         let mut sort2 = |a: &mut usize, b: &mut usize| unsafe {
566             if is_less(v.get_unchecked(*b), v.get_unchecked(*a)) {
567                 ptr::swap(a, b);
568                 swaps += 1;
569             }
570         };
571
572         // Swaps indices so that `v[a] <= v[b] <= v[c]`.
573         let mut sort3 = |a: &mut usize, b: &mut usize, c: &mut usize| {
574             sort2(a, b);
575             sort2(b, c);
576             sort2(a, b);
577         };
578
579         if len >= SHORTEST_MEDIAN_OF_MEDIANS {
580             // Finds the median of `v[a - 1], v[a], v[a + 1]` and stores the index into `a`.
581             let mut sort_adjacent = |a: &mut usize| {
582                 let tmp = *a;
583                 sort3(&mut (tmp - 1), a, &mut (tmp + 1));
584             };
585
586             // Find medians in the neighborhoods of `a`, `b`, and `c`.
587             sort_adjacent(&mut a);
588             sort_adjacent(&mut b);
589             sort_adjacent(&mut c);
590         }
591
592         // Find the median among `a`, `b`, and `c`.
593         sort3(&mut a, &mut b, &mut c);
594     }
595
596     if swaps < MAX_SWAPS {
597         (b, swaps == 0)
598     } else {
599         // The maximum number of swaps was performed. Chances are the slice is descending or mostly
600         // descending, so reversing will probably help sort it faster.
601         v.reverse();
602         (len - 1 - b, true)
603     }
604 }
605
606 /// Sorts `v` recursively.
607 ///
608 /// If the slice had a predecessor in the original array, it is specified as `pred`.
609 ///
610 /// `limit` is the number of allowed imbalanced partitions before switching to `heapsort`. If zero,
611 /// this function will immediately switch to heapsort.
612 fn recurse<'a, T, F>(mut v: &'a mut [T], is_less: &mut F, mut pred: Option<&'a T>, mut limit: usize)
613     where F: FnMut(&T, &T) -> bool
614 {
615     // Slices of up to this length get sorted using insertion sort.
616     const MAX_INSERTION: usize = 20;
617
618     // True if the last partitioning was reasonably balanced.
619     let mut was_balanced = true;
620     // True if the last partitioning didn't shuffle elements (the slice was already partitioned).
621     let mut was_partitioned = true;
622
623     loop {
624         let len = v.len();
625
626         // Very short slices get sorted using insertion sort.
627         if len <= MAX_INSERTION {
628             insertion_sort(v, is_less);
629             return;
630         }
631
632         // If too many bad pivot choices were made, simply fall back to heapsort in order to
633         // guarantee `O(n log n)` worst-case.
634         if limit == 0 {
635             heapsort(v, is_less);
636             return;
637         }
638
639         // If the last partitioning was imbalanced, try breaking patterns in the slice by shuffling
640         // some elements around. Hopefully we'll choose a better pivot this time.
641         if !was_balanced {
642             break_patterns(v);
643             limit -= 1;
644         }
645
646         // Choose a pivot and try guessing whether the slice is already sorted.
647         let (pivot, likely_sorted) = choose_pivot(v, is_less);
648
649         // If the last partitioning was decently balanced and didn't shuffle elements, and if pivot
650         // selection predicts the slice is likely already sorted...
651         if was_balanced && was_partitioned && likely_sorted {
652             // Try identifying several out-of-order elements and shifting them to correct
653             // positions. If the slice ends up being completely sorted, we're done.
654             if partial_insertion_sort(v, is_less) {
655                 return;
656             }
657         }
658
659         // If the chosen pivot is equal to the predecessor, then it's the smallest element in the
660         // slice. Partition the slice into elements equal to and elements greater than the pivot.
661         // This case is usually hit when the slice contains many duplicate elements.
662         if let Some(p) = pred {
663             if !is_less(p, &v[pivot]) {
664                 let mid = partition_equal(v, pivot, is_less);
665
666                 // Continue sorting elements greater than the pivot.
667                 v = &mut {v}[mid..];
668                 continue;
669             }
670         }
671
672         // Partition the slice.
673         let (mid, was_p) = partition(v, pivot, is_less);
674         was_balanced = cmp::min(mid, len - mid) >= len / 8;
675         was_partitioned = was_p;
676
677         // Split the slice into `left`, `pivot`, and `right`.
678         let (left, right) = {v}.split_at_mut(mid);
679         let (pivot, right) = right.split_at_mut(1);
680         let pivot = &pivot[0];
681
682         // Recurse into the shorter side only in order to minimize the total number of recursive
683         // calls and consume less stack space. Then just continue with the longer side (this is
684         // akin to tail recursion).
685         if left.len() < right.len() {
686             recurse(left, is_less, pred, limit);
687             v = right;
688             pred = Some(pivot);
689         } else {
690             recurse(right, is_less, Some(pivot), limit);
691             v = left;
692         }
693     }
694 }
695
696 /// Sorts `v` using pattern-defeating quicksort, which is `O(n log n)` worst-case.
697 pub fn quicksort<T, F>(v: &mut [T], mut is_less: F)
698     where F: FnMut(&T, &T) -> bool
699 {
700     // Sorting has no meaningful behavior on zero-sized types.
701     if mem::size_of::<T>() == 0 {
702         return;
703     }
704
705     // Limit the number of imbalanced partitions to `floor(log2(len)) + 1`.
706     let limit = mem::size_of::<usize>() * 8 - v.len().leading_zeros() as usize;
707
708     recurse(v, &mut is_less, None, limit);
709 }