mecab_ko_dict/trie/
mmap.rs1use std::path::Path;
4
5use yada::DoubleArray;
6
7use crate::error::{DictError, Result};
8
9pub struct MmapTrie {
13 pub(super) da: DoubleArray<memmap2::Mmap>,
14}
15
16impl MmapTrie {
17 #[allow(unsafe_code)]
23 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
24 let file = std::fs::File::open(path.as_ref()).map_err(DictError::Io)?;
25 let mmap = unsafe { memmap2::Mmap::map(&file).map_err(DictError::Io)? };
28 Ok(Self {
29 da: DoubleArray::new(mmap),
30 })
31 }
32
33 #[must_use]
35 pub fn exact_match(&self, key: &str) -> Option<u32> {
36 self.da.exact_match_search(key.as_bytes())
37 }
38
39 #[must_use]
41 pub fn exact_match_bytes(&self, key: &[u8]) -> Option<u32> {
42 self.da.exact_match_search(key)
43 }
44
45 pub fn common_prefix_search<'a>(
47 &'a self,
48 text: &'a str,
49 ) -> impl Iterator<Item = (u32, usize)> + 'a {
50 self.da.common_prefix_search(text.as_bytes())
51 }
52
53 pub fn common_prefix_search_bytes<'a>(
55 &'a self,
56 key: &'a [u8],
57 ) -> impl Iterator<Item = (u32, usize)> + 'a {
58 self.da.common_prefix_search(key)
59 }
60
61 #[must_use]
63 pub fn common_prefix_search_at(
64 &self,
65 text: &str,
66 start_byte: usize,
67 ) -> super::backend::PrefixSearchResult {
68 if start_byte >= text.len() {
69 return super::backend::PrefixSearchResult::new();
70 }
71 let suffix = &text[start_byte..];
72 self.da
73 .common_prefix_search(suffix.as_bytes())
74 .map(|(value, len)| (value, start_byte + len))
75 .collect()
76 }
77}