node and bits
This commit is contained in:
24
3
Normal file
24
3
Normal file
@@ -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<char, i32> = 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(())
|
||||
}
|
||||
25
fo.txt
25
fo.txt
@@ -1,2 +1,23 @@
|
||||
éè
|
||||
é
|
||||
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
|
||||
|
||||
29
src/byte_writer.rs
Normal file
29
src/byte_writer.rs
Normal 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
|
||||
}
|
||||
62
src/main.rs
62
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<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
43
src/node.rs
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user