From f22b27100099de0771612812fd4b03cfd01e23d6 Mon Sep 17 00:00:00 2001 From: David Wang Date: Sat, 18 Mar 2017 09:14:35 +1100 Subject: [PATCH] Clean up. - Use cloned instead of map. - Use Val instead of i32. - Import module instead of trait. --- src/constraint/alldifferent.rs | 2 +- src/lib.rs | 4 ++-- src/puzzle.rs | 6 +++--- tests/samuraisudoku.rs | 6 +++--- tests/xkcd.rs | 3 ++- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/constraint/alldifferent.rs b/src/constraint/alldifferent.rs index 0deef62..6366194 100644 --- a/src/constraint/alldifferent.rs +++ b/src/constraint/alldifferent.rs @@ -24,7 +24,7 @@ impl AllDifferent { pub fn new<'a, I>(vars: I) -> Self where I: IntoIterator { AllDifferent { - vars: vars.into_iter().map(|&x| x).collect(), + vars: vars.into_iter().cloned().collect(), } } } diff --git a/src/lib.rs b/src/lib.rs index cba2bbe..02bf6ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ extern crate num_rational; extern crate num_traits; use std::collections::HashMap; -use std::ops::Index; +use std::ops; use num_rational::Rational32; pub use constraint::Constraint; @@ -52,7 +52,7 @@ pub mod constraint; mod linexpr; mod puzzle; -impl Index for Solution { +impl ops::Index for Solution { type Output = Val; fn index(&self, var: VarToken) -> &Val { let VarToken(idx) = var; diff --git a/src/puzzle.rs b/src/puzzle.rs index b5f12d5..360a229 100644 --- a/src/puzzle.rs +++ b/src/puzzle.rs @@ -5,7 +5,7 @@ use std::collections::BTreeSet; use std::fmt; use std::iter; use std::mem; -use std::ops::Index; +use std::ops; use std::rc::Rc; use bit_set::BitSet; @@ -82,7 +82,7 @@ impl Candidates { match self { &Candidates::None => Box::new(iter::empty()), &Candidates::Value(val) => Box::new(iter::once(val)), - &Candidates::Set(ref rc) => Box::new(rc.iter().map(|x| *x)), + &Candidates::Set(ref rc) => Box::new(rc.iter().cloned()), } } } @@ -865,7 +865,7 @@ impl<'a> fmt::Debug for PuzzleSearch<'a> { } } -impl<'a> Index for PuzzleSearch<'a> { +impl<'a> ops::Index for PuzzleSearch<'a> { type Output = Val; /// Get the value assigned to a variable. diff --git a/tests/samuraisudoku.rs b/tests/samuraisudoku.rs index cfcfec5..2380fea 100644 --- a/tests/samuraisudoku.rs +++ b/tests/samuraisudoku.rs @@ -5,12 +5,12 @@ extern crate puzzle_solver; -use puzzle_solver::{Puzzle,Solution,VarToken}; +use puzzle_solver::{Puzzle,Solution,Val,VarToken}; const SQRT_SIZE: usize = 3; const SIZE: usize = 9; -const X: i32 = -1; -type Board = [[i32; SIZE + SQRT_SIZE + SIZE]; SIZE + SQRT_SIZE + SIZE]; +const X: Val = -1; +type Board = [[Val; SIZE + SQRT_SIZE + SIZE]; SIZE + SQRT_SIZE + SIZE]; type SudokuVars = Vec>; type SamuraiVars = (SudokuVars, SudokuVars, SudokuVars, SudokuVars, SudokuVars); diff --git a/tests/xkcd.rs b/tests/xkcd.rs index 9604833..e4bb547 100644 --- a/tests/xkcd.rs +++ b/tests/xkcd.rs @@ -19,7 +19,8 @@ fn xkcd_knapsack() { (Ratio::new(3_35, 100), "Side Salad"), (Ratio::new(3_55, 100), "Hot Wings"), (Ratio::new(4_20, 100), "Mozzarella Sticks"), - (Ratio::new(5_80, 100), "Sampler Plate") ]; + (Ratio::new(5_80, 100), "Sampler Plate"), + ]; let mut sys = Puzzle::new(); let mut vars = Vec::with_capacity(menu.len());