Skip to content

Commit

Permalink
update'
Browse files Browse the repository at this point in the history
  • Loading branch information
lxaw committed Nov 11, 2023
1 parent 7048bca commit 9d3170b
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 123 deletions.
5 changes: 5 additions & 0 deletions decoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Project Gutenberg eBook of Anna Karenina, by Leo Tolstoy
This eBook is for the use of anyone anywhere in the United States and most other parts of the world at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org. If you are not located in the United States, you will have to check the laws of the country where you are located before using this eBook.
Title: Anna Karenina
Author: Leo Tolstoy
Release Date: July 1, 1998 [eBook #1399]w
Binary file added out.bin
Binary file not shown.
50 changes: 50 additions & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
111
# 100100000
% 0100011
, 1000010
- 100100001
. 1001100
1 11000001
3 100100010
8 100100011
9 1000011
: 0000100
A 0000101
B 1001101
D 100100100
G 01000101
I 100100101
J 100100110
K 10001010
L 0000110
P 10001011
R 100100111
S 10001100
T 1001110
U 10001101
Y 110000000
[ 110000001
] 01000100
a 0011
b 1001111
c 110001
d 100101
e 011
f 100000
g 010000
h 10100
i 11001
j 10001110
k 1100001
l 00101
m 1000100
n 0101
o 1101
p 10001111
r 0001
s 10101
t 1011
u 01001
v 0000111
w 00000
y 00100
118 changes: 118 additions & 0 deletions src/file_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::collections::BTreeMap;
use std::io::{Read, Result,Write, BufReader, BufRead};
use std::fs::File;
use std::fs;

/*
Read a BTreeMap from the key file.
*/
pub fn get_hash_from_txt(file_name: &str) -> Result<BTreeMap<char, String> > {
// Create a BTreeMap to store the data
let mut data_map: BTreeMap<char, String> = BTreeMap::new();

if let Ok(file) = File::open(file_name) {
let reader = BufReader::new(file);

for (_line_number, line) in reader.lines().enumerate() {
if let Ok(line_text) = line {

// Check if the line has the expected format
if let (Some(first_char), Some(rest)) = (line_text.chars().next(),line_text.get(1..)) {
// Trim white space from the "rest" string
let trimmed_rest = rest.trim();
// Insert the data into the BTreeMap
data_map.insert(first_char, trimmed_rest.to_string());
}
}
}
}

Ok(data_map)
}

pub fn read_file_to_string(filename: &str) -> Result<String> {
// Open the file in read-only mode
let mut file = File::open(filename)?;

// Read the entire content of the file into a String
let mut content = String::new();
file.read_to_string(&mut content)?;

Ok(content)
}


pub fn get_file_size_bytes(filename: &str) -> Result<u64>{
match fs::metadata(filename) {
Ok(metadata) => {
let file_size = metadata.len();
return Ok(file_size);
}
Err(err) => {
return Err(err);
}
}
}

pub fn write_str_to_file(filename: &str, data: &Vec<bool>) -> std::io::Result<()>{
let mut file = File::create(filename)?;

let _byte_buffer: u8 = 0;
let _bit_position = data.len();

// Create a buffer to store bits
let mut bit_buffer: u8 = 0;
let mut bit_position = 0; // Start from the least significant bit

// read the bits
for bit in data.iter() {
if *bit {
bit_buffer |= 1 << bit_position;
}

bit_position += 1;

// If we've filled a byte, write it to the file
if bit_position == 8 {
file.write(&[bit_buffer])?;
bit_buffer = 0;
bit_position = 0;
}
}

// If there are any remaining bits in the buffer, write them and pad with '0's
println!("bit position at end: {}",bit_position);
let num_bits_to_write = 8 - bit_position;

let two = 2;

let bit_buffer = two.pow(num_bits_to_write)-1;

if bit_position != 0 {
file.write(&[bit_buffer])?;
}

let _ = file.flush();

Ok(())
}

pub fn print_hash_to_file(map: &BTreeMap<char,Vec<bool>>, filename: &str) -> std::io::Result<()>{
let mut file = File::create(filename)?;

for (key,value) in map{
file.write(&[key.to_owned() as u8])?;
file.write(&[' ' as u8])?;
for bool_val in value{
if *bool_val{
file.write(&['1' as u8])?;
}else{
file.write(&['0' as u8])?;
}
}
file.write(&['\n' as u8])?;
}
file.flush()?;

Ok(())
}
8 changes: 4 additions & 4 deletions src/huffman.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{collections::{BTreeMap,VecDeque}};
use super::node::{Node};
use std::collections::{BTreeMap,VecDeque};
use super::node::Node;

const SPECIAL_CHAR: char = '\0';
const NEW_LINE: char = '%';
Expand All @@ -9,7 +9,7 @@ fn build_huff_tree(nodes : &mut Vec<Node>) -> Node{
// 1) Order the nodes based on freq
// 2) Merge the two nodes with smallest freqs
// 3) repeat until one node left
while(nodes.len() > 1){
while nodes.len() > 1{
let new_freq = nodes[0].freq + nodes[1].freq;
let left_link = Some(Box::new(Node{
data:nodes[0].data,freq:nodes[0].freq,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn get_tree_root(msg: &String) -> Option<Box<Node>>{
let mut nodes = get_nodes(hm);
// sort in desc order
nodes.sort();
let mut tree_head = build_huff_tree(&mut nodes);
let tree_head = build_huff_tree(&mut nodes);
let mut tree_head_ref = Some(Box::new(tree_head));
mark_tree(&mut tree_head_ref,&mut Vec::new());

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod huffman;
pub mod node;
pub mod node;
pub mod file_reader;
Loading

0 comments on commit 9d3170b

Please sign in to comment.