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
use crate::fast_index_data::*;
use std::iter::Iterator;
#[derive(Debug)]
pub struct FastIndexIter {
incr: usize,
idx_pos: &'static [u8],
}
impl Iterator for FastIndexIter {
type Item = usize;
fn next(&mut self) -> Option<usize> {
if self.incr >= self.idx_pos.len() {
None
} else {
let res = Some(self.idx_pos[self.incr] as usize);
self.incr += 1;
res
}
}
}
pub fn row_iter(idx: usize) -> FastIndexIter {
let start = idx * 9;
let end = start + 9;
FastIndexIter {
incr: 0,
idx_pos: &ROW_DATA[start..end],
}
}
pub fn column_iter(idx: usize) -> FastIndexIter {
let start = idx * 9;
let end = start + 9;
FastIndexIter {
incr: 0,
idx_pos: &COLUMN_DATA[start..end],
}
}
pub fn box_iter(idx: usize) -> FastIndexIter {
let start = idx * 9;
let end = start + 9;
FastIndexIter {
incr: 0,
idx_pos: &BOX_DATA[start..end],
}
}
#[derive(Debug)]
pub struct CombinationIterator {
num_idx: usize,
idx: [usize; 4],
values: &'static [u8],
}
impl CombinationIterator {
pub fn new(num_idx: usize, values: &'static [u8]) -> Self {
assert!(num_idx <= 4);
assert!(num_idx >= 2);
let mut idx = [0; 4];
for (i, idx_item) in idx.iter_mut().enumerate().take(num_idx) {
*idx_item = i;
}
idx[num_idx - 1] -= 1;
CombinationIterator {
num_idx,
idx,
values,
}
}
}
impl Iterator for CombinationIterator {
type Item = Vec<usize>;
fn next(&mut self) -> Option<Vec<usize>> {
let mut current_level: usize = self.num_idx - 1;
while current_level < 4 {
self.idx[current_level] += 1;
let needed_after = self.num_idx as i32 - (current_level + 1) as i32;
if self.idx[current_level] as i32 + needed_after >= 9 {
if current_level == 0 {
return None;
}
current_level -= 1;
} else {
if current_level < self.num_idx - 1 {
self.idx[current_level + 1] = self.idx[current_level];
}
current_level += 1;
}
}
Some(
self.idx[0..self.num_idx]
.iter()
.cloned()
.map(|i| self.values[i] as usize)
.collect(),
)
}
}
pub fn row_comb_iter(idx: usize, sz: usize) -> CombinationIterator {
let start = idx * 9;
let end = start + 9;
CombinationIterator::new(sz, &ROW_DATA[start..end])
}
pub fn column_comb_iter(idx: usize, sz: usize) -> CombinationIterator {
let start = idx * 9;
let end = start + 9;
CombinationIterator::new(sz, &COLUMN_DATA[start..end])
}
pub fn box_comb_iter(idx: usize, sz: usize) -> CombinationIterator {
let start = idx * 9;
let end = start + 9;
CombinationIterator::new(sz, &BOX_DATA[start..end])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::slow_index;
#[test]
fn test_iter() {
for row in 0..9 {
let i = row_iter(row).zip(slow_index::row_iter(row));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
for col in 0..9 {
let i = column_iter(col).zip(slow_index::column_iter(col));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
for box_i in 0..9 {
let i = box_iter(box_i).zip(slow_index::box_iter(box_i));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
}
#[test]
fn test_comb_iter() {
for sz in 2..5 {
for row in 0..9 {
let i = row_comb_iter(row, sz).zip(slow_index::row_comb_iter(row, sz));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
for col in 0..9 {
let i = column_comb_iter(col, sz).zip(slow_index::column_comb_iter(col, sz));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
for box_i in 0..9 {
let i = box_comb_iter(box_i, sz).zip(slow_index::box_comb_iter(box_i, sz));
for (fast, slow) in i {
assert_eq!(fast, slow);
}
}
}
}
}