Skip to content

Commit

Permalink
writer init
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Dec 3, 2023
1 parent 9718be5 commit 6c260a8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 38 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
name = "search-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
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);
}
}
17 changes: 2 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
mod io;
mod trie;
use trie::Trie;

fn main() {
let mut t = Trie::new();

t.insert("hello", 1);
t.insert("hell", 2);
t.insert("hey", 3);

println!("{:?}", t.get("hello"));
println!("{:?}", t.get("hell"));
println!("{:?}", t.get("hey"));
println!("{:?}", t.get("he"));

println!("{:?}", t.get_by_prefix("hel"));
}
fn main() {}
44 changes: 25 additions & 19 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<i32>,
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: i32) {
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<i32> {
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<i32> {
pub fn get_by_prefix(&self, prefix: &str) -> Vec<T> {
self.get_internal(prefix)
.map_or_else(|| Vec::new(), |n| self.visit(n))
.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<i32> {
let mut res: Vec<i32> = 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 All @@ -76,19 +84,17 @@ mod tests {
#[test]
fn test_insert_and_get() {
let mut trie = Trie::new();

trie.insert("hello", 42);

assert_eq!(trie.get("hello"), Some(42));
assert_eq!(trie.get("world"), None);
}

// Add more tests...

#[test]
fn test_get_by_prefix() {
let mut trie = Trie::new();

trie.insert("hello", 42);
trie.insert("help", 99);
trie.insert("world", 123);
Expand Down

0 comments on commit 6c260a8

Please sign in to comment.