Change data structure for stroke info

Hash sets' contains method running in O(1) time complexity speeds up
kanji radical search (not counting waiting for user input) by 15%
This commit is contained in:
Hiers 2025-01-02 10:29:32 +00:00
parent 6307b3379a
commit 0d3bab226f

View file

@ -6,7 +6,7 @@ use std::{
use kradical_parsing::radk; use kradical_parsing::radk;
pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], stroke_info: &[String]) -> Option<()> { pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], stroke_info: &[HashSet<char>]) -> Option<()> {
let mut result: HashSet<_> = HashSet::new(); let mut result: HashSet<_> = HashSet::new();
let mut aux: HashSet<_> = HashSet::new(); let mut aux: HashSet<_> = HashSet::new();
@ -48,8 +48,8 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
} }
} }
/* Hash sets are unordered; Will now order the results */ /* Hash sets are unordered; Will now order the results by number of strokes */
let mut vec: Vec<Vec<String>> = Vec::with_capacity(30); /* The kanji we care about will have at most 30 strokes */ let mut vec: Vec<Vec<&String>> = Vec::with_capacity(30); /* The kanji we care about will have at most 30 strokes */
for _i in 0..29 { for _i in 0..29 {
vec.push(Vec::new()); vec.push(Vec::new());
} }
@ -61,8 +61,8 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
*/ */
for r in &result { for r in &result {
for (i, s) in stroke_info.iter().enumerate() { for (i, s) in stroke_info.iter().enumerate() {
if s.contains(r.as_str()) { if s.contains(&(r.chars().next().unwrap())) { /* r is a String that has just one character */
vec[i].push(r.to_string()); vec[i].push(r);
break; break;
} }
} }
@ -147,8 +147,9 @@ fn search_by_strokes(query: &mut String, radk_list: &[radk::Membership], n: usiz
} }
} }
pub fn get_stroke_info() -> Result<Vec<String>, std::io::Error> { pub fn get_stroke_info() -> Result<Vec<HashSet<char>>, std::io::Error> {
let file: String; let file: String;
let mut r: Vec<HashSet<char>> = Vec::with_capacity(31);
#[cfg(unix)] #[cfg(unix)]
{ {
file = fs::read_to_string("/usr/local/share/ykdt/kanji_strokes")?; file = fs::read_to_string("/usr/local/share/ykdt/kanji_strokes")?;
@ -157,6 +158,10 @@ pub fn get_stroke_info() -> Result<Vec<String>, std::io::Error> {
{ {
file = fs::read_to_string("C:\\Program Files\\ykdt\\kanji_strokes")?; file = fs::read_to_string("C:\\Program Files\\ykdt\\kanji_strokes")?;
} }
let stroke_info: Vec<String> = Vec::from_iter(file.split('\n').map(|s| s.to_owned())); for l in file.split('\n') {
Ok(stroke_info) let b: HashSet<char> = HashSet::from_iter(l.chars());
r.push(b);
}
Ok(r)
} }