diff options
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; |
