aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2023-04-27 17:32:07 +0100
committerAkshay <[email protected]>2023-04-27 17:32:07 +0100
commit2e57b250c6e931c13ed9ec1870e133c59151422b (patch)
tree3642b89cd7fadd9dad0dc63438160ecb6fcdbf6d
parent7f9f70d0c7f93f11fa896e5544a5da59319a56f9 (diff)
switch to an actually working trie crateHEADmaster
-rw-r--r--Cargo.lock32
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs70
-rw-r--r--src/main.rs1
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
6name = "dict" 6name = "dict"
7version = "0.1.0" 7version = "0.1.0"
8dependencies = [ 8dependencies = [
9 "radix_trie", 9 "qp-trie",
10] 10]
11 11
12[[package]] 12[[package]]
13name = "endian-type" 13name = "new_debug_unreachable"
14version = "0.1.2" 14version = "1.0.4"
15source = "registry+https://github.com/rust-lang/crates.io-index" 15source = "registry+https://github.com/rust-lang/crates.io-index"
16checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" 16checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
17 17
18[[package]] 18[[package]]
19name = "nibble_vec" 19name = "qp-trie"
20version = "0.1.0" 20version = "0.8.1"
21source = "registry+https://github.com/rust-lang/crates.io-index" 21source = "registry+https://github.com/rust-lang/crates.io-index"
22checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" 22checksum = "9569328cda9b68120dbbf855bac541eeb40c475d96a9a380cf8b5547bfe0c165"
23dependencies = [ 23dependencies = [
24 "smallvec", 24 "new_debug_unreachable",
25 "unreachable",
25] 26]
26 27
27[[package]] 28[[package]]
28name = "radix_trie" 29name = "unreachable"
29version = "0.2.1" 30version = "1.0.0"
30source = "registry+https://github.com/rust-lang/crates.io-index" 31source = "registry+https://github.com/rust-lang/crates.io-index"
31checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" 32checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
32dependencies = [ 33dependencies = [
33 "endian-type", 34 "void",
34 "nibble_vec",
35] 35]
36 36
37[[package]] 37[[package]]
38name = "smallvec" 38name = "void"
39version = "1.10.0" 39version = "1.0.2"
40source = "registry+https://github.com/rust-lang/crates.io-index" 40source = "registry+https://github.com/rust-lang/crates.io-index"
41checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" 41checksum = "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"
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 7
8[dependencies] 8[dependencies]
9radix_trie = "0.2.1" 9qp-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;
3pub mod parse; 3pub mod parse;
4mod utils; 4mod utils;
5 5
6use std::fmt; 6use qp_trie::{
7 7 wrapper::{BStr, BString},
8use radix_trie::{Trie, TrieCommon}; 8 Iter, Trie,
9};
9 10
10pub struct Dict { 11pub struct Dict {
11 inner: Trie<DictKey, DictValue>, 12 inner: Trie<DictKey, DictValue>,
@@ -16,66 +17,39 @@ impl Dict {
16 Self { inner: Trie::new() } 17 Self { inner: Trie::new() }
17 } 18 }
18 19
19 fn insert(&mut self, entry: DictKey, value: DictValue) { 20 fn insert(&mut self, entry: &'static str, value: DictValue) {
20 self.inner.map_with_default( 21 let key = BString::from(entry);
21 entry, 22 let dict_value = self.inner.entry(key).or_insert(Default::default());
22 |dict_value| { 23 for v in value.defn.into_iter() {
23 // TODO: this only merges defns, not notes/syns 24 dict_value.defn.push(v);
24 for v in value.defn.iter() { 25 }
25 dict_value.defn.push(v);
26 }
27 },
28 value.clone(),
29 );
30 } 26 }
31 27
32 pub fn search<'dict, 'search>( 28 pub fn search<'a, 's>(&'a self, search_term: &'s str) -> SearchResults<'a> {
33 &'dict self, 29 SearchResults(self.inner.iter_prefix_str(search_term))
34 search_term: &'search str,
35 ) -> SearchResults<'dict, 'search> {
36 self.inner
37 .subtrie(search_term)
38 .map_or(SearchResults::Empty, |subtrie| {
39 SearchResults::Hit(subtrie.iter())
40 })
41 } 30 }
42} 31}
43 32
44pub enum SearchResults<'dict, 'search> { 33pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>);
45 Empty,
46 Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>),
47}
48 34
49impl<'dict, 'search> SearchResults<'dict, 'search> { 35impl<'a> SearchResults<'a> {
50 // mutable ref here to advance the iterator present in Self::Hit 36 // mutable ref here to advance the iterator present in Self::Hit
51 pub fn print(&mut self) { 37 pub fn print(&mut self) {
52 match self { 38 while let Some((key, value)) = self.0.next() {
53 Self::Hit(results) => { 39 if value.defn.len() > 1 {
54 while let Some((key, value)) = results.next() { 40 for (def, idx) in value.defn.iter().zip(1..) {
55 if value.defn.len() > 1 { 41 println!("{}({}) {}", key.as_str(), idx, def.replace('\n', " "));
56 for (def, idx) in value.defn.iter().zip(1..) {
57 println!("{}({}) {}", key, idx, def.replace('\n', " "));
58 }
59 } else {
60 println!("{} {}", key, value.defn[0].replace('\n', " "));
61 }
62
63 // if let Some(note) = value.note {
64 // print!("\t{}", note);
65 // }
66 // if let Some(synonym) = value.synonym {
67 // print!("\t{}", synonym);
68 // }
69 } 42 }
43 } else {
44 println!("{} {}", key.as_str(), value.defn[0].replace('\n', " "));
70 } 45 }
71 Self::Empty => (),
72 } 46 }
73 } 47 }
74} 48}
75 49
76type DictKey = &'static str; 50type DictKey = BString;
77 51
78#[derive(Clone)] 52#[derive(Clone, Debug, Default)]
79pub struct DictValue { 53pub struct DictValue {
80 defn: Vec<&'static str>, 54 defn: Vec<&'static str>,
81 note: Option<&'static str>, 55 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() {
9 .filter_map(Result::ok) 9 .filter_map(Result::ok)
10 .fold(ParseState::new(), ParseState::advance) 10 .fold(ParseState::new(), ParseState::advance)
11 .finish() 11 .finish()
12 //.children();
12 .search(search_term.to_ascii_uppercase().as_str()) 13 .search(search_term.to_ascii_uppercase().as_str())
13 .print() 14 .print()
14} 15}