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
use core::{
cmp::Ordering,
fmt::{self, Debug},
};
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Hash, Ord, Debug)]
pub enum RegisterGroup {
Accum = 0,
Code = 1,
Data = 2,
}
const REGISTER_GROUPS: &[RegisterGroup] = &[
RegisterGroup::Accum,
RegisterGroup::Code,
RegisterGroup::Data,
];
#[derive(Debug)]
pub struct TapData {
pub offset: u16,
pub back: u16,
pub group: RegisterGroup,
pub combo: u8,
pub skip: u8,
}
impl PartialEq for TapData {
fn eq(&self, other: &Self) -> bool {
self.offset == other.offset && self.back == other.back && self.group == other.group
}
}
impl PartialOrd for TapData {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.group.partial_cmp(&other.group) {
Some(Ordering::Equal) => {}
ord => return ord,
}
match self.offset.partial_cmp(&other.offset) {
Some(Ordering::Equal) => {}
ord => return ord,
}
self.back.partial_cmp(&other.back)
}
}
#[derive(Debug)]
pub struct TapSet<'a> {
pub taps: &'a [TapData],
pub combo_taps: &'a [u16],
pub combo_begin: &'a [u16],
pub group_begin: [usize; REGISTER_GROUPS.len() + 1],
pub combos_count: usize,
pub reg_count: usize,
pub tot_combo_backs: usize,
}
impl<'a> TapSet<'a> {
pub fn tap_size(&self) -> usize {
self.group_begin[REGISTER_GROUPS.len()]
}
pub fn taps(&self) -> TapIter {
TapIter {
data: &self.taps,
cursor: 0,
end: self.group_begin[REGISTER_GROUPS.len()],
}
}
pub fn regs(&self) -> RegisterIter {
RegisterIter {
data: &self.taps,
cursor: 0,
end: self.group_begin[REGISTER_GROUPS.len()],
}
}
pub fn group_taps(&self, group: RegisterGroup) -> TapIter {
let group_id = group as usize;
TapIter {
data: &self.taps,
cursor: self.group_begin[group_id],
end: self.group_begin[group_id + 1],
}
}
pub fn group_regs(&self, group: RegisterGroup) -> RegisterIter {
let group_id = group as usize;
RegisterIter {
data: &self.taps,
cursor: self.group_begin[group_id],
end: self.group_begin[group_id + 1],
}
}
pub fn group_size(&self, group: RegisterGroup) -> usize {
let group_id = group as usize;
let idx = self.group_begin[group_id + 1] - 1;
let last = self.taps[idx].offset as usize;
last + 1
}
pub fn combos_size(&self) -> usize {
self.combos_count
}
pub fn reg_count(&self) -> usize {
self.reg_count
}
pub fn combos(&self) -> ComboIter {
ComboIter {
data: ComboData {
taps: &self.combo_taps,
offsets: &self.combo_begin,
},
id: 0,
end: self.combos_count,
}
}
pub fn get_combo(&self, id: usize) -> ComboRef {
ComboRef {
data: ComboData {
taps: &self.combo_taps,
offsets: &self.combo_begin,
},
id,
}
}
}
pub struct RegisterRef<'a> {
data: &'a [TapData],
cursor: usize,
}
impl<'a> RegisterRef<'a> {
pub fn group(&self) -> RegisterGroup {
self.data[self.cursor].group
}
pub fn offset(&self) -> usize {
self.data[self.cursor].offset as usize
}
pub fn combo_id(&self) -> usize {
self.data[self.cursor].combo as usize
}
pub fn size(&self) -> usize {
self.data[self.cursor].skip as usize
}
pub fn back(&self, i: usize) -> usize {
self.data[self.cursor + i].back as usize
}
}
impl<'a> Debug for RegisterRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("RegisterRef")
.field("group", &self.group())
.field("offset", &self.offset())
.field("combo_id", &self.combo_id())
.field("size", &self.size())
.finish()
}
}
impl<'a> IntoIterator for RegisterRef<'a> {
type Item = TapRef<'a>;
type IntoIter = TapIter<'a>;
fn into_iter(self) -> Self::IntoIter {
TapIter {
data: self.data,
cursor: self.cursor,
end: self.cursor + self.size(),
}
}
}
pub struct RegisterIter<'a> {
data: &'a [TapData],
cursor: usize,
end: usize,
}
impl<'a> Iterator for RegisterIter<'a> {
type Item = RegisterRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
let cursor = self.cursor;
if cursor >= self.data.len() {
return None;
}
let next = cursor + self.data[cursor].skip as usize;
if next > self.end {
return None;
}
self.cursor = next;
Some(RegisterRef {
data: self.data,
cursor,
})
}
}
pub struct TapRef<'a> {
data: &'a TapData,
}
impl<'a> TapRef<'a> {
pub fn group(&self) -> RegisterGroup {
self.data.group
}
pub fn offset(&self) -> usize {
self.data.offset as usize
}
pub fn back(&self) -> usize {
self.data.back as usize
}
pub fn combo_id(&self) -> usize {
self.data.combo as usize
}
}
impl<'a> Debug for TapRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("TapRef")
.field("group", &self.group())
.field("offset", &self.offset())
.field("back", &self.back())
.field("conmbo_id", &self.combo_id())
.finish()
}
}
pub struct TapIter<'a> {
data: &'a [TapData],
cursor: usize,
end: usize,
}
impl<'a> Iterator for TapIter<'a> {
type Item = TapRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
let cursor = self.cursor;
let next = self.cursor + 1;
if next > self.end {
return None;
}
self.cursor = next;
Some(TapRef {
data: &self.data[cursor],
})
}
}
#[derive(Clone)]
pub struct ComboData<'a> {
taps: &'a [u16],
offsets: &'a [u16],
}
pub struct ComboRef<'a> {
data: ComboData<'a>,
id: usize,
}
impl<'a> ComboRef<'a> {
pub fn id(&self) -> usize {
self.id
}
pub fn size(&self) -> usize {
self.next_offset() - self.self_offset()
}
pub fn slice(&self) -> &[u16] {
&self.data.taps[self.self_offset()..self.next_offset()]
}
fn self_offset(&self) -> usize {
self.data.offsets[self.id] as usize
}
fn next_offset(&self) -> usize {
self.data.offsets[self.id + 1] as usize
}
}
pub struct ComboIter<'a> {
data: ComboData<'a>,
id: usize,
end: usize,
}
impl<'a> Iterator for ComboIter<'a> {
type Item = ComboRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
let id = self.id;
let next = self.id + 1;
if next > self.end {
return None;
}
self.id = next;
Some(ComboRef {
data: self.data.clone(),
id,
})
}
}