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/config.rs | 20 ++++++++++---------- srchr/src/corpus.rs | 14 +++++++------- srchr/src/evaluation.rs | 2 +- srchr/src/layout.rs | 46 +++++++++++++++++++++++----------------------- 4 files changed, 41 insertions(+), 41 deletions(-) (limited to 'srchr/src') diff --git a/srchr/src/config.rs b/srchr/src/config.rs index 6409ef7..c67a888 100644 --- a/srchr/src/config.rs +++ b/srchr/src/config.rs @@ -44,19 +44,19 @@ pub const KEY_TO_FINGER: [usize; NUM_KEYS] = [ 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, ]; -pub fn canonicalise(inp: char) -> Option { +pub fn canonicalise(inp: char) -> Option { if inp.is_ascii_alphabetic() { - return Some(inp.to_ascii_uppercase()); + return Some(inp.to_ascii_uppercase() as u8); } else { match inp { - '.' => Some('.'), - '>' => Some('.'), - ',' => Some(','), - '<' => Some(','), - '/' => Some('/'), - '?' => Some('/'), - '\'' => Some('\''), - '"' => Some('\''), + '.' => Some('.' as u8), + '>' => Some('.' as u8), + ',' => Some(',' as u8), + '<' => Some(',' as u8), + '/' => Some('/' as u8), + '?' => Some('/' as u8), + '\'' => Some('\'' as u8), + '"' => Some('\'' as u8), _ => None, } } 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)])); } } diff --git a/srchr/src/evaluation.rs b/srchr/src/evaluation.rs index bbbe364..aba3501 100644 --- a/srchr/src/evaluation.rs +++ b/srchr/src/evaluation.rs @@ -65,7 +65,7 @@ impl Evaluation { let mut sfb: [u32; 8] = [0; 8]; for (i, &c) in layout.get_keys().iter().enumerate() { - keypress[i] = corpus.get_character_count(c); + keypress[i] = corpus.get_character_count(c as u8); } for i in 0..8 { diff --git a/srchr/src/layout.rs b/srchr/src/layout.rs index a20e130..68b0341 100644 --- a/srchr/src/layout.rs +++ b/srchr/src/layout.rs @@ -6,16 +6,16 @@ use std::fmt; #[derive(Copy, Clone)] pub struct Prelayout { - standard_columns: [[char; 3]; 6], - index_columns: [[char; 6]; 2], + standard_columns: [[u8; 3]; 6], + index_columns: [[u8; 6]; 2], } impl Prelayout { - pub fn get_standard_column(&self, index: usize) -> &[char; 3] { + pub fn get_standard_column(&self, index: usize) -> &[u8; 3] { &self.standard_columns[index] } - pub fn get_index_column(&self, index: usize) -> &[char; 6] { + pub fn get_index_column(&self, index: usize) -> &[u8; 6] { &self.index_columns[index] } @@ -74,21 +74,21 @@ impl Prelayout { } fn from_char_array(ca: &[char; NUM_KEYS]) -> Prelayout { - let mut standard_columns = [['x'; 3]; 6]; - let mut index_columns = [['x'; 6]; 2]; + let mut standard_columns = [[0; 3]; 6]; + let mut index_columns = [[0; 6]; 2]; for i in 0..6 { let ind = if i < 3 { i } else { i + 4 }; for j in 0..3 { - standard_columns[i][j] = ca[ind + j * ROW_LENGTH]; + standard_columns[i][j] = ca[ind + j * ROW_LENGTH] as u8; } } for j in 0..3 { - index_columns[0][j] = ca[3 + j * ROW_LENGTH]; - index_columns[1][j] = ca[6 + j * ROW_LENGTH]; - index_columns[0][j + 3] = ca[4 + j * ROW_LENGTH]; - index_columns[1][j + 3] = ca[5 + j * ROW_LENGTH]; + index_columns[0][j] = ca[3 + j * ROW_LENGTH] as u8; + index_columns[1][j] = ca[6 + j * ROW_LENGTH] as u8; + index_columns[0][j + 3] = ca[4 + j * ROW_LENGTH] as u8; + index_columns[1][j + 3] = ca[5 + j * ROW_LENGTH] as u8; } Prelayout { @@ -110,14 +110,14 @@ impl Layout { pub fn from_prelayout(pl: &Prelayout, corpus: &Corpus) -> Layout { fn weight_function( - columns: &[[char; N]; M], + columns: &[[u8; N]; M], corpus: &Corpus, - ) -> Vec<(Vec, u32)> { + ) -> Vec<(Vec, u32)> { let mut result = columns .iter() .map(|col| { let mut weight = 0; - let mut wcol: Vec<(char, u32)> = col + let mut wcol: Vec<(u8, u32)> = col .iter() .map(|&c| { let w = corpus.get_character_count(c); @@ -132,9 +132,9 @@ impl Layout { wcol.swap(0, 2); wcol.swap(3, 5); } - (wcol.into_iter().map(|(k, _)| k).collect(), weight) + (wcol.into_iter().map(|(k, _)| k as u8).collect(), weight) }) - .collect::, u32)>>(); + .collect::, u32)>>(); result.sort_by(|(_, l), (_, r)| r.cmp(l)); result } @@ -149,7 +149,7 @@ impl Layout { let left: bool = ((balance >= 0) && (left_col <= 2)) || (right_col <= 6); let ind = if left { left_col } else { right_col }; for i in 0..3 { - keys[ind + i * ROW_LENGTH] = col[i]; + keys[ind + i * ROW_LENGTH] = col[i] as char; } if left { left_col += 1; @@ -166,10 +166,10 @@ impl Layout { let w_index_columns = weight_function(&pl.index_columns, corpus); let (left_ind, right_ind) = if balance >= 0 { (0, 1) } else { (1, 0) }; for j in 0..3 { - keys[3 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j]; - keys[6 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j]; - keys[4 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j + 3]; - keys[5 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j + 3]; + keys[3 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j] as char; + keys[6 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j] as char; + keys[4 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j + 3] as char; + keys[5 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j + 3] as char; } Layout { keys } @@ -183,8 +183,8 @@ impl Layout { return Layout { keys }; } - pub fn get_key(&self, index: usize) -> char { - self.keys[index] + pub fn get_key(&self, index: usize) -> u8 { + self.keys[index] as u8 } pub fn from_verbose(inp: &str) -> Option { -- cgit v1.3.1