From dbd4a9b5c8367d5e992338fd87a2b0b13f50917e Mon Sep 17 00:00:00 2001 From: David Wang Date: Sun, 19 Feb 2017 08:05:44 +1100 Subject: [PATCH] Add puzzle variable tokens. We have chosen to return a variable token for every variable created, instead of referencing puzzle variables by string or something else. Puzzle variables should only be used in the puzzle that created it, though this is neither checked nor enforced. Eventually, we would also like to overload the standard arithmetic symbols to ergonomically build constraint expressions. --- src/lib.rs | 4 ++++ src/puzzle.rs | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a7f7370..b98c8df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,8 @@ extern crate bit_set; pub use puzzle::Puzzle; +/// A puzzle variable token. +#[derive(Copy,Clone,Debug,Eq,PartialEq)] +pub struct VarToken(usize); + mod puzzle; diff --git a/src/puzzle.rs b/src/puzzle.rs index 0d0881a..ab761d2 100644 --- a/src/puzzle.rs +++ b/src/puzzle.rs @@ -1,7 +1,8 @@ //! The puzzle's state and rules. +use ::VarToken; + /// The puzzle to be solved. -#[allow(dead_code)] pub struct Puzzle { // The number of variables in the puzzle. num_vars: usize, @@ -20,4 +21,19 @@ impl Puzzle { num_vars: 0, } } + + /// Allocate a new puzzle variable, without inserting any + /// candidates. + /// + /// # Examples + /// + /// ``` + /// let mut puzzle = puzzle_solver::Puzzle::new(); + /// puzzle.new_var(); + /// ``` + pub fn new_var(&mut self) -> VarToken { + let var = VarToken(self.num_vars); + self.num_vars = self.num_vars + 1; + var + } }