| 1 | //! The bottom sorting algorithm (we could just have 1-element runs and do all |
| 2 | //! the sorting with the merge algorithm, but that would be much slower). |
| 3 | |
| 4 | #[cfg(test)] |
| 5 | mod testsrust-analyzer cargo timsort 0.1.3 insort/tests/; |
| 6 | |
| 7 | use craterust-analyzer cargo timsort 0.1.3 crate/::Comparatorrust-analyzer cargo timsort 0.1.3 Comparator#; |
| 8 | |
| 9 | /// Sorts the list using insertion sort. |
| 10 | // This version was almost completely copied from libcollections/slice.rs |
| 11 | pub(craterust-analyzer cargo timsort 0.1.3 crate/) fn sortrust-analyzer cargo timsort 0.1.3 insort/sort().<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#> { |
| 12 | 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().2 { |
| 13 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 14 | } |
| 15 | for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#list.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 16 | let i_el = &list[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 17 | // find the index just above the element that is in order wrt list[i] |
| 18 | let mut j = 0; |
| 19 | for (jj, j_el) in list[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 ops/range/RangeTo#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() { |
| 20 | 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().(j_el, i_el)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 21 | j = jj 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; |
| 22 | break; |
| 23 | } |
| 24 | } |
| 25 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().j { |
| 26 | // SAFETY: j<i, i<list.len |
| 27 | unsafe { list.get_unchecked_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_unchecked_mut().(j..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#i).rotate_rightrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]rotate_right().(1) }; |
| 28 | } |
| 29 | } |
| 30 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 31 | } |