From 33ed1a3dbbcf78dd5b0b56cb63f9f14ba98ef5c8 Mon Sep 17 00:00:00 2001 From: David Wang Date: Sun, 19 Feb 2017 08:16:19 +1100 Subject: [PATCH] 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. --- src/lib.rs | 3 +++ src/puzzle.rs | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b98c8df..8c0223e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/puzzle.rs b/src/puzzle.rs index ab761d2..cdd3b58 100644 --- a/src/puzzle.rs +++ b/src/puzzle.rs @@ -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>), // 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, } 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 } }