-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.rs
581 lines (488 loc) · 16.3 KB
/
value.rs
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use std::fmt::{Debug, Formatter};
use std::num::{NonZeroU8, TryFromIntError};
use std::ops::Deref;
/// A classical Sudoku index, ranging 1..=9 for 9 fields.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Value(NonZeroU8);
/// Simplified type name for an optional value.
pub type ValueOption = Option<Value>;
impl Value {
pub const ONE: Value = unsafe { Value::new_unchecked(1) };
pub const TWO: Value = unsafe { Value::new_unchecked(2) };
pub const THREE: Value = unsafe { Value::new_unchecked(3) };
pub const FOUR: Value = unsafe { Value::new_unchecked(4) };
pub const FIVE: Value = unsafe { Value::new_unchecked(5) };
pub const SIX: Value = unsafe { Value::new_unchecked(6) };
pub const SEVEN: Value = unsafe { Value::new_unchecked(7) };
pub const EIGHT: Value = unsafe { Value::new_unchecked(8) };
pub const NINE: Value = unsafe { Value::new_unchecked(9) };
#[inline]
pub fn range() -> impl Iterator<Item = Value> {
(1..=9).map(|v| unsafe { Value::new_unchecked(v) })
}
pub const fn new(value: NonZeroU8) -> Self {
assert!(value.get() <= 9);
Self(value)
}
pub fn try_from(value: u8) -> Result<Value, TryFromIntError> {
assert!(value <= 9);
let value = NonZeroU8::try_from(value)?;
Ok(Self(value))
}
/// Uses [`NonZeroU8::new_unchecked`] to construct the value.
#[inline]
const unsafe fn new_unchecked(value: u8) -> Self {
debug_assert!(value > 0 && value <= 9);
Self(NonZeroU8::new_unchecked(value))
}
/// Gets the underlying [`u8`] value.
const fn get(&self) -> u8 {
self.0.get()
}
}
impl Deref for Value {
type Target = NonZeroU8;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Debug for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.get())
}
}
impl TryFrom<u8> for Value {
type Error = ValueOutOfRangeError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
if value == 0 || value > 9 {
Err(ValueOutOfRangeError(value))
} else {
Ok(Self(unsafe { NonZeroU8::new_unchecked(value) }))
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("The specified value `{0}` is out of range")]
pub struct ValueOutOfRangeError(u8);
/// A simple bitset for storing regular Sudoku-sized (i.e., up to 9) cell values.
///
/// ## Technical Notes
/// Practically this implementation allows for storing up to 65535 different indexes.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ValueBitSet {
/// We anticipate at most 9 distinct values on a standard Sudoku game.
/// We use a 16-bit type here to directly encode the field values,
/// even though this wastes 7 bits.
state: u16,
}
impl ValueBitSet {
/// The mask for storing the actual values.
const MASK: u16 = 0b111111111u16;
pub const fn empty() -> Self {
Self { state: 0 }
}
pub const fn all_values() -> Self {
Self::empty()
.with_value(Value::ONE)
.with_value(Value::TWO)
.with_value(Value::THREE)
.with_value(Value::FOUR)
.with_value(Value::FIVE)
.with_value(Value::SIX)
.with_value(Value::SEVEN)
.with_value(Value::EIGHT)
.with_value(Value::NINE)
}
#[inline]
pub const fn with_value(mut self, value: Value) -> Self {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
self.state |= (1u16 << (value - 1)) & Self::MASK;
self
}
#[inline]
pub fn insert(&mut self, value: Value) -> &mut Self {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
self.state |= (1u16 << (value - 1)) & Self::MASK;
self
}
#[inline]
pub const fn without_value(mut self, value: Value) -> Self {
debug_assert!(value.get() <= 9);
let value = value.get() as u128;
// Since the value is a non-zero u8 we subtract one for the first bit.
self.state &= (!(1u16 << (value - 1))) & Self::MASK;
self
}
#[inline]
pub fn remove(&mut self, value: Value) -> &mut Self {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
self.state &= (!(1u16 << (value - 1))) & Self::MASK;
self
}
#[inline]
pub fn remove_many(&mut self, values: ValueBitSet) -> &mut Self {
self.state &= (!values.state) & Self::MASK;
self
}
/// Sets the possible values to only the specified value.
#[inline]
pub fn set_to(&mut self, value: Value) -> &mut Self {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
self.state = (1u16 << (value - 1)) & Self::MASK;
self
}
#[inline]
pub const fn with_union(mut self, other: ValueBitSet) -> Self {
self.state |= other.state & Self::MASK;
self
}
#[inline]
pub const fn with_intersection(mut self, other: ValueBitSet) -> Self {
self.state &= other.state & Self::MASK;
self
}
#[inline]
pub fn union(&mut self, other: ValueBitSet) -> &mut Self {
self.state |= other.state & Self::MASK;
self
}
#[inline]
pub const fn contains(&self, value: Value) -> bool {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
let flag = self.state & (1u16 << (value - 1));
flag != 0
}
#[inline]
pub const fn is_exactly(&self, value: Value) -> bool {
debug_assert!(value.get() <= 9);
let value = value.get() as u16;
// Since the value is a non-zero u8 we subtract one for the first bit.
let flag = self.state & (1u16 << (value - 1));
flag == self.state
}
#[inline]
pub const fn contains_all(&self, values: ValueBitSet) -> bool {
(self.state & values.state) == values.state
}
#[inline]
pub const fn contains_some(&self, values: ValueBitSet) -> bool {
(self.state & values.state) != 0
}
#[inline]
pub const fn len(&self) -> usize {
let masked = self.state & Self::MASK;
masked.count_ones() as _
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.state & Self::MASK == 0
}
#[inline]
pub const fn iter(&self) -> ValueBitSetIter {
ValueBitSetIter {
value: *self,
index: 0,
}
}
/// Reduces this set to a single value.
///
/// ## Returns
/// Returns [`Some`] value or [`None`] if this set encodes zero or more than one value.
pub const fn as_single_value(&self) -> Option<Value> {
// Need to test here, will produce attempt to left shift with overflow otherwise.
if self.state == 0 {
return None;
}
let pow2 = self.state.trailing_zeros() as u16;
// Ensure that exactly one bit is set.
let test = (1u16 << pow2) & Self::MASK;
if self.state != test {
return None;
}
// Zero is disallowed, so we add one.
Some(unsafe { Value::new_unchecked(pow2 as u8 + 1) })
}
}
impl From<&[u8]> for ValueBitSet {
#[inline]
fn from(values: &[u8]) -> Self {
let mut state = 0u16;
for value in values {
debug_assert_ne!(*value, 0);
state |= 1 << (value - 1);
}
Self { state }
}
}
pub struct ValueBitSetIter {
value: ValueBitSet,
index: u8,
}
impl IntoIterator for ValueBitSet {
type Item = Value;
type IntoIter = ValueBitSetIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl Iterator for ValueBitSetIter {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
let state = self.value.state;
let mut index = self.index;
while index < 9 {
let test = (state >> index) & 0b1;
index += 1;
if test != 0 {
self.index = index;
return Some(unsafe { Value::new_unchecked(index) });
}
}
self.index = 10;
None
}
}
impl From<&[Value]> for ValueBitSet {
#[inline]
fn from(values: &[Value]) -> Self {
let mut state = 0u16;
for value in values {
// Since the value is a non-zero u8 we subtract one for the first bit.
state |= 1u16 << (value.get() - 1);
}
Self { state }
}
}
impl From<&[ValueOption]> for ValueBitSet {
#[inline]
fn from(values: &[ValueOption]) -> Self {
let mut state = 0u16;
for value in values {
if let Some(value) = value {
// Since the value is a non-zero u8 we subtract one for the first bit.
state |= 1u16 << (value.get() - 1);
}
}
Self { state }
}
}
impl Debug for ValueBitSet {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for (i, value) in self.iter().enumerate() {
if i > 0 {
write!(f, " ").ok();
}
write!(f, "{}", value.0.get()).ok();
}
write!(f, "")
}
}
pub trait IntoValueOptions {
fn into(self) -> [ValueOption; 81];
}
impl IntoValueOptions for [u8; 81] {
fn into(self) -> [ValueOption; 81] {
let mut values = [None; 81];
for (i, v) in self.into_iter().enumerate() {
match v {
10.. => panic!("An invalid value was specified"),
0 => values[i] = None,
x => values[i] = Some(unsafe { Value::new_unchecked(x) }),
}
}
values
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn with_value() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let bitset = ValueBitSet::default().with_value(a).with_value(b);
assert!(bitset.contains(a));
assert!(bitset.contains(b));
assert!(!bitset.contains(c));
assert_eq!(bitset.len(), 2);
assert!(!bitset.is_empty());
}
#[test]
fn set() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let mut bitset = ValueBitSet::default().with_value(a).with_value(b);
bitset.set_to(c);
assert!(!bitset.contains(a));
assert!(!bitset.contains(b));
assert!(bitset.contains(c));
assert_eq!(bitset.len(), 1);
assert!(!bitset.is_empty());
}
#[test]
fn union() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let bitset_a = ValueBitSet::default().with_value(a);
let bitset_b = ValueBitSet::default().with_value(b);
let bitset = bitset_a.with_union(bitset_b);
assert!(bitset.contains(a));
assert!(bitset.contains(b));
assert!(!bitset.contains(c));
}
#[test]
fn without_value() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let bitset = ValueBitSet::default()
.with_value(a)
.with_value(b)
.with_value(c);
let bitset = bitset.without_value(a).without_value(b);
assert!(!bitset.contains(a));
assert!(!bitset.contains(b));
assert!(bitset.contains(c));
}
#[test]
fn from_u8_slice() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let bitset = ValueBitSet::from([a, b].as_slice());
assert!(bitset.contains(a));
assert!(bitset.contains(b));
assert!(!bitset.contains(c));
}
#[test]
fn iter() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let bitset = ValueBitSet::default().with_value(a).with_value(b);
let mut iter = bitset.into_iter();
assert_eq!(iter.next(), Some(b));
assert_eq!(iter.next(), Some(a));
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn iter_test() {
let bitset = ValueBitSet { state: 16 };
let mut iter = bitset.into_iter();
assert_eq!(iter.next(), Some(Value::FIVE));
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), None);
}
#[test]
pub fn all_values() {
let set = ValueBitSet::all_values();
assert_eq!(set.len(), 9);
assert!(set.contains(Value::ONE));
assert!(set.contains(Value::TWO));
assert!(set.contains(Value::THREE));
assert!(set.contains(Value::FOUR));
assert!(set.contains(Value::FIVE));
assert!(set.contains(Value::SIX));
assert!(set.contains(Value::SEVEN));
assert!(set.contains(Value::EIGHT));
assert!(set.contains(Value::NINE));
let values: Vec<_> = set.into_iter().collect();
assert!(values.contains(&Value::ONE));
assert!(values.contains(&Value::TWO));
assert!(values.contains(&Value::THREE));
assert!(values.contains(&Value::FOUR));
assert!(values.contains(&Value::FIVE));
assert!(values.contains(&Value::SIX));
assert!(values.contains(&Value::SEVEN));
assert!(values.contains(&Value::EIGHT));
assert!(values.contains(&Value::NINE));
}
#[test]
pub fn len_works() {
let set = ValueBitSet::empty()
.with_value(Value::FIVE)
.with_value(Value::NINE);
assert_eq!(set.len(), 2);
}
#[test]
fn remove_many() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let mut bitset = ValueBitSet::default()
.with_value(a)
.with_value(b)
.with_value(c);
let remove = ValueBitSet::default().with_value(a).with_value(b);
bitset.remove_many(remove);
assert!(!bitset.contains(a));
assert!(!bitset.contains(b));
assert!(bitset.contains(c));
}
#[test]
fn is_exactly() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let mut bitset = ValueBitSet::default()
.with_value(a)
.with_value(b)
.with_value(c);
assert!(bitset.contains(c));
assert!(!bitset.is_exactly(c));
let remove = ValueBitSet::default().with_value(a).with_value(b);
bitset.remove_many(remove);
assert!(bitset.contains(c));
assert!(bitset.is_exactly(c));
}
#[test]
fn as_single_value() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let mut bitset = ValueBitSet::default()
.with_value(a)
.with_value(b)
.with_value(c);
assert!(bitset.as_single_value().is_none());
let remove = ValueBitSet::default().with_value(a).with_value(b);
bitset.remove_many(remove);
assert_eq!(bitset.as_single_value(), Some(c));
}
#[test]
fn with_intersection() {
let a = Value::try_from(9).unwrap();
let b = Value::try_from(5).unwrap();
let c = Value::try_from(2).unwrap();
let d = Value::try_from(7).unwrap();
let lhs = ValueBitSet::default()
.with_value(a)
.with_value(b)
.with_value(c);
let rhs = ValueBitSet::default()
.with_value(b)
.with_value(c)
.with_value(d);
let intersection = lhs.with_intersection(rhs);
assert!(!intersection.contains(a));
assert!(intersection.contains(b));
assert!(intersection.contains(c));
assert!(!intersection.contains(d));
}
}