From c4c0c32f0eda94b0abf70a13332c9e0169e34934 Mon Sep 17 00:00:00 2001 From: Dukantic Date: Mon, 7 Apr 2025 20:12:04 +0200 Subject: [PATCH] node and bits --- 3 | 24 ++++++++++++++++++ fo.txt | 25 +++++++++++++++++-- src/byte_writer.rs | 29 ++++++++++++++++++++++ src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++------ src/node.rs | 43 ++++++++++++++++++++++++++++++++ test.txt | 2 +- 6 files changed, 174 insertions(+), 11 deletions(-) create mode 100644 3 create mode 100644 src/byte_writer.rs create mode 100644 src/node.rs diff --git a/3 b/3 new file mode 100644 index 0000000..3ae40cb --- /dev/null +++ b/3 @@ -0,0 +1,24 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::prelude::*; + +fn main() -> std::io::Result<()> { + let mut file = File::open("test.txt")?; + let mut contents = String::new(); + let mut hashmap: HashMap = HashMap::new(); + file.read_to_string(&mut contents)?; + for c in contents.chars() { + if let Some(value) = hashmap.get(&c) { + hashmap.insert(value, hashmap.get(value) + 1); + } + //println!("{:?}", contents.chars().nth(i)); + } + /* + contents.push('é'); + let mut file2 = File::create("fo.txt")?; + let test: i64 = 10000000; + //writeln!(file2, "{}", test)?; + file2.write_all(&test.to_be_bytes())?; + */ + Ok(()) +} diff --git a/fo.txt b/fo.txt index e313070..04a91ae 100644 --- a/fo.txt +++ b/fo.txt @@ -1,2 +1,23 @@ -éè -é \ No newline at end of file +r:1 +l:3 + +:1 +a:6 +i:3 +k:1 +v:1 +o:3 + :10 +s:5 +S:1 +d:2 +j:1 +g:1 +t:2 +e:4 +,:1 +m:2 +f:1 +u:3 +n:1 +p:1 diff --git a/src/byte_writer.rs b/src/byte_writer.rs new file mode 100644 index 0000000..90e2710 --- /dev/null +++ b/src/byte_writer.rs @@ -0,0 +1,29 @@ +pub fn string_to_binary(bits: &str) -> Vec { + let mut bytes = Vec::new(); + let mut current_byte: u8 = 0; + + let mut count: u8 = 0; + for c in bits.chars() { + current_byte <<= 1; + if c == '1' { + current_byte += 1; + } + count += 1; + + if count >= 8 { + bytes.push(current_byte); + current_byte = 0; + count = 0; + } + } + + if count > 0 { + while count < 8 { + current_byte <<= 1; + count += 1; + } + bytes.push(current_byte); + } + + bytes +} diff --git a/src/main.rs b/src/main.rs index bbe2e9a..17dba16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,61 @@ +use std::collections::HashMap; +use std::env; use std::fs::File; use std::io::prelude::*; +mod byte_writer; +mod node; + +use byte_writer::string_to_binary; +use node::Node; + fn main() -> std::io::Result<()> { - let mut file = File::open("test.txt")?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - for i in 0..contents.len() { - println!("{:?}", contents.chars().nth(i)); + let args: Vec = env::args().collect(); + + // Afficher les arguments + println!("Arguments passés au programme :"); + for (index, arg) in args.iter().enumerate() { + println!("Argument {}: {}", index, arg); } - contents.push('é'); - let mut file2 = File::create("fo.txt")?; - file2.write_all(contents.as_bytes())?; + + let file = File::open("test.txt")?; + //let mut file2 = File::create("fo.txt")?; + + let map = get_map_file(file).expect("Failed to load!"); + let vec = nodes_at_vec(map); + let root = node::create_tree(vec); + eprintln!("root.n = {:#?}", root.n); + + let mut v = string_to_binary("101010111010010101"); + eprintln!("v = {:#?}", v); + //writeln!(file2, "{}", tes;t)?; + //file2.write_all(&test.to_be_bytes())?; + //file2.write_all(&[*key as u8])?; + //writeln!(file2, ":{}", value)?; + //file2.write_all(&value.to_be_bytes())?; Ok(()) } + +fn get_map_file(mut file: File) -> Result, std::io::Error> { + let mut hashmap: HashMap = HashMap::new(); + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + for c in contents.chars() { + let counter = hashmap.entry(c).or_insert(0); + *counter += 1; + } + Ok(hashmap) +} + +fn nodes_at_vec(map: HashMap) -> Vec { + let mut nodes = Vec::new(); + + for (key, value) in &map { + let mut node = Node::new(); + node.set_char(*key); + node.set_counter(*value); + nodes.push(node); + } + nodes.sort_by(|a, b| a.val.cmp(&b.val)); + nodes +} diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 0000000..1ddf5dd --- /dev/null +++ b/src/node.rs @@ -0,0 +1,43 @@ +pub struct Node { + pub val: Option>, + pub l: Option>, + pub r: Option>, + pub n: i32, +} + +impl Node { + pub fn new() -> Self { + Node { + val: None, + l: None, + r: None, + n: 0, + } + } + + pub fn set_char(&mut self, v: char) { + self.val = Some(Box::new(v)); + } + + pub fn set_counter(&mut self, n: i32) { + self.n = n; + } + + pub fn insert(&mut self, l: Node, r: Node) { + self.l = Some(Box::new(l)); + self.r = Some(Box::new(r)); + } +} + +pub fn create_tree(mut nodes: Vec) -> Node { + while nodes.len() > 1 { + nodes.sort_by_key(|node| node.n); + let mut new_root = Node::new(); + let l = nodes.remove(0); + let r = nodes.remove(0); + new_root.set_counter(l.n + r.n); + new_root.insert(l, r); + nodes.push(new_root); + } + nodes.pop().unwrap() +} diff --git a/test.txt b/test.txt index f5daee3..28d0af9 100644 --- a/test.txt +++ b/test.txt @@ -1 +1 @@ -éè +coucou