From 97376bad72eb7a991fb4634f22c0158eaec72fec Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Sat, 7 Dec 2024 16:23:24 -0600 Subject: [PATCH] Work on Trie. --- src/lib.rs | 1 + src/util/mod.rs | 1 + src/util/trie.rs | 120 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/util/mod.rs create mode 100644 src/util/trie.rs diff --git a/src/lib.rs b/src/lib.rs index b301d39..b3fa46b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod lexer; pub mod parser; +mod util; pub mod vm; diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..225296c --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1 @@ +pub mod trie; diff --git a/src/util/trie.rs b/src/util/trie.rs new file mode 100644 index 0000000..43dbe67 --- /dev/null +++ b/src/util/trie.rs @@ -0,0 +1,120 @@ +// use crate::util::trie::GetEdgeResult::{EdgeKeyIsPrefix, EqualKeys, KeyIsPrefix}; +// use std::collections::HashMap; +// use std::rc::Rc; +// +// pub struct RadixTrie { +// root: RadixTrieNode, +// } +// +// struct RadixTrieNode { +// edges: HashMap>, +// value: Option>, +// } +// +// impl RadixTrie { +// pub fn new() -> Self { +// RadixTrie { +// root: Default::default(), +// } +// } +// +// pub fn insert(&mut self, key: &str, value: &Rc) { +// self.root.insert_helper(key, value); +// } +// +// pub fn remove(&mut self, key: &str) { +// todo!() +// } +// +// pub fn find(&self, key: &str) -> Option> { +// todo!() +// } +// } +// +// impl Default for RadixTrieNode { +// fn default() -> Self { +// RadixTrieNode::new() +// } +// } +// +// enum GetEdgeResult<'a, T> { +// EqualKeys, +// KeyIsPrefix(&'a str, &'a mut RadixTrieNode), // common prefix and target node +// EdgeKeyIsPrefix(&'a str, &'a mut RadixTrieNode), // non-common suffix and target node, +// None, +// } +// +// impl RadixTrieNode { +// fn new() -> Self { +// RadixTrieNode { +// edges: HashMap::new(), +// value: None, +// } +// } +// +// fn get_edge<'a>(&'a mut self, key: &'a str) -> GetEdgeResult<'a, T> { +// for (edge_key, edge_node) in self.edges.iter_mut() { +// // Case: edge_key == key: overwrite data +// if *key == *edge_key { +// return EqualKeys; +// } +// +// // Find how many common characters there are starting from the beginning and terminating +// // as soon as there is no match +// let mut i = 0; +// 'number_of_common_chars: for (key_char, edge_key_char) in +// key.chars().zip(edge_key.chars()) +// { +// if key_char == edge_key_char { +// i += 1; +// } else { +// break 'number_of_common_chars; +// } +// } +// +// // Case: key's first char does not match at all: continue searching +// if i == 0 { +// continue; +// } +// +// // Case: key is prefix of edge_key +// if i < edge_key.len() { +// return KeyIsPrefix(key, edge_node); +// } +// +// if i == edge_key.len() { +// panic!( +// "Should not have gotten here: counted common chars equals edge_key's length." +// ) +// } +// +// return EdgeKeyIsPrefix(&edge_key[i..], edge_node); +// } +// GetEdgeResult::None +// } +// +// fn insert_helper(&mut self, key: &str, value: &Rc) { +// match self.get_edge(key) { +// EqualKeys => { +// let edge_node = self.edges.get_mut(key).unwrap(); +// edge_node.value = Some(value.clone()); +// } +// KeyIsPrefix(prefix, edge_node) => { +// // split like asparagus break +// let old_target_node = self.edges.remove(key).unwrap(); +// +// +// let mut common_prefix_node: RadixTrieNode = RadixTrieNode::new(); +// } +// EdgeKeyIsPrefix(suffix, edge_node) => { +// // recursive on edge_node +// edge_node.insert_helper(suffix, value); +// } +// GetEdgeResult::None => { +// let mut new_edge_node = RadixTrieNode::new(); +// new_edge_node.value = Some(value.clone()); +// self.edges.insert(String::from(key), new_edge_node); +// } +// } +// } +// }