From 3ec781f4684c7d24d9c1205d6fa71c6d44318568 Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Thu, 29 Sep 2022 23:33:43 +0200 Subject: switch from char to u8, decrease char range to 128 from 256 --- srchr/src/corpus.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'srchr/src/corpus.rs') diff --git a/srchr/src/corpus.rs b/srchr/src/corpus.rs index eecb122..aa6e163 100644 --- a/srchr/src/corpus.rs +++ b/srchr/src/corpus.rs @@ -18,23 +18,23 @@ const CHAR_TO_INDEX: [usize; 128] = build_lookup_table(); const NUM_BIGRAMS: usize = NUM_KEYS * NUM_KEYS; pub struct Corpus { bigram_count: [u32; NUM_BIGRAMS], - character_count: [u32; 256], + character_count: [u32; 128], total_count: u32, index_threshold: u32, } -fn pair_to_index(x: char, y: char) -> usize { +fn pair_to_index(x: u8, y: u8) -> usize { let xi = CHAR_TO_INDEX[x as usize]; let yi = CHAR_TO_INDEX[y as usize]; xi + NUM_KEYS * yi } impl Corpus { - pub fn get_character_count(&self, c: char) -> u32 { + pub fn get_character_count(&self, c: u8) -> u32 { self.character_count[c as usize] } - pub fn get_bigram_count(&self, x: char, y: char) -> u32 { + pub fn get_bigram_count(&self, x: u8, y: u8) -> u32 { self.bigram_count[pair_to_index(x, y)] } @@ -50,10 +50,10 @@ impl Corpus { let contents = fs::read_to_string(path)?; let mut bigram_count = [0; NUM_BIGRAMS]; - let mut character_count = [0; 256]; + let mut character_count = [0; 128]; let mut total_count = 0; - let mut last_char = None; + let mut last_char: Option = None; for c in contents.chars() { if let Some(c) = canonicalise(c) { if let Some(lc) = last_char { @@ -89,7 +89,7 @@ fn dump_bigrams(corpus: &Corpus) -> Vec<(String, u32)> { for &y in &KEY_CHARS[i..] { let mut pair = String::from(x); pair.push(y); - result.push((pair, corpus.bigram_count[pair_to_index(x, y)])); + result.push((pair, corpus.bigram_count[pair_to_index(x as u8, y as u8)])); } } -- cgit v1.3.1