From 2e57b250c6e931c13ed9ec1870e133c59151422b Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 27 Apr 2023 22:02:07 +0530 Subject: switch to an actually working trie crate --- Cargo.lock | 32 ++++++++++++++-------------- Cargo.toml | 2 +- src/lib.rs | 70 +++++++++++++++++++------------------------------------------ src/main.rs | 1 + 4 files changed, 40 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e49c8e5..68a1cb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,36 +6,36 @@ version = 3 name = "dict" version = "0.1.0" dependencies = [ - "radix_trie", + "qp-trie", ] [[package]] -name = "endian-type" -version = "0.1.2" +name = "new_debug_unreachable" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" [[package]] -name = "nibble_vec" -version = "0.1.0" +name = "qp-trie" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +checksum = "9569328cda9b68120dbbf855bac541eeb40c475d96a9a380cf8b5547bfe0c165" dependencies = [ - "smallvec", + "new_debug_unreachable", + "unreachable", ] [[package]] -name = "radix_trie" -version = "0.2.1" +name = "unreachable" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" dependencies = [ - "endian-type", - "nibble_vec", + "void", ] [[package]] -name = "smallvec" -version = "1.10.0" +name = "void" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index 044c314..8ac846b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -radix_trie = "0.2.1" +qp-trie = "0.8.1" diff --git a/src/lib.rs b/src/lib.rs index 1324f70..b286cb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,10 @@ pub mod lex; pub mod parse; mod utils; -use std::fmt; - -use radix_trie::{Trie, TrieCommon}; +use qp_trie::{ + wrapper::{BStr, BString}, + Iter, Trie, +}; pub struct Dict { inner: Trie, @@ -16,66 +17,39 @@ impl Dict { Self { inner: Trie::new() } } - fn insert(&mut self, entry: DictKey, value: DictValue) { - self.inner.map_with_default( - entry, - |dict_value| { - // TODO: this only merges defns, not notes/syns - for v in value.defn.iter() { - dict_value.defn.push(v); - } - }, - value.clone(), - ); + fn insert(&mut self, entry: &'static str, value: DictValue) { + let key = BString::from(entry); + let dict_value = self.inner.entry(key).or_insert(Default::default()); + for v in value.defn.into_iter() { + dict_value.defn.push(v); + } } - pub fn search<'dict, 'search>( - &'dict self, - search_term: &'search str, - ) -> SearchResults<'dict, 'search> { - self.inner - .subtrie(search_term) - .map_or(SearchResults::Empty, |subtrie| { - SearchResults::Hit(subtrie.iter()) - }) + pub fn search<'a, 's>(&'a self, search_term: &'s str) -> SearchResults<'a> { + SearchResults(self.inner.iter_prefix_str(search_term)) } } -pub enum SearchResults<'dict, 'search> { - Empty, - Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>), -} +pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>); -impl<'dict, 'search> SearchResults<'dict, 'search> { +impl<'a> SearchResults<'a> { // mutable ref here to advance the iterator present in Self::Hit pub fn print(&mut self) { - match self { - Self::Hit(results) => { - while let Some((key, value)) = results.next() { - if value.defn.len() > 1 { - for (def, idx) in value.defn.iter().zip(1..) { - println!("{}({}) {}", key, idx, def.replace('\n', " ")); - } - } else { - println!("{} {}", key, value.defn[0].replace('\n', " ")); - } - - // if let Some(note) = value.note { - // print!("\t{}", note); - // } - // if let Some(synonym) = value.synonym { - // print!("\t{}", synonym); - // } + while let Some((key, value)) = self.0.next() { + if value.defn.len() > 1 { + for (def, idx) in value.defn.iter().zip(1..) { + println!("{}({}) {}", key.as_str(), idx, def.replace('\n', " ")); } + } else { + println!("{} {}", key.as_str(), value.defn[0].replace('\n', " ")); } - Self::Empty => (), } } } -type DictKey = &'static str; +type DictKey = BString; -#[derive(Clone)] +#[derive(Clone, Debug, Default)] pub struct DictValue { defn: Vec<&'static str>, note: Option<&'static str>, diff --git a/src/main.rs b/src/main.rs index 9def90c..ffa7757 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ fn main() { .filter_map(Result::ok) .fold(ParseState::new(), ParseState::advance) .finish() + //.children(); .search(search_term.to_ascii_uppercase().as_str()) .print() } -- cgit v1.2.3