diff options
author | Akshay <[email protected]> | 2023-04-27 17:32:07 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2023-04-27 17:32:07 +0100 |
commit | 2e57b250c6e931c13ed9ec1870e133c59151422b (patch) | |
tree | 3642b89cd7fadd9dad0dc63438160ecb6fcdbf6d | |
parent | 7f9f70d0c7f93f11fa896e5544a5da59319a56f9 (diff) |
-rw-r--r-- | Cargo.lock | 32 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/lib.rs | 70 | ||||
-rw-r--r-- | src/main.rs | 1 |
4 files changed, 40 insertions, 65 deletions
@@ -6,36 +6,36 @@ version = 3 | |||
6 | name = "dict" | 6 | name = "dict" |
7 | version = "0.1.0" | 7 | version = "0.1.0" |
8 | dependencies = [ | 8 | dependencies = [ |
9 | "radix_trie", | 9 | "qp-trie", |
10 | ] | 10 | ] |
11 | 11 | ||
12 | [[package]] | 12 | [[package]] |
13 | name = "endian-type" | 13 | name = "new_debug_unreachable" |
14 | version = "0.1.2" | 14 | version = "1.0.4" |
15 | source = "registry+https://github.com/rust-lang/crates.io-index" | 15 | source = "registry+https://github.com/rust-lang/crates.io-index" |
16 | checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" | 16 | checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" |
17 | 17 | ||
18 | [[package]] | 18 | [[package]] |
19 | name = "nibble_vec" | 19 | name = "qp-trie" |
20 | version = "0.1.0" | 20 | version = "0.8.1" |
21 | source = "registry+https://github.com/rust-lang/crates.io-index" | 21 | source = "registry+https://github.com/rust-lang/crates.io-index" |
22 | checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" | 22 | checksum = "9569328cda9b68120dbbf855bac541eeb40c475d96a9a380cf8b5547bfe0c165" |
23 | dependencies = [ | 23 | dependencies = [ |
24 | "smallvec", | 24 | "new_debug_unreachable", |
25 | "unreachable", | ||
25 | ] | 26 | ] |
26 | 27 | ||
27 | [[package]] | 28 | [[package]] |
28 | name = "radix_trie" | 29 | name = "unreachable" |
29 | version = "0.2.1" | 30 | version = "1.0.0" |
30 | source = "registry+https://github.com/rust-lang/crates.io-index" | 31 | source = "registry+https://github.com/rust-lang/crates.io-index" |
31 | checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" | 32 | checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" |
32 | dependencies = [ | 33 | dependencies = [ |
33 | "endian-type", | 34 | "void", |
34 | "nibble_vec", | ||
35 | ] | 35 | ] |
36 | 36 | ||
37 | [[package]] | 37 | [[package]] |
38 | name = "smallvec" | 38 | name = "void" |
39 | version = "1.10.0" | 39 | version = "1.0.2" |
40 | source = "registry+https://github.com/rust-lang/crates.io-index" | 40 | source = "registry+https://github.com/rust-lang/crates.io-index" |
41 | checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" | 41 | checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" |
@@ -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] |
9 | radix_trie = "0.2.1" | 9 | qp-trie = "0.8.1" |
@@ -3,9 +3,10 @@ pub mod lex; | |||
3 | pub mod parse; | 3 | pub mod parse; |
4 | mod utils; | 4 | mod utils; |
5 | 5 | ||
6 | use std::fmt; | 6 | use qp_trie::{ |
7 | 7 | wrapper::{BStr, BString}, | |
8 | use radix_trie::{Trie, TrieCommon}; | 8 | Iter, Trie, |
9 | }; | ||
9 | 10 | ||
10 | pub struct Dict { | 11 | pub 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 | ||
44 | pub enum SearchResults<'dict, 'search> { | 33 | pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>); |
45 | Empty, | ||
46 | Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>), | ||
47 | } | ||
48 | 34 | ||
49 | impl<'dict, 'search> SearchResults<'dict, 'search> { | 35 | impl<'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 | ||
76 | type DictKey = &'static str; | 50 | type DictKey = BString; |
77 | 51 | ||
78 | #[derive(Clone)] | 52 | #[derive(Clone, Debug, Default)] |
79 | pub struct DictValue { | 53 | pub 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 | } |