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
use consts::*;
use positions::FIELD;
#[derive(Debug)]
pub(crate) struct Unsolvable;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct Entry {
pub cell: u8,
pub num: u8,
}
impl Entry {
#[inline] pub fn cell(self) -> usize { self.cell as usize }
#[inline] pub fn row(self) -> u8 { self.cell / 9 }
#[inline] pub fn col(self) -> u8 { self.cell % 9 }
#[inline] pub fn field(self) -> u8 { FIELD[self.cell()] }
#[inline] pub fn num(self) -> u8 { self.num }
#[inline] pub fn mask(self) -> Mask<Digit> { Mask::from_num(self.num()) }
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct PubEntry {
pub cell: u8,
pub ch: char,
}
impl PubEntry {
#[inline] pub fn row(self) -> u8 { self.cell / 9 }
#[inline] pub fn col(self) -> u8 { self.cell % 9 }
#[inline] pub fn field(self) -> u8 { FIELD[self.cell as usize] }
}
use std::fmt;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct NotEnoughRows(pub u8);
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum BlockFormatParseError {
InvalidEntry(PubEntry),
InvalidLineLength(u8),
NotEnoughRows(u8),
IncorrectFieldDelimiter,
TooManyRows,
MissingCommentDelimiter(u8),
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum LineFormatParseError {
InvalidEntry(PubEntry),
NotEnoughCells(u8),
TooManyCells,
MissingCommentDelimiter,
}
impl fmt::Display for LineFormatParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::LineFormatParseError::*;
match *self {
InvalidEntry(PubEntry { cell, ch }) => write!(f, "cell {} contains invalid character '{}'", cell, ch),
NotEnoughCells(cells) => write!(f, "sudoku contains {} cells instead of required 81", cells),
TooManyCells => write!(f, "sudoku contains more than 81 cells or is missing comment delimiter"),
MissingCommentDelimiter => write!(f, "missing comment delimiter"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) struct Position;
#[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) struct Digit;
#[derive(Clone, Copy, Eq, Debug)]
pub(crate) struct Mask<T>(u16, ::std::marker::PhantomData<T>);
#[derive(Clone, PartialEq, Eq, Debug)]
pub(crate) struct MaskIter<T>(Mask<T>);
impl<T> Mask<T> {
pub const ALL: Mask<T> = Mask(0b_0001_1111_1111, ::std::marker::PhantomData);
pub const NONE: Mask<T> = Mask(0, ::std::marker::PhantomData);
#[inline(always)]
fn new(mask: u16) -> Self {
Mask(mask, ::std::marker::PhantomData)
}
#[inline(always)]
pub fn n_possibilities(self) -> u8 {
self.0.count_ones() as u8
}
#[inline(always)]
pub fn is_empty(self) -> bool {
self == Mask::NONE
}
#[inline(always)]
pub fn without(self, other: Self) -> Self {
Mask::new(self.0 & !other.0)
}
#[inline(always)]
pub fn remove(&mut self, other: Self) {
self.0 &= !other.0;
}
#[inline(always)]
pub fn iter(self) -> MaskIter<T> {
MaskIter(self)
}
}
impl<T> PartialEq<Mask<T>> for Mask<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Mask<Digit> {
#[inline(always)]
pub fn from_num(num: u8) -> Self {
debug_assert!(num > 0);
debug_assert!(num <= 9);
Mask::new(1 << (num - 1))
}
#[inline(always)]
pub fn unique_num(self) -> Result<Option<u8>, Unsolvable> {
match self.0 {
0 => Err(Unsolvable),
n if n.is_power_of_two() => Ok(Some(n.trailing_zeros() as u8+1)),
_ => Ok(None),
}
}
#[inline(always)]
pub fn one_possibility(self) -> u8 {
debug_assert!(self.0 != 0);
16 - self.0.leading_zeros() as u8
}
}
impl<T> MaskIter<T> {
#[allow(unused)]
pub fn to_mask(self) -> Mask<T> {
self.0
}
}
impl Iterator for MaskIter<Digit> {
type Item = u8;
fn next(&mut self) -> Option<u8> {
match (self.0).0 {
0 => None,
_ => {
let num = self.0.one_possibility();
self.0.remove(Mask::from_num(num));
Some(num)
},
}
}
}
impl<T> ::std::ops::Not for Mask<T> {
type Output = Self;
#[inline(always)]
fn not(self) -> Self {
Mask::new(!self.0 & 0b_0001_1111_1111)
}
}
macro_rules! impl_bitops {
($trait_:path, $fn_name:ident ) => {
impl<T> $trait_ for Mask<T> {
type Output = Self;
#[inline(always)]
fn $fn_name(self, rhs: Self) -> Self {
Mask::new(u16::$fn_name(self.0, rhs.0))
}
}
}
}
impl_bitops!(::std::ops::BitAnd, bitand);
impl_bitops!(::std::ops::BitOr, bitor);
impl_bitops!(::std::ops::BitXor, bitxor);
macro_rules! impl_bitops_assign {
($trait_:path, $fn_name:ident ) => {
impl<T> $trait_ for Mask<T> {
fn $fn_name(&mut self, rhs: Self) {
u16::$fn_name(&mut self.0, rhs.0)
}
}
}
}
impl_bitops_assign!(::std::ops::BitAndAssign, bitand_assign);
impl_bitops_assign!(::std::ops::BitOrAssign, bitor_assign);
impl_bitops_assign!(::std::ops::BitXorAssign, bitxor_assign);
#[derive(Copy, Clone)]
pub(crate) struct Array81<T>(pub [T; N_CELLS]);
impl<T: fmt::Debug> fmt::Debug for Array81<T> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
(&self.0[..]).fmt(f)
}
}
impl<T> ::std::ops::Deref for Array81<T> {
type Target = [T; 81];
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> ::std::ops::DerefMut for Array81<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}