Add types for a single candidate and a set of candidates.

Candidates are always an i32 (rather than a generic integer type) to
more easily perform arithmetic.  The puzzles we will deal with should
always be small enough such that we do not exceed the limits of i32.

Candidate groups are separated into an enum.  This is to help us keep
it general, as we would like to include a range option in the future.
This commit is contained in:
David Wang 2017-02-19 08:16:19 +11:00
parent dbd4a9b5c8
commit 33ed1a3dbb
2 changed files with 21 additions and 1 deletions

View File

@ -9,4 +9,7 @@ pub use puzzle::Puzzle;
#[derive(Copy,Clone,Debug,Eq,PartialEq)]
pub struct VarToken(usize);
/// The type of a puzzle variable's value (i.e. the candidate type).
pub type Val = i32;
mod puzzle;

View File

@ -1,11 +1,26 @@
//! The puzzle's state and rules.
use ::VarToken;
use std::collections::BTreeSet;
use std::rc::Rc;
use ::{Val,VarToken};
/// A collection of candidates.
#[derive(Clone,Debug,Eq,PartialEq)]
#[allow(dead_code)]
enum Candidates {
None, // A variable with no candidates.
Value(Val), // A variable set to its initial value.
Set(Rc<BTreeSet<Val>>), // A variable with a list of candidates.
}
/// The puzzle to be solved.
pub struct Puzzle {
// The number of variables in the puzzle.
num_vars: usize,
// The list of candidates for each variable.
candidates: Vec<Candidates>,
}
impl Puzzle {
@ -19,6 +34,7 @@ impl Puzzle {
pub fn new() -> Self {
Puzzle {
num_vars: 0,
candidates: Vec::new(),
}
}
@ -34,6 +50,7 @@ impl Puzzle {
pub fn new_var(&mut self) -> VarToken {
let var = VarToken(self.num_vars);
self.num_vars = self.num_vars + 1;
self.candidates.push(Candidates::None);
var
}
}