| 1 | //! The top sorting algorithm; that is, the modified merge sort we keep |
| 2 | //! talking about. |
| 3 | |
| 4 | #[cfg(test)] |
| 5 | mod testsrust-analyzer cargo timsort 0.1.3 sort/tests/; |
| 6 | |
| 7 | use craterust-analyzer cargo timsort 0.1.3 crate/::find_runrust-analyzer cargo timsort 0.1.3 find_run/::get_runrust-analyzer cargo timsort 0.1.3 find_run/get_run().; |
| 8 | use craterust-analyzer cargo timsort 0.1.3 crate/::insortrust-analyzer cargo timsort 0.1.3 insort/; |
| 9 | use craterust-analyzer cargo timsort 0.1.3 crate/::mergerust-analyzer cargo timsort 0.1.3 merge/::mergerust-analyzer cargo timsort 0.1.3 merge/merge().; |
| 10 | use craterust-analyzer cargo timsort 0.1.3 crate/::Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#; |
| 11 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#; |
| 12 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().; |
| 13 | |
| 14 | /// Minimum run length to merge; anything shorter will be lengthend and |
| 15 | /// sorted using `insort::sort`. |
| 16 | const MIN_MERGErust-analyzer cargo timsort 0.1.3 sort/MIN_MERGE.: usize = 64; |
| 17 | |
| 18 | /// Compute the actual minimum merge size for a particular list. |
| 19 | fn calc_min_mergerust-analyzer cargo timsort 0.1.3 sort/calc_min_merge().(mut len: usize) -> usize { |
| 20 | if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().MIN_MERGErust-analyzer cargo timsort 0.1.3 sort/MIN_MERGE. { |
| 21 | len |
| 22 | } else { |
| 23 | let mut r: usize = 0; |
| 24 | while len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().MIN_MERGErust-analyzer cargo timsort 0.1.3 sort/MIN_MERGE. { |
| 25 | r rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitOrAssign<Self>`]bitor_assign().len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().1; |
| 26 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`ShrAssign<i32>`]shr_assign().1; |
| 27 | } |
| 28 | len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().r |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Represents a known-sorted sublist. |
| 33 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 34 | struct Runrust-analyzer cargo timsort 0.1.3 sort/Run# { |
| 35 | posrust-analyzer cargo timsort 0.1.3 sort/Run#pos.: usize, |
| 36 | lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.: usize, |
| 37 | } |
| 38 | |
| 39 | /// All the ongoing state of the sort. |
| 40 | struct SortStaterust-analyzer cargo timsort 0.1.3 sort/SortState#<'a, T, C: Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T>> { |
| 41 | /// The list that is being sorted. |
| 42 | listrust-analyzer cargo timsort 0.1.3 sort/SortState#list.: &'a mut [T], |
| 43 | /// The comparator function. Should return true if the first argument is |
| 44 | /// greater than the second. |
| 45 | cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp.: &'a C, |
| 46 | /// The list of known-sorted sections of the list that can be merged. |
| 47 | /// To keep the size of this list down, this invariant is preserved: |
| 48 | /// - `runs.len < 3 || runs[i-2].len > runs[i-1].len + runs[i].len` |
| 49 | /// - `runs.len < 2 || runs[i-1].len > runs[i].len` |
| 50 | runsrust-analyzer cargo timsort 0.1.3 sort/SortState#runs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Runrust-analyzer cargo timsort 0.1.3 sort/Run#>, |
| 51 | /// The current position in the list. When `pos == list.len()`, we can now |
| 52 | /// merge the last of the runs, and we're done. |
| 53 | posrust-analyzer cargo timsort 0.1.3 sort/SortState#pos.: usize, |
| 54 | } |
| 55 | |
| 56 | impl<'a, T, C: Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T>> SortStaterust-analyzer cargo timsort 0.1.3 sort/SortState#<'a, T, C> { |
| 57 | #[inline] |
| 58 | fn newrust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]new().(list: &'a mut [T], cmp: &'a C) -> SortStaterust-analyzer cargo timsort 0.1.3 sort/SortState#<'a, T, C> { |
| 59 | SortStaterust-analyzer cargo timsort 0.1.3 sort/SortState# { |
| 60 | listrust-analyzer cargo timsort 0.1.3 sort/SortState#list., |
| 61 | cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp., |
| 62 | runsrust-analyzer cargo timsort 0.1.3 sort/SortState#runs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 63 | posrust-analyzer cargo timsort 0.1.3 sort/SortState#pos.: 0, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// The outer loop. Find runs, and move forward. |
| 68 | fn sortrust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]sort().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 69 | let list_len = self.listrust-analyzer cargo timsort 0.1.3 sort/SortState#list..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 70 | // Minimum run size to use merge sort on. Any sorted sections of the |
| 71 | // list that are shorter than this are lengthened using `insort::sort`. |
| 72 | let min_run = calc_min_mergerust-analyzer cargo timsort 0.1.3 sort/calc_min_merge().(list_len); |
| 73 | while self.posrust-analyzer cargo timsort 0.1.3 sort/SortState#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().list_len { |
| 74 | let pos = self.posrust-analyzer cargo timsort 0.1.3 sort/SortState#pos.; |
| 75 | let mut run_len = get_runrust-analyzer cargo timsort 0.1.3 find_run/get_run().(&mut self.listrust-analyzer cargo timsort 0.1.3 sort/SortState#list.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()., self.cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 76 | let run_min_len = minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(min_run, list_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().pos); |
| 77 | if run_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().run_min_len { |
| 78 | run_len = run_min_len; |
| 79 | let l = &mut self.listrust-analyzer cargo timsort 0.1.3 sort/SortState#list.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#run_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut().; |
| 80 | insortrust-analyzer cargo timsort 0.1.3 insort/::sortrust-analyzer cargo timsort 0.1.3 insort/sort().(l, self.cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 81 | } |
| 82 | self.runsrust-analyzer cargo timsort 0.1.3 sort/SortState#runs..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Runrust-analyzer cargo timsort 0.1.3 sort/Run# { posrust-analyzer cargo timsort 0.1.3 sort/Run#pos., lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.: run_len }); |
| 83 | self.posrust-analyzer cargo timsort 0.1.3 sort/SortState#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().run_len; |
| 84 | self.merge_collapserust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]merge_collapse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 85 | } |
| 86 | self.merge_force_collapserust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]merge_force_collapse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 88 | } |
| 89 | |
| 90 | /// Merge the runs if they're too big. |
| 91 | /// Copied almost verbatim from |
| 92 | /// http://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it/#sec3.2 |
| 93 | fn merge_collapserust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]merge_collapse().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 94 | let runs = &mut self.runsrust-analyzer cargo timsort 0.1.3 sort/SortState#runs.; |
| 95 | while runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 { |
| 96 | let l = runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 97 | if (l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().3 && runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.) |
| 98 | || (l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().4 && runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.) |
| 99 | { |
| 100 | let (pos1, pos2) = if runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. { |
| 101 | (l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().3, l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2) |
| 102 | } else { |
| 103 | (l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2, l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) |
| 104 | }; |
| 105 | let (run1, run2) = (runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 106 | debug_assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_eq!!(run1.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., run2.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos.); |
| 107 | runs.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(pos2); |
| 108 | runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().pos1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = Runrust-analyzer cargo timsort 0.1.3 sort/Run# { |
| 109 | posrust-analyzer cargo timsort 0.1.3 sort/Run#pos.: run1.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos., |
| 110 | lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.: run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().run2.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., |
| 111 | }; |
| 112 | let l = &mut self.listrust-analyzer cargo timsort 0.1.3 sort/SortState#list.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().run1.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().run2.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut().; |
| 113 | mergerust-analyzer cargo timsort 0.1.3 merge/merge().(l, run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., self.cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 114 | } else { |
| 115 | break; // Invariant established. |
| 116 | } |
| 117 | } |
| 118 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 119 | } |
| 120 | |
| 121 | /// Merge any outstanding runs, at the end. |
| 122 | fn merge_force_collapserust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]merge_force_collapse().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 123 | let runs = &mut self.runsrust-analyzer cargo timsort 0.1.3 sort/SortState#runs.; |
| 124 | while runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 { |
| 125 | let (mut pos1, mut pos2) = (runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2, runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1); |
| 126 | if runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().2 && runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().runs.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. { |
| 127 | pos1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().1; |
| 128 | pos2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().1; |
| 129 | } |
| 130 | let (run1, run2) = (runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 131 | debug_assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_eq!!(run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., run2.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos.); |
| 132 | runs.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(pos2); |
| 133 | runs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().pos1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = Runrust-analyzer cargo timsort 0.1.3 sort/Run# { |
| 134 | posrust-analyzer cargo timsort 0.1.3 sort/Run#pos.: run1.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos., |
| 135 | lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.: run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().run2.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., |
| 136 | }; |
| 137 | let l = &mut self.listrust-analyzer cargo timsort 0.1.3 sort/SortState#list.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().run1.posrust-analyzer cargo timsort 0.1.3 sort/Run#pos...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().run2.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut().; |
| 138 | mergerust-analyzer cargo timsort 0.1.3 merge/merge().(l, run1.lenrust-analyzer cargo timsort 0.1.3 sort/Run#len., self.cmprust-analyzer cargo timsort 0.1.3 sort/SortState#cmp.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 139 | } |
| 140 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// Sorts the list using merge sort. |
| 145 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn try_sort_byrust-analyzer cargo timsort 0.1.3 sort/try_sort_by().<T, C: Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T>>(list: &mut [T], cmp: C) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 146 | if list.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().MIN_MERGErust-analyzer cargo timsort 0.1.3 sort/MIN_MERGE. { |
| 147 | insortrust-analyzer cargo timsort 0.1.3 insort/::sortrust-analyzer cargo timsort 0.1.3 insort/sort().(list, &cmp) |
| 148 | } else { |
| 149 | SortStaterust-analyzer cargo timsort 0.1.3 sort/SortState#::newrust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]new().(list, &cmp).sortrust-analyzer cargo timsort 0.1.3 sort/impl#[`SortState<'a, T, C>`]sort().() |
| 150 | } |
| 151 | } |