fn max_heapify<T: PartialOrd + Clone>(src: &mut [T], start_from: usize) {
    let mut largest_index = start_from;
    loop {
        let parent_index = largest_index;
        let left_child_index = largest_index * 2 + 1;
        let right_child_index = largest_index * 2 + 2;

        let subtree = [parent_index, left_child_index, right_child_index];
        largest_index = subtree
            .iter()
            .filter_map(|&idx| src.get(idx))
            .enumerate()
            .reduce(|tup1, tup2| if tup1.1 > tup2.1 { tup1 } else { tup2 })
            .map(|(idx, _)| subtree[idx])
            .expect("iterator isn't empty");

        if parent_index != largest_index {
            src.swap(parent_index, largest_index);
            continue;
        }
        return;
    }
}
// Recursive version is very expensive and leads to stack overflow
Recursive version is very expensive and leads to stack overflow
Recursive version is very expensive and leads to stack overflow
fn max_heapify<T: PartialOrd + Clone>(src: &mut [T], start_from: usize) {
    let mut largest_index = start_from;
    loop {
        let parent_index = largest_index;
        let left_child_index = largest_index * 2 + 1;
        let right_child_index = largest_index * 2 + 2;
        let subtree = [parent_index, left_child_index, right_child_index];
        largest_index = subtree
            .iter()
            .filter_map(|&idx| src.get(idx))
            .enumerate()
            .reduce(|tup1, tup2| if tup1.1 > tup2.1 { tup1 } else { tup2 })
            .map(|(idx, _)| subtree[idx])
            .expect("iterator isn't empty");
        if parent_index != largest_index {
            src.swap(parent_index, largest_index);
            continue;
        }
        return;
    }
}
fn max_heapify < T : PartialOrd + Clone > ( src : & mut [ T ] , start_from : usize ) { let mut largest_index = start_from ; loop { let parent_index = largest_index ; let left_child_index = largest_index * 2 + 1 ; let right_child_index = largest_index * 2 + 2 ; let subtree = [ parent_index , left_child_index , right_child_index ] ; largest_index = subtree . iter ( ) . filter_map ( | & idx | src . get ( idx ) ) . enumerate ( ) . reduce ( | tup1 , tup2 | if tup1 . 1 > tup2 . 1 { tup1 } else { tup2 } ) . map ( | ( idx , _ ) | subtree [ idx ] ) . expect ( " " ) ; if parent_index != largest_index { src . swap ( parent_index , largest_index ) ; continue ; } return ; } }
Recursive version is very expensive and leads to stack overflow
Recursive version is very expensive and leads to stack overflow
src
&mut [T]
start_from
usize
src
&mut [T]
start_from
usize