diff options
| author | tslil clingman <tslil@posteo.de> | 2022-10-20 23:17:19 +0200 |
|---|---|---|
| committer | tslil clingman <tslil@posteo.de> | 2022-10-20 23:20:12 +0200 |
| commit | 1fa7552c3018d541b617b5ceebc6abeaba013eaa (patch) | |
| tree | 7cdc73a4057ff395694c6a72817167d02280efce /srchr/src/layout.rs | |
| parent | aa55ddbbf13ea8b2a259ad7a07490efe9dda57f5 (diff) | |
penalise pinky columns with too high usage
even though we don't technically know which columns they are, we do
know they will be the two lowest ultimately when the prelayout becomes
a layout
Diffstat (limited to 'srchr/src/layout.rs')
| -rw-r--r-- | srchr/src/layout.rs | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/srchr/src/layout.rs b/srchr/src/layout.rs index 81cfd9b..8f8b03a 100644 --- a/srchr/src/layout.rs +++ b/srchr/src/layout.rs @@ -109,23 +109,30 @@ impl Layout { } pub fn from_prelayout(pl: &Prelayout, corpus: &Corpus) -> Layout { + fn my_f32_compare(r: f32, l: f32) -> std::cmp::Ordering { + if r < l { + std::cmp::Ordering::Less + } else { + std::cmp::Ordering::Greater + } + } fn weight_function<const N: usize, const M: usize>( columns: &[[u8; N]; M], corpus: &Corpus, - ) -> Vec<(Vec<u8>, u32)> { + ) -> Vec<(Vec<u8>, f32)> { let mut result = columns .iter() .map(|col| { - let mut weight = 0; - let mut wcol: Vec<(u8, u32)> = col + let mut weight = 0.0; + let mut wcol: Vec<(u8, f32)> = col .iter() .map(|&c| { - let w = corpus.get_character_count(c); + let w = corpus.get_character_perc(c); weight += w; (c, w) }) .collect(); - wcol.sort_by(|(_, l), (_, r)| r.cmp(l)); + wcol.sort_by(|(_, l), (_, r)| my_f32_compare(*r, *l)); wcol.swap(0, 1); if N == 6 { wcol.swap(0, 2); @@ -134,19 +141,19 @@ impl Layout { } (wcol.into_iter().map(|(k, _)| k as u8).collect(), weight) }) - .collect::<Vec<(Vec<u8>, u32)>>(); - result.sort_by(|(_, l), (_, r)| r.cmp(l)); + .collect::<Vec<(Vec<u8>, f32)>>(); + result.sort_by(|(_, l), (_, r)| my_f32_compare(*r, *l)); result } - let mut balance: i64 = 0; + let mut balance: f32 = 0.0; let mut left_col: usize = 0; let mut right_col: usize = 9; let mut keys = ['x'; NUM_KEYS]; let mut w_standard_columns = weight_function(&pl.standard_columns, corpus); while let Some((col, weight)) = w_standard_columns.pop() { - let left: bool = ((balance >= 0) && (left_col <= 2)) || (right_col <= 6); + let left: bool = ((balance >= 0.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] as char; @@ -156,15 +163,11 @@ impl Layout { } else { right_col -= 1; } - balance += if left { - -(weight as i64) - } else { - weight as i64 - }; + balance += if left { -weight } else { weight }; } let w_index_columns = weight_function(&pl.index_columns, corpus); - let (left_ind, right_ind) = if balance >= 0 { (0, 1) } else { (1, 0) }; + let (left_ind, right_ind) = if balance >= 0.0 { (0, 1) } else { (1, 0) }; for j in 0..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; |
