pub fn consolidate_slice<T: Ord, R: AddAssignByRef + HasZero>(slice: &mut [(T, R)]) -> usize {
    // We could do an insertion-sort like initial scan which builds up sorted,
    // consolidated runs. In a world where there are not many results, we may
    // never even need to call in to merge sort.
    slice.sort_by(|x, y| x.0.cmp(&y.0));

    // Counts the number of distinct known-non-zero accumulations. Indexes the write
    // location.
    let mut offset = 0;
    for index in 1..slice.len() {
        // The following unsafe block elides various bounds checks, using the reasoning
        // that `offset` is always strictly less than `index` at the beginning
        // of each iteration. This is initially true, and in each iteration
        // `offset` can increase by at most one (whereas `index` always
        // increases by one). As `index` is always in bounds, and `offset` starts at
        // zero, it too is always in bounds.
        //
        // LLVM appears to struggle to optimize out Rust's split_at_mut, which would
        // prove disjointness using run-time tests.
        unsafe {
            assert!(offset < index);

            // LOOP INVARIANT: offset < index
            let ptr1 = slice.as_mut_ptr().add(offset);
            let ptr2 = slice.as_mut_ptr().add(index);

            if (*ptr1).0 == (*ptr2).0 {
                (*ptr1).1.add_assign_by_ref(&(*ptr2).1);
            } else {
                if !(*ptr1).1.is_zero() {
                    offset += 1;
                }
                let ptr1 = slice.as_mut_ptr().add(offset);
                std::ptr::swap(ptr1, ptr2);
            }
        }
    }
    if offset < slice.len() && !slice[offset].1.is_zero() {
        offset += 1;
    }

    offset
}
/// Sorts and consolidates a slice, returning the valid prefix length.
Sorts and consolidates a slice, returning the valid prefix length.
Sorts and consolidates a slice returning the valid prefix length .
pub fn consolidate_slice<T: Ord, R: AddAssignByRef + HasZero>(slice: &mut [(T, R)]) -> usize {
    // We could do an insertion-sort like initial scan which builds up sorted,
    // consolidated runs. In a world where there are not many results, we may
    // never even need to call in to merge sort.
    slice.sort_by(|x, y| x.0.cmp(&y.0));
    // Counts the number of distinct known-non-zero accumulations. Indexes the write
    // location.
    let mut offset = 0;
    for index in 1..slice.len() {
        // The following unsafe block elides various bounds checks, using the reasoning
        // that `offset` is always strictly less than `index` at the beginning
        // of each iteration. This is initially true, and in each iteration
        // `offset` can increase by at most one (whereas `index` always
        // increases by one). As `index` is always in bounds, and `offset` starts at
        // zero, it too is always in bounds.
        //
        // LLVM appears to struggle to optimize out Rust's split_at_mut, which would
        // prove disjointness using run-time tests.
        unsafe {
            assert!(offset < index);
            // LOOP INVARIANT: offset < index
            let ptr1 = slice.as_mut_ptr().add(offset);
            let ptr2 = slice.as_mut_ptr().add(index);
            if (*ptr1).0 == (*ptr2).0 {
                (*ptr1).1.add_assign_by_ref(&(*ptr2).1);
            } else {
                if !(*ptr1).1.is_zero() {
                    offset += 1;
                }
                let ptr1 = slice.as_mut_ptr().add(offset);
                std::ptr::swap(ptr1, ptr2);
            }
        }
    }
    if offset < slice.len() && !slice[offset].1.is_zero() {
        offset += 1;
    }
    offset
}
pub fn consolidate_slice < T : Ord , R : AddAssignByRef + HasZero > ( slice : & mut [ ( T , R ) ] ) -> usize { // We could do an insertion-sort like initial scan which builds up sorted, // consolidated runs. In a world where there are not many results, we may // never even need to call in to merge sort. slice . sort_by ( | x , y | x . 0 . cmp ( & y . 0 ) ) ; // Counts the number of distinct known-non-zero accumulations. Indexes the write // location. let mut offset = 0 ; for index in 1 .. slice . len ( ) { // The following unsafe block elides various bounds checks, using the reasoning // that `offset` is always strictly less than `index` at the beginning // of each iteration. This is initially true, and in each iteration // `offset` can increase by at most one (whereas `index` always // increases by one). As `index` is always in bounds, and `offset` starts at // zero, it too is always in bounds. // // LLVM appears to struggle to optimize out Rust's split_at_mut, which would // prove disjointness using run-time tests. unsafe { assert ! ( offset index ) ; // LOOP INVARIANT: offset < index let ptr1 = slice . as_mut_ptr ( ) . add ( offset ) ; let ptr2 = slice . as_mut_ptr ( ) . add ( index ) ; if ( * ptr1 ) . 0 == ( * ptr2 ) . 0 { ( * ptr1 ) . 1 . add_assign_by_ref ( & ( * ptr2 ) . 1 ) ; } else { if ! ( * ptr1 ) . 1 . is_zero ( ) { offset += 1 ; } let ptr1 = slice . as_mut_ptr ( ) . add ( offset ) ; std :: ptr :: swap ( ptr1 , ptr2 ) ; } } } if offset < slice . len ( ) && ! slice [ offset ] . 1 . is_zero ( ) { offset += 1 ; } offset }
Sorts and consolidates a slice, returning the valid prefix length.
Sorts and consolidates a slice returning the valid prefix length .
slice
&mut [(T, R)]