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

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
}