Răsfoiți Sursa

This is solvable, but not feeling it rn...

AvariceLHubris 1 an în urmă
părinte
comite
e4f3e58aab
2 a modificat fișierele cu 13 adăugiri și 6 ștergeri
  1. 1 1
      src/hufftree/canonical.rs
  2. 12 5
      src/storage.rs

+ 1 - 1
src/hufftree/canonical.rs

@@ -96,7 +96,7 @@ impl CanonicalHufftree {
         }
     }
 
-    pub fn from_bimap(bi: BiMap<char, BitVec>) -> Self {
+    pub fn from_bimap(bi: BiMap<char, u32>) -> Self {
         CanonicalHufftree {
             characters_and_codes: bi
         }

+ 12 - 5
src/storage.rs

@@ -69,7 +69,7 @@ pub fn read_tree_and_text<F: Read>(reader: &mut F) -> String {
 
     let mut length_of_file_in_bits: u32 = four_b_to_u32(&length_of_file_in_bits);
 
-    let mut working_bimap: BiMap<char, BitVec> = BiMap::new();
+    let mut working_bimap: BiMap<char, u32> = BiMap::new();
 
     let mut char_and_code: [u8; 8] = [0; 8];
     reader
@@ -85,10 +85,12 @@ pub fn read_tree_and_text<F: Read>(reader: &mut F) -> String {
         let c: String = String::from_utf8(Vec::from(c)).expect("Corrupted data 🪳");
         // There should only be one character per 4 bytes.
         let c = c.chars().next().expect("Corrupted data 🪳");
-        let code = BitVec::from_bytes(&char_and_code[0..4]);
-        println!("Character: {:?}", c);
+        // let code = BitVec::from_bytes(&char_and_code[0..4]);
+        let mut code = [0;4];
+        code.clone_from_slice(&char_and_code[0..4]);
+        // println!("Character: {:?}", c);
 
-        working_bimap.insert(c, code);
+        working_bimap.insert(c, four_b_to_u32(&code));
 
         length_of_file_in_bits -= 64;
 
@@ -108,7 +110,9 @@ pub fn read_tree_and_text<F: Read>(reader: &mut F) -> String {
 
     if length_of_file_in_bits < 32 {
         let mut rest_of_binary = Vec::new();
-        reader.read_to_end(&mut rest_of_binary).expect("Could not read data to end.");
+        reader
+            .read_to_end(&mut rest_of_binary)
+            .expect("Could not read data to end.");
         println!("Rest of binary: {:?}", rest_of_binary);
         let rest_of_binary = &rest_of_binary[4..];
         println!("Rest of binary: {:?}", rest_of_binary);
@@ -116,6 +120,9 @@ pub fn read_tree_and_text<F: Read>(reader: &mut F) -> String {
         let mut bits = BitVec::from_bytes(rest_of_binary);
         bits.split_off(length_of_file_in_bits as usize);
         println!("Bit vec: {:?}", bits);
+
+        let can_tree = CanonicalHufftree::from_bimap(working_bimap);
+        return can_tree.decode_text(bits).unwrap();
     }
 
     let mut encoded_text = BitVec::from_bytes(&char_and_code[4..8]);