Work on Trie.
This commit is contained in:
		
							parent
							
								
									d4280f40e1
								
							
						
					
					
						commit
						97376bad72
					
				@ -1,3 +1,4 @@
 | 
			
		||||
pub mod lexer;
 | 
			
		||||
pub mod parser;
 | 
			
		||||
mod util;
 | 
			
		||||
pub mod vm;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								src/util/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/util/mod.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
pub mod trie;
 | 
			
		||||
							
								
								
									
										120
									
								
								src/util/trie.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								src/util/trie.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,120 @@
 | 
			
		||||
// use crate::util::trie::GetEdgeResult::{EdgeKeyIsPrefix, EqualKeys, KeyIsPrefix};
 | 
			
		||||
// use std::collections::HashMap;
 | 
			
		||||
// use std::rc::Rc;
 | 
			
		||||
// 
 | 
			
		||||
// pub struct RadixTrie<T> {
 | 
			
		||||
//     root: RadixTrieNode<T>,
 | 
			
		||||
// }
 | 
			
		||||
// 
 | 
			
		||||
// struct RadixTrieNode<T> {
 | 
			
		||||
//     edges: HashMap<String, RadixTrieNode<T>>,
 | 
			
		||||
//     value: Option<Rc<T>>,
 | 
			
		||||
// }
 | 
			
		||||
// 
 | 
			
		||||
// impl<T> RadixTrie<T> {
 | 
			
		||||
//     pub fn new() -> Self {
 | 
			
		||||
//         RadixTrie {
 | 
			
		||||
//             root: Default::default(),
 | 
			
		||||
//         }
 | 
			
		||||
//     }
 | 
			
		||||
// 
 | 
			
		||||
//     pub fn insert(&mut self, key: &str, value: &Rc<T>) {
 | 
			
		||||
//         self.root.insert_helper(key, value);
 | 
			
		||||
//     }
 | 
			
		||||
// 
 | 
			
		||||
//     pub fn remove(&mut self, key: &str) {
 | 
			
		||||
//         todo!()
 | 
			
		||||
//     }
 | 
			
		||||
// 
 | 
			
		||||
//     pub fn find(&self, key: &str) -> Option<Rc<T>> {
 | 
			
		||||
//         todo!()
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
// 
 | 
			
		||||
// impl<T> Default for RadixTrieNode<T> {
 | 
			
		||||
//     fn default() -> Self {
 | 
			
		||||
//         RadixTrieNode::new()
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
// 
 | 
			
		||||
// enum GetEdgeResult<'a, T> {
 | 
			
		||||
//     EqualKeys,
 | 
			
		||||
//     KeyIsPrefix(&'a str, &'a mut RadixTrieNode<T>), // common prefix and target node
 | 
			
		||||
//     EdgeKeyIsPrefix(&'a str, &'a mut RadixTrieNode<T>), // non-common suffix and target node,
 | 
			
		||||
//     None,
 | 
			
		||||
// }
 | 
			
		||||
// 
 | 
			
		||||
// impl<T> RadixTrieNode<T> {
 | 
			
		||||
//     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<T>) {
 | 
			
		||||
//         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<T> = 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);
 | 
			
		||||
//             }
 | 
			
		||||
//         }
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user