|
|
@@ -55,7 +55,6 @@ impl CanonicalHufftree {
|
|
|
}
|
|
|
|
|
|
character_and_codes.sort();
|
|
|
- // println!("Ordered characters: {:?}", character_and_codes);
|
|
|
let mut character_and_codes: Vec<CharTempCode> =
|
|
|
character_and_codes.into_iter().rev().collect();
|
|
|
// println!("Ordered characters: {:?}", character_and_codes);
|
|
|
@@ -68,10 +67,12 @@ impl CanonicalHufftree {
|
|
|
|
|
|
// This will results in the vector being ordered with the most frequent
|
|
|
// character first, ordered by code length.
|
|
|
- storage_char_codes.push((temp_char.character, temp_char.code_length as u32));
|
|
|
+ storage_char_codes.push((temp_char.character.clone(), temp_char.code_length as u32));
|
|
|
+
|
|
|
+ let mut code = BitVec::new();
|
|
|
+ code.grow(temp_char.code_length, false);
|
|
|
+
|
|
|
if first {
|
|
|
- let mut code = BitVec::new();
|
|
|
- code.grow(temp_char.code_length, false);
|
|
|
prev_length = temp_char.code_length;
|
|
|
|
|
|
output_characters_and_codes.insert(temp_char.character, code);
|
|
|
@@ -83,16 +84,31 @@ impl CanonicalHufftree {
|
|
|
working_code += 1;
|
|
|
working_code = working_code << (temp_char.code_length - prev_length);
|
|
|
|
|
|
+
|
|
|
+ let code = convert_no_to_bit_vec_known_length(working_code, &mut code);
|
|
|
output_characters_and_codes
|
|
|
- .insert(temp_char.character, convert_no_to_bit_vec(working_code));
|
|
|
+ .insert_no_overwrite(
|
|
|
+ temp_char.character,
|
|
|
+ code
|
|
|
+ )
|
|
|
+ .expect("There was already a character with that code.");
|
|
|
+
|
|
|
+ assert!(output_characters_and_codes.contains_left(&temp_char.character));
|
|
|
} else {
|
|
|
assert_eq!(
|
|
|
temp_char.code_length, prev_length,
|
|
|
"Something went really wrong if we got here."
|
|
|
);
|
|
|
working_code += 1;
|
|
|
+
|
|
|
+ let code = convert_no_to_bit_vec_known_length(working_code, &mut code);
|
|
|
+
|
|
|
output_characters_and_codes
|
|
|
- .insert(temp_char.character, convert_no_to_bit_vec(working_code));
|
|
|
+ .insert_no_overwrite(
|
|
|
+ temp_char.character,
|
|
|
+ code
|
|
|
+ )
|
|
|
+ .expect("There was already a character with that code.");
|
|
|
}
|
|
|
|
|
|
prev_length = temp_char.code_length;
|
|
|
@@ -117,12 +133,13 @@ impl CanonicalHufftree {
|
|
|
let mut working_code: u32 = 0b0;
|
|
|
while temp_storage.len() > 0 {
|
|
|
let (temp_char, code_length) = temp_storage.pop().unwrap();
|
|
|
-
|
|
|
// This will result in the vector being ordered with the most frequent
|
|
|
// character first, ordered by code length.
|
|
|
+ //
|
|
|
+ let mut code = BitVec::new();
|
|
|
+ code.grow(code_length as usize, false);
|
|
|
+
|
|
|
if first {
|
|
|
- let mut code = BitVec::new();
|
|
|
- code.grow(code_length as usize, false);
|
|
|
prev_length = code_length;
|
|
|
|
|
|
bi.insert(temp_char, code);
|
|
|
@@ -134,14 +151,22 @@ impl CanonicalHufftree {
|
|
|
working_code += 1;
|
|
|
working_code = working_code << (code_length - prev_length);
|
|
|
|
|
|
- bi.insert(temp_char, convert_no_to_bit_vec(working_code));
|
|
|
+ let code = convert_no_to_bit_vec_known_length(working_code, &mut code);
|
|
|
+ bi.insert_no_overwrite(
|
|
|
+ temp_char,
|
|
|
+ code,
|
|
|
+ ).expect("There was already a character with that code.");
|
|
|
} else {
|
|
|
assert_eq!(
|
|
|
code_length, prev_length,
|
|
|
"Something went really wrong if we got here."
|
|
|
);
|
|
|
working_code += 1;
|
|
|
- bi.insert(temp_char, convert_no_to_bit_vec(working_code));
|
|
|
+ let code = convert_no_to_bit_vec_known_length(working_code, &mut code);
|
|
|
+ bi.insert_no_overwrite(
|
|
|
+ temp_char,
|
|
|
+ code,
|
|
|
+ ).expect("There was already a character with that code.");
|
|
|
}
|
|
|
|
|
|
prev_length = code_length;
|
|
|
@@ -185,6 +210,11 @@ impl CanonicalHufftree {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ println!("Decoded text: {}", decoded_text);
|
|
|
+ println!("Buff: {:?}", buffer);
|
|
|
+
|
|
|
+ println!("\nSelf:\n{:?}\n\n", self.characters_and_codes);
|
|
|
+
|
|
|
if !buffer.is_empty() {
|
|
|
Err("Text was not decoded properly (trailing bits).")
|
|
|
} else {
|
|
|
@@ -215,6 +245,23 @@ pub fn convert_no_to_bit_vec(mut numb: u32) -> BitVec {
|
|
|
output_vec
|
|
|
}
|
|
|
|
|
|
+pub fn convert_no_to_bit_vec_known_length(mut numb: u32, bits: &mut BitVec) -> BitVec {
|
|
|
+ let mut counter = 0;
|
|
|
+ while numb > 0 {
|
|
|
+ if numb % 2 == 0 {
|
|
|
+ bits.set(counter ,false);
|
|
|
+ } else {
|
|
|
+ bits.set(counter, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ numb = numb / 2;
|
|
|
+ counter += 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ let bits = bits.iter().rev().collect();
|
|
|
+ bits
|
|
|
+}
|
|
|
+
|
|
|
#[cfg(test)]
|
|
|
mod canonical_tests {
|
|
|
use std::collections::HashMap;
|