Debian Codegraph @

src/insort.rs

raw symbols SCIP
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 tests;
6
7 use crate::Comparator;
8
9 /// Sorts the list using insertion sort.
10 // This version was almost completely copied from libcollections/slice.rs
11 pub(crate) fn sort<T, C: Comparator<T>>(list: &mut [T], cmp: &C) -> Result<(), C::Error> {
12 if list.len() < 2 {
13 return Ok(());
14 }
15 for i in 0..list.len() {
16 let i_el = &list[i];
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[..i].iter().enumerate().rev() {
20 if !cmp.is_gt(j_el, i_el)? {
21 j = jj + 1;
22 break;
23 }
24 }
25 if i != j {
26 // SAFETY: j<i, i<list.len
27 unsafe { list.get_unchecked_mut(j..=i).rotate_right(1) };
28 }
29 }
30 Ok(())
31 }