1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
use crate::corpus::*;
use rand::prelude::*;
use std::fmt;
pub const NUM_KEYS: usize = 30;
pub const ROW_LENGTH: usize = 10;
pub const KEY_CHARS: [char; NUM_KEYS] = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '/', '.', ',', '\'',
];
pub fn canonicalise(inp: char) -> Option<char> {
if inp.is_ascii_alphabetic() {
return Some(inp.to_ascii_uppercase());
} else {
match inp {
'.' => Some('.'),
'>' => Some('.'),
',' => Some(','),
'<' => Some(','),
'/' => Some('/'),
'?' => Some('/'),
'\'' => Some('\''),
'"' => Some('\''),
_ => None,
}
}
}
#[derive(Copy, Clone)]
pub struct Prelayout {
standard_columns: [[char; 3]; 6],
index_columns: [[char; 6]; 2],
}
impl Prelayout {
pub fn get_standard_column(&self, index: usize) -> &[char; 3] {
&self.standard_columns[index]
}
pub fn get_index_column(&self, index: usize) -> &[char; 6] {
&self.index_columns[index]
}
pub fn new_random_from<R: RngCore>(pl: &Prelayout, rng: &mut R) -> Prelayout {
let mut standard_columns = pl.standard_columns.clone();
let mut index_columns = pl.index_columns.clone();
let mut count = rng.gen_range(1..NUM_KEYS);
while count > 0 {
let source_index: bool = rng.gen();
let target_index: bool = rng.gen();
let saved;
let target_col: usize;
let target_idx: usize;
if target_index {
target_col = rng.gen_range(0..2);
target_idx = rng.gen_range(0..6);
saved = index_columns[target_col][target_idx];
} else {
target_col = rng.gen_range(0..6);
target_idx = rng.gen_range(0..3);
saved = standard_columns[target_col][target_idx];
}
let source_col: usize;
let source_idx: usize;
if source_index {
source_col = rng.gen_range(0..2);
source_idx = rng.gen_range(0..6);
if target_index {
index_columns[target_col][target_idx] = index_columns[source_col][source_idx];
} else {
standard_columns[target_col][target_idx] =
index_columns[source_col][source_idx];
}
index_columns[source_col][source_idx] = saved;
} else {
source_col = rng.gen_range(0..6);
source_idx = rng.gen_range(0..3);
if target_index {
index_columns[target_col][target_idx] =
standard_columns[source_col][source_idx];
} else {
standard_columns[target_col][target_idx] =
standard_columns[source_col][source_idx];
}
standard_columns[source_col][source_idx] = saved;
}
count -= 1;
}
Prelayout {
standard_columns,
index_columns,
}
}
fn from_char_array(ca: &[char; NUM_KEYS]) -> Prelayout {
let mut standard_columns = [['x'; 3]; 6];
let mut index_columns = [['x'; 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];
}
}
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];
}
Prelayout {
standard_columns,
index_columns,
}
}
}
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,
];
#[derive(Copy, Clone)]
pub struct Layout {
keys: [char; NUM_KEYS],
}
impl Layout {
pub fn as_prelayout(&self) -> Prelayout {
Prelayout::from_char_array(&self.keys)
}
pub fn from_prelayout(pl: &Prelayout, corpus: &Corpus) -> Layout {
fn weight_function<const N: usize, const M: usize>(
columns: &[[char; N]; M],
corpus: &Corpus,
) -> Vec<(Vec<char>, u32)> {
let mut result = columns
.iter()
.map(|col| {
let mut weight = 0;
let mut wcol: Vec<(char, u32)> = col
.iter()
.map(|&c| {
let w = corpus.get_character_count(c);
weight += w;
(c, w)
})
.collect();
wcol.sort_by(|(_, l), (_, r)| r.cmp(l));
wcol.swap(0, 1);
if N == 6 {
wcol.swap(3, 4);
}
(wcol.into_iter().map(|(k, _)| k).collect(), weight)
})
.collect::<Vec<(Vec<char>, u32)>>();
result.sort_by(|(_, l), (_, r)| r.cmp(l));
result
}
let mut balance: i64 = 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 ind = if left { left_col } else { right_col };
for i in 0..3 {
keys[ind + i * ROW_LENGTH] = col[i];
}
if left {
left_col += 1;
} else {
right_col -= 1;
}
balance += if left {
-(weight as i64)
} else {
weight as i64
};
}
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];
}
Layout { keys }
}
pub fn get_index(&self, c: char) -> usize {
let mut found_key = 0;
while self.keys[found_key] != c {
found_key += 1
}
found_key
}
fn char_array_to_layout(keys: [char; NUM_KEYS]) -> Layout {
return Layout { keys };
}
pub fn get_key(&self, index: usize) -> char {
self.keys[index]
}
pub fn from_verbose(inp: &str) -> Option<Layout> {
let mut layout: [char; NUM_KEYS] = ['x'; NUM_KEYS];
let mut k: usize = 0;
for c in inp.chars() {
let valid = (c.is_uppercase() && c.is_alphabetic())
|| c == '.'
|| c == '/'
|| c == ','
|| c == '\'';
if valid {
layout[k] = c;
k += 1;
}
if k > NUM_KEYS {
return None;
}
}
return Some(Layout::char_array_to_layout(layout));
}
}
impl fmt::Display for Layout {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&format_block_output(self.keys.into_iter()))
}
}
#[derive(Copy, Clone)]
pub struct Evaluation {
keypress: [u32; NUM_KEYS],
total_keypress: u32,
sfb: [u32; 8],
}
impl Evaluation {
pub fn new(keypress: [u32; NUM_KEYS], total_keypress: u32, sfb: [u32; 8]) -> Evaluation {
Evaluation {
keypress,
sfb,
total_keypress,
}
}
fn output_eval(&self) -> String {
let mut result = String::new();
let tot = self.total_keypress as f32;
result += &"Percent per key:\n";
result += &format_block_output(self.keypress.into_iter().map(|k| 100.0 * k as f32 / tot));
let mut finger_usages = [0; 8];
for (i, &c) in self.keypress.iter().enumerate() {
finger_usages[KEY_TO_FINGER[i]] += c;
}
result += "\nFinger usage: ";
let mut lh: f32 = 0.0;
let mut rh: f32 = 0.0;
for (i, &u) in finger_usages.iter().enumerate() {
let f = u as f32 / tot * 100.0;
result += &format!("{:>5.2}%{}", f, if i < 7 { ", " } else { "" });
if i % 10 < 4 {
lh += f;
} else {
rh += f;
}
}
result += &format!("\nHand usage: {:.2}% vs {:.2}%", lh, rh);
result += "\nSame finger bigrams: ";
for (i, &u) in self.sfb.iter().enumerate() {
result += &format!(
"{:>6.3}%{}",
u as f32 / tot * 100.0,
if i < 7 { ", " } else { "" }
);
}
let sfb = self.sfb.iter().sum::<u32>();
result += &format!("\nTotal sfb: {:.2}% ({})", sfb as f32 / tot * 100.0, sfb);
return result;
}
}
impl fmt::Display for Evaluation {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&self.output_eval())
}
}
// This is a silly amount of work to genericise the below...
trait ToMyString {
const BLANK_STRING: &'static str;
fn to_my_string(&self) -> String;
}
impl ToMyString for char {
const BLANK_STRING: &'static str = " ";
fn to_my_string(&self) -> String {
self.to_string()
}
}
impl ToMyString for u32 {
const BLANK_STRING: &'static str = " ";
fn to_my_string(&self) -> String {
format!("{:5}", *self)
}
}
impl ToMyString for f32 {
const BLANK_STRING: &'static str = " ";
fn to_my_string(&self) -> String {
format!("{:>4.1}", *self)
}
}
fn format_block_output<T: Iterator<Item = S>, S: ToMyString>(things: T) -> String {
let mut result = String::new();
for (i, t) in things.enumerate() {
result += &t.to_my_string();
if i < NUM_KEYS - 1 {
result.push(' ');
}
if (i + 1) % 10 == 0 {
result.push('\n');
if NUM_KEYS < 30 && i == 19 {
result += S::BLANK_STRING;
result.push(' ')
}
} else if (i < 20 && (i + 1) % 5 == 0) || (i == (NUM_KEYS - 20) / 2 + 19) {
result.push(' ');
}
if i + 1 >= NUM_KEYS {
break;
}
}
return result;
}
|