| 1 | //! The run finder algorithm. Takes an unsorted slice, and returns the number |
| 2 | //! of sequential elements in a row. |
| 3 | |
| 4 | #[cfg(test)] |
| 5 | mod testsrust-analyzer cargo timsort 0.1.3 find_run/tests/; |
| 6 | |
| 7 | use craterust-analyzer cargo timsort 0.1.3 crate/::Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#; |
| 8 | |
| 9 | /// Find a run, reversing if necessary. |
| 10 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn get_runrust-analyzer cargo timsort 0.1.3 find_run/get_run().<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#<usize, C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 11 | let (ord, len) = find_runrust-analyzer cargo timsort 0.1.3 find_run/find_run().(list, cmp)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 12 | if ord { |
| 13 | list[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 ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]reverse().(); |
| 14 | } |
| 15 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) |
| 16 | } |
| 17 | |
| 18 | /// Find a run. Returns true if it needs reversed, and false otherwise. |
| 19 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn find_runrust-analyzer cargo timsort 0.1.3 find_run/find_run().<T, C: Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#<T>>( |
| 20 | list: &[T], |
| 21 | cmp: &C, |
| 22 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(bool, usize), C::Errorrust-analyzer cargo timsort 0.1.3 Comparator#Error#> { |
| 23 | let (first, second) = match list { |
| 24 | [a, b, ..] => (a, b), |
| 25 | _ => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((false, list.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())), |
| 26 | }; |
| 27 | let mut pos = 1; |
| 28 | let gt = if cmp.is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(first, second)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 29 | for pair in list[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..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/Index#index()..windowsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]windows().(2) { |
| 30 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().cmp.is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(&pair[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &pair[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 31 | break; |
| 32 | } |
| 33 | 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().1; |
| 34 | } |
| 35 | true |
| 36 | } else { |
| 37 | for pair in list[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..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/Index#index()..windowsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]windows().(2) { |
| 38 | if cmp.is_gtrust-analyzer cargo timsort 0.1.3 Comparator#is_gt().(&pair[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &pair[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 39 | break; |
| 40 | } |
| 41 | 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().1; |
| 42 | } |
| 43 | false |
| 44 | }; |
| 45 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((gt, pos 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().1)) |
| 46 | } |