Skip to content

Commit

Permalink
writer class start
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Dec 3, 2023
1 parent d9498f6 commit 7f539b7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 89 deletions.
74 changes: 0 additions & 74 deletions src/index.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod writer;
51 changes: 51 additions & 0 deletions src/io/writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::fs::File;

const BUFFER_SIZE: usize = 1024;

#[allow(dead_code)]
pub struct Writer {
file: File,
buffer: u128,
free: u16,
vec_buffer: [u128; BUFFER_SIZE],
}

#[allow(dead_code)]
impl Writer {
pub fn new(filename: &str) -> Writer {
let file = File::create(filename).unwrap();
let buffer = 0;
let free = 128;
let vec_buffer: [u128; BUFFER_SIZE] = [0; BUFFER_SIZE];
Writer {
file,
buffer,
free,
vec_buffer,
}
}

pub fn int_to_gamma(n: u32) -> (u64, u32) {
let msb = 31 - n.leading_zeros();
let unary: u32 = 1 << msb;
let gamma: u64 = (((n ^ unary) as u64) << (msb + 1)) | unary as u64;
(gamma, 2 * msb + 1)
}
}

#[cfg(test)]
mod test {

use super::*;

#[test]
fn test_gamma_coding() {
let (g, l) = Writer::int_to_gamma(1);
assert_eq!(format!("{g:b}"), "1");
assert_eq!(l, 1);

let (g, l) = Writer::int_to_gamma(7);
assert_eq!(format!("{g:b}"), "11100");
assert_eq!(l, 5);
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod index;
mod io;
mod trie;

fn main() {}
36 changes: 22 additions & 14 deletions src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
use std::collections::{HashMap, VecDeque};

#[derive(Default)]
struct Node {
value: Option<usize>,
children: HashMap<char, Node>,
struct Node<T> {
value: Option<T>,
children: HashMap<char, Node<T>>,
}

pub struct Trie {
root: Node,
#[allow(dead_code)]
pub struct Trie<T>
where
T: Default + Clone + Copy,
{
root: Node<T>,
}

impl Trie {
pub fn new() -> Trie {
#[allow(dead_code)]
impl<T> Trie<T>
where
T: Default + Clone + Copy,
{
pub fn new() -> Trie<T> {
Trie {
root: Node::default(),
}
}

pub fn insert(&mut self, word: &str, value: usize) {
pub fn insert(&mut self, word: &str, value: T) {
let mut node = &mut self.root;

for c in word.chars() {
Expand All @@ -27,16 +35,16 @@ impl Trie {
node.value = Some(value);
}

pub fn get(&self, word: &str) -> Option<usize> {
pub fn get(&self, word: &str) -> Option<T> {
self.get_internal(word).and_then(|n| n.value)
}

pub fn get_by_prefix(&self, prefix: &str) -> Vec<usize> {
pub fn get_by_prefix(&self, prefix: &str) -> Vec<T> {
self.get_internal(prefix)
.map_or_else(Vec::new, |n| self.visit(n))
}

fn get_internal(&self, word: &str) -> Option<&Node> {
fn get_internal(&self, word: &str) -> Option<&Node<T>> {
let mut node = &self.root;

for c in word.chars() {
Expand All @@ -49,9 +57,9 @@ impl Trie {
Some(node)
}

fn visit(&self, node: &Node) -> Vec<usize> {
let mut res: Vec<usize> = Vec::new();
let mut queue: VecDeque<&Node> = VecDeque::new();
fn visit(&self, node: &Node<T>) -> Vec<T> {
let mut res: Vec<T> = Vec::new();
let mut queue: VecDeque<&Node<T>> = VecDeque::new();
queue.push_back(node);

while let Some(el) = queue.pop_front() {
Expand Down

0 comments on commit 7f539b7

Please sign in to comment.