fn pack_encoding_table(
    frequencies: &[u64],
    min_index: usize,
    max_index: usize,
    mut out: &mut Cursor<Vec<u8>>,
) -> UnitResult
{
    let mut code_bits = 0_u64;
    let mut code_bit_count = 0_u64;

    let mut frequency_index = min_index;
    while frequency_index <= max_index { // TODO slice iteration?
        let code_length = length(frequencies[frequency_index]);

        if code_length == 0 {
            let mut zero_run = 1;

            while frequency_index < max_index && zero_run < LONGEST_LONG_RUN {
                if length(frequencies[frequency_index + 1]) > 0 {
                    break;
                }

                frequency_index += 1;
                zero_run += 1;
            }

            if zero_run >= 2 {
                if zero_run >= SHORTEST_LONG_RUN {
                    write_bits(6, LONG_ZEROCODE_RUN, &mut code_bits, &mut code_bit_count, &mut out)?;
                    write_bits(8, zero_run - SHORTEST_LONG_RUN, &mut code_bits, &mut code_bit_count, &mut out)?;
                }
                else {
                    write_bits(6, SHORT_ZEROCODE_RUN + zero_run - 2, &mut code_bits, &mut code_bit_count, &mut out)?;
                }

                frequency_index += 1; // we must increment or else this may go very wrong
                continue;
            }
        }

        write_bits(6, code_length, &mut code_bits, &mut code_bit_count, &mut out)?;
        frequency_index += 1;
    }

    if code_bit_count > 0 {
        out.write(&[
            (code_bits << (8 - code_bit_count)) as u8
        ])?;
    }

    Ok(())
}
///
/// Pack an encoding table:
///	- only code lengths, not actual codes, are stored
///	- runs of zeroes are compressed as follows:
///
///	  unpacked		packed
///	  --------------------------------
///	  1 zero		0	(6 bits)
///	  2 zeroes		59
///	  3 zeroes		60
///	  4 zeroes		61
///	  5 zeroes		62
///	  n zeroes (6 or more)	63 n-6	(6 + 8 bits)
///
Pack an encoding table:
only code lengths, not actual codes, are stored
runs of zeroes are compressed as follows.

unpacked              packed


Pack an encoding table : only code lengths not actual codes are stored runs of zeroes are compressed as follows . unpacked packed
fn pack_encoding_table(
    frequencies: &[u64],
    min_index: usize,
    max_index: usize,
    mut out: &mut Cursor<Vec<u8>>,
) -> UnitResult
{
    let mut code_bits = 0_u64;
    let mut code_bit_count = 0_u64;
    let mut frequency_index = min_index;
    while frequency_index <= max_index { // TODO slice iteration?
        let code_length = length(frequencies[frequency_index]);
        if code_length == 0 {
            let mut zero_run = 1;
            while frequency_index < max_index && zero_run < LONGEST_LONG_RUN {
                if length(frequencies[frequency_index + 1]) > 0 {
                    break;
                }
                frequency_index += 1;
                zero_run += 1;
            }
            if zero_run >= 2 {
                if zero_run >= SHORTEST_LONG_RUN {
                    write_bits(6, LONG_ZEROCODE_RUN, &mut code_bits, &mut code_bit_count, &mut out)?;
                    write_bits(8, zero_run - SHORTEST_LONG_RUN, &mut code_bits, &mut code_bit_count, &mut out)?;
                }
                else {
                    write_bits(6, SHORT_ZEROCODE_RUN + zero_run - 2, &mut code_bits, &mut code_bit_count, &mut out)?;
                }
                frequency_index += 1; // we must increment or else this may go very wrong
                continue;
            }
        }
        write_bits(6, code_length, &mut code_bits, &mut code_bit_count, &mut out)?;
        frequency_index += 1;
    }
    if code_bit_count > 0 {
        out.write(&[
            (code_bits << (8 - code_bit_count)) as u8
        ])?;
    }
    Ok(())
}
fn pack_encoding_table ( frequencies : & [ u64 ] , min_index : usize , max_index : usize , mut out : & mut Cursor < Vec < u8 > > , ) -> UnitResult { let mut code_bits = 0_u64 ; let mut code_bit_count = 0_u64 ; let mut frequency_index = min_index ; while frequency_index <= max_index { // TODO slice iteration? let code_length = length ( frequencies [ frequency_index ] ) ; if code_length == 0 { let mut zero_run = 1 ; while frequency_index < max_index && zero_run < LONGEST_LONG_RUN { if length ( frequencies [ frequency_index + 1 ] ) > 0 { break ; } frequency_index += 1 ; zero_run += 1 ; } if zero_run >= 2 { if zero_run >= SHORTEST_LONG_RUN { write_bits ( 6 , LONG_ZEROCODE_RUN , & mut code_bits , & mut code_bit_count , & mut out ) ? ; write_bits ( 8 , zero_run - SHORTEST_LONG_RUN , & mut code_bits , & mut code_bit_count , & mut out ) ? ; } else { write_bits ( 6 , SHORT_ZEROCODE_RUN + zero_run - 2 , & mut code_bits , & mut code_bit_count , & mut out ) ? ; } frequency_index += 1 ; // we must increment or else this may go very wrong continue ; } } write_bits ( 6 , code_length , & mut code_bits , & mut code_bit_count , & mut out ) ? ; frequency_index += 1 ; } if code_bit_count > 0 { out . write ( & [ ( code_bits << ( 8 - code_bit_count ) ) as u8 ] ) ? ; } Ok ( ( ) ) }
Pack an encoding table:
 only code lengths, not actual codes, are stored
 runs of zeroes are compressed as follows:
Pack an encoding table : only code lengths not actual codes are stored runs of zeroes are compressed as follows :
frequencies
&[u64]
min_index
usize
max_index
usize
out
&mut Cursor<Vec<u8>>
frequencies
&[u64]
min_index
usize
max_index
usize
out
&mut Cursor<Vec<u8>>