node and bits

This commit is contained in:
2025-04-07 20:12:04 +02:00
parent ebd3e33a03
commit c4c0c32f0e
6 changed files with 174 additions and 11 deletions

29
src/byte_writer.rs Normal file
View File

@@ -0,0 +1,29 @@
pub fn string_to_binary(bits: &str) -> Vec<u8> {
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
}

View File

@@ -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<String> = 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<HashMap<char, i32>, std::io::Error> {
let mut hashmap: HashMap<char, i32> = 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<char, i32>) -> Vec<Node> {
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
}

43
src/node.rs Normal file
View File

@@ -0,0 +1,43 @@
pub struct Node {
pub val: Option<Box<char>>,
pub l: Option<Box<Node>>,
pub r: Option<Box<Node>>,
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>) -> 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()
}