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 --- src/lib.rs | 70 +++++++++++++++++++------------------------------------------ src/main.rs | 1 + 2 files changed, 23 insertions(+), 48 deletions(-) (limited to 'src') 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