fn from_iter<T: IntoIterator<Item = (V, u8)>>(iter: T) -> Self {
        let mut leaves: Vec<_> = iter
            .into_iter()
            .map(|(value, byte)| HuffmanTree::Leaf { value, byte })
            .collect();
        if leaves.len() == 1 {
            return leaves.pop().unwrap()
        }
        assert!(!leaves.is_empty());
        leaves.sort_unstable_by_key(|l| {
            match l {
                HuffmanTree::Leaf { value, byte } => {
                    // sort by decreasing value then byte
                    Reverse((*value, *byte))
                },
                _ => unsafe {
                    // SAFETY: leaves only contains Leaf variants
                    unreachable_unchecked()
                },
            }
        });
        let mut nodes = VecDeque::with_capacity(leaves.len() / 2);
        while nodes.len() != 1 || !leaves.is_empty() {
            // group two lightest trees in a new tree
            let left = Box::new(pop_smallest(&mut leaves, &mut nodes));
            let right = Box::new(pop_smallest(&mut leaves, &mut nodes));
            let value = *left.value() + *right.value();
            let new_node = HuffmanTree::Node { value, left, right };
            nodes.push_front(new_node);
        }
        nodes.pop_back().unwrap()
    }
/// Builds a HuffmanTree from a collection of bytes and their values.
///
/// The process to build a HuffmanTree uses two queues, one containing
/// leaves (essentially a (value, byte) tuple), the other containing
/// partial tree. Queues are ordered by decreasing values (and then
/// bytes to form canonical encoding).  Both elements with the lowest value
/// are combined to form a partial tree and pushed in the relevant queue
/// until only one tree remains.
Builds a HuffmanTree from a collection of bytes and their values.
The process to build a HuffmanTree uses two queues, one containing
leaves (essentially a (value, byte) tuple), the other containing
partial tree. Queues are ordered by decreasing values (and then
bytes to form canonical encoding).  Both elements with the lowest value
are combined to form a partial tree and pushed in the relevant queue
until only one tree remains.
Builds a HuffmanTree from a collection of bytes and their values . The process to build a HuffmanTree uses two queues one containing leaves ( essentially a ( value byte ) tuple ) the other containing partial tree . Queues are ordered by decreasing values ( and then bytes to form canonical encoding ) . Both elements with the lowest value are combined to form a partial tree and pushed in the relevant queue until only one tree remains .
fn from_iter<T: IntoIterator<Item = (V, u8)>>(iter: T) -> Self {
        let mut leaves: Vec<_> = iter
            .into_iter()
            .map(|(value, byte)| HuffmanTree::Leaf { value, byte })
            .collect();
        if leaves.len() == 1 {
            return leaves.pop().unwrap()
        }
        assert!(!leaves.is_empty());
        leaves.sort_unstable_by_key(|l| {
            match l {
                HuffmanTree::Leaf { value, byte } => {
                    // sort by decreasing value then byte
                    Reverse((*value, *byte))
                },
                _ => unsafe {
                    // SAFETY: leaves only contains Leaf variants
                    unreachable_unchecked()
                },
            }
        });
        let mut nodes = VecDeque::with_capacity(leaves.len() / 2);
        while nodes.len() != 1 || !leaves.is_empty() {
            // group two lightest trees in a new tree
            let left = Box::new(pop_smallest(&mut leaves, &mut nodes));
            let right = Box::new(pop_smallest(&mut leaves, &mut nodes));
            let value = *left.value() + *right.value();
            let new_node = HuffmanTree::Node { value, left, right };
            nodes.push_front(new_node);
        }
        nodes.pop_back().unwrap()
    }
fn from_iter < T : IntoIterator < Item = ( V , u8 ) > > ( iter : T ) -> Self { let mut leaves : Vec < _ > = iter . into_iter ( ) . map ( | ( value , byte ) | HuffmanTree :: Leaf { value , byte } ) . collect ( ) ; if leaves . len ( ) == 1 { return leaves . pop ( ) . unwrap ( ) } assert ! ( leaves is_empty ( ) ) ; leaves . sort_unstable_by_key ( | l | { match l { HuffmanTree :: Leaf { value , byte } => { // sort by decreasing value then byte Reverse ( ( * value , * byte ) ) } , _ => unsafe { // SAFETY: leaves only contains Leaf variants unreachable_unchecked ( ) } , } } ) ; let mut nodes = VecDeque :: with_capacity ( leaves . len ( ) / 2 ) ; while nodes . len ( ) != 1 || ! leaves . is_empty ( ) { // group two lightest trees in a new tree let left = Box :: new ( pop_smallest ( & mut leaves , & mut nodes ) ) ; let right = Box :: new ( pop_smallest ( & mut leaves , & mut nodes ) ) ; let value = * left . value ( ) + * right . value ( ) ; let new_node = HuffmanTree :: Node { value , left , right } ; nodes . push_front ( new_node ) ; } nodes . pop_back ( ) . unwrap ( ) }
Builds a HuffmanTree from a collection of bytes and their values.
Builds a HuffmanTree from a collection of bytes and their values .
iter
T
iter
T