diff options
| author | tslil clingman <tslil@posteo.de> | 2022-09-29 23:33:43 +0200 |
|---|---|---|
| committer | tslil clingman <tslil@posteo.de> | 2022-09-29 23:33:43 +0200 |
| commit | 3ec781f4684c7d24d9c1205d6fa71c6d44318568 (patch) | |
| tree | 40ad096b9484c02a165d21ec05b495d56f0cb55b /srchr/src/layout.rs | |
| parent | 55206dfdc7482acbd2e2b4dedf285c24e638f925 (diff) | |
switch from char to u8, decrease char range to 128 from 256
Diffstat (limited to 'srchr/src/layout.rs')
| -rw-r--r-- | srchr/src/layout.rs | 46 |
1 files changed, 23 insertions, 23 deletions
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<const N: usize, const M: usize>( - columns: &[[char; N]; M], + columns: &[[u8; N]; M], corpus: &Corpus, - ) -> Vec<(Vec<char>, u32)> { + ) -> Vec<(Vec<u8>, 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::<Vec<(Vec<char>, u32)>>(); + .collect::<Vec<(Vec<u8>, 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<Layout> { |
