From 74ea9c497f4ebb31642e95edb2c141ec91a177e9 Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Tue, 7 Dec 2021 20:47:45 +0400 Subject: [PATCH] Edition 2021 --- Cargo.toml | 1 + src/constraint/alldifferent.rs | 12 ++++++------ src/constraint/equality.rs | 16 ++++++++-------- src/constraint/mod.rs | 6 +++--- src/constraint/unify.rs | 8 ++++---- src/lib.rs | 6 +++--- src/linexpr.rs | 4 ++-- src/puzzle.rs | 28 ++++++++++++++-------------- tests/nonogram.rs | 8 ++++---- tests/queens.rs | 8 ++++---- tests/takuzu.rs | 8 ++++---- tests/zebra.rs | 2 +- 12 files changed, 54 insertions(+), 53 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e8f6f2f..5e61350 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ license = "MIT" readme = "README.md" keywords = ["constraint", "finite", "domain", "puzzle", "sudoku"] categories = ["science"] +edition = "2021" [dependencies] bit-set = "0.4" diff --git a/src/constraint/alldifferent.rs b/src/constraint/alldifferent.rs index 6366194..744b239 100644 --- a/src/constraint/alldifferent.rs +++ b/src/constraint/alldifferent.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use std::rc::Rc; -use ::{Constraint,PsResult,PuzzleSearch,Val,VarToken}; +use crate::{Constraint,PsResult,PuzzleSearch,Val,VarToken}; pub struct AllDifferent { vars: Vec, @@ -30,14 +30,14 @@ impl AllDifferent { } impl Constraint for AllDifferent { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { Box::new(self.vars.iter()) } fn on_assigned(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> { for &var2 in self.vars.iter().filter(|&v| *v != var) { - try!(search.remove_candidate(var2, val)); + r#try!(search.remove_candidate(var2, val)); } Ok(()) @@ -67,7 +67,7 @@ impl Constraint for AllDifferent { // As many as variables as candidates. for (&val, &opt) in all_candidates.iter() { if let Some(var) = opt { - try!(search.set_candidate(var, val)); + r#try!(search.set_candidate(var, val)); } } } @@ -76,7 +76,7 @@ impl Constraint for AllDifferent { } fn substitute(&self, from: VarToken, to: VarToken) - -> PsResult> { + -> PsResult> { if let Some(idx) = self.vars.iter().position(|&var| var == from) { if !self.vars.contains(&to) { let mut new_vars = self.vars.clone(); @@ -91,7 +91,7 @@ impl Constraint for AllDifferent { #[cfg(test)] mod tests { - use ::{Puzzle,Val}; + use crate::{Puzzle,Val}; #[test] fn test_contradiction() { diff --git a/src/constraint/equality.rs b/src/constraint/equality.rs index 1a96f37..94e512d 100644 --- a/src/constraint/equality.rs +++ b/src/constraint/equality.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use num_rational::Ratio; use num_traits::Zero; -use ::{Constraint,LinExpr,PsResult,PuzzleSearch,Val,VarToken}; +use crate::{Constraint,LinExpr,PsResult,PuzzleSearch,Val,VarToken}; pub struct Equality { // The equation: 0 = constant + coef1 * var1 + coef2 * var2 + ... @@ -32,7 +32,7 @@ impl Equality { } impl Constraint for Equality { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { Box::new(self.eqn.coef.keys()) } @@ -60,7 +60,7 @@ impl Constraint for Equality { // sum + coef * var = 0. let val = -sum / coef; if val.is_integer() { - try!(search.set_candidate(var, val.to_integer())); + r#try!(search.set_candidate(var, val.to_integer())); } else { return Err(()); } @@ -78,7 +78,7 @@ impl Constraint for Equality { let mut sum_max = self.eqn.constant; for (&var, &coef) in self.eqn.coef.iter() { - let (min_val, max_val) = try!(search.get_min_max(var)); + let (min_val, max_val) = r#try!(search.get_min_max(var)); if coef > Ratio::zero() { sum_min = sum_min + coef * Ratio::from_integer(min_val); sum_max = sum_max + coef * Ratio::from_integer(max_val); @@ -104,7 +104,7 @@ impl Constraint for Equality { continue; } - let (min_val, max_val) = try!(search.get_min_max(var)); + let (min_val, max_val) = r#try!(search.get_min_max(var)); let (min_bnd, max_bnd); if coef > Ratio::zero() { @@ -117,7 +117,7 @@ impl Constraint for Equality { if min_val < min_bnd || max_bnd < max_val { let (new_min, new_max) - = try!(search.bound_candidate_range(var, min_bnd, max_bnd)); + = r#try!(search.bound_candidate_range(var, min_bnd, max_bnd)); if coef > Ratio::zero() { sum_min = sum_min + coef * Ratio::from_integer(new_min - min_val); @@ -135,7 +135,7 @@ impl Constraint for Equality { } fn substitute(&self, from: VarToken, to: VarToken) - -> PsResult> { + -> PsResult> { let mut eqn = self.eqn.clone(); if let Some(coef) = eqn.coef.remove(&from) { eqn = eqn + coef * to; @@ -147,7 +147,7 @@ impl Constraint for Equality { #[cfg(test)] mod tests { - use ::{Puzzle,Val}; + use crate::{Puzzle,Val}; #[test] fn test_contradiction() { diff --git a/src/constraint/mod.rs b/src/constraint/mod.rs index ab4ab17..a6eb40e 100644 --- a/src/constraint/mod.rs +++ b/src/constraint/mod.rs @@ -7,12 +7,12 @@ use std::rc::Rc; -use ::{PsResult,PuzzleSearch,Val,VarToken}; +use crate::{PsResult,PuzzleSearch,Val,VarToken}; /// Constraint trait. pub trait Constraint { /// An iterator over the variables that are involved in the constraint. - fn vars<'a>(&'a self) -> Box + 'a>; + fn vars<'a>(&'a self) -> Box + 'a>; /// Applied after a variable has been assigned. fn on_assigned(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val) @@ -30,7 +30,7 @@ pub trait Constraint { /// Returns a new constraint with all instances of "from" replaced /// with "to", or Err if a contradiction was found. fn substitute(&self, from: VarToken, to: VarToken) - -> PsResult>; + -> PsResult>; } pub use self::alldifferent::AllDifferent; diff --git a/src/constraint/unify.rs b/src/constraint/unify.rs index 8a59373..4c5661f 100644 --- a/src/constraint/unify.rs +++ b/src/constraint/unify.rs @@ -3,7 +3,7 @@ use std::iter; use std::rc::Rc; -use ::{Constraint,PsResult,PuzzleSearch,VarToken}; +use crate::{Constraint,PsResult,PuzzleSearch,VarToken}; pub struct Unify { var1: VarToken, @@ -33,7 +33,7 @@ impl Unify { } impl Constraint for Unify { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { if self.var1 != self.var2 { Box::new(iter::once(&self.var1).chain(iter::once(&self.var2))) } else { @@ -50,7 +50,7 @@ impl Constraint for Unify { } fn substitute(&self, from: VarToken, to: VarToken) - -> PsResult> { + -> PsResult> { let var1 = if self.var1 == from { to } else { self.var1 }; let var2 = if self.var2 == from { to } else { self.var2 }; Ok(Rc::new(Unify{ var1: var1, var2: var2 })) @@ -59,7 +59,7 @@ impl Constraint for Unify { #[cfg(test)] mod tests { - use ::Puzzle; + use crate::Puzzle; #[test] fn test_unify_alldifferent() { diff --git a/src/lib.rs b/src/lib.rs index 02bf6ec..863e668 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,9 +9,9 @@ use std::collections::HashMap; use std::ops; use num_rational::Rational32; -pub use constraint::Constraint; -pub use puzzle::Puzzle; -pub use puzzle::PuzzleSearch; +pub use crate::constraint::Constraint; +pub use crate::puzzle::Puzzle; +pub use crate::puzzle::PuzzleSearch; /// A puzzle variable token. #[derive(Copy,Clone,Debug,Eq,Hash,PartialEq)] diff --git a/src/linexpr.rs b/src/linexpr.rs index 7992e5f..4fcbd05 100644 --- a/src/linexpr.rs +++ b/src/linexpr.rs @@ -7,7 +7,7 @@ use std::ops::{Add,Mul,Neg,Sub}; use num_rational::{Ratio,Rational32}; use num_traits::{One,Zero}; -use ::{Coef,LinExpr,VarToken}; +use crate::{Coef,LinExpr,VarToken}; macro_rules! impl_commutative_op { ($LHS:ident + $RHS:ident) => { @@ -219,7 +219,7 @@ impl_subtract_op!(LinExpr - LinExpr); #[cfg(test)] mod tests { use num_rational::Ratio; - use ::Puzzle; + use crate::Puzzle; #[test] fn test_ops() { diff --git a/src/puzzle.rs b/src/puzzle.rs index 360a229..e8f7a6b 100644 --- a/src/puzzle.rs +++ b/src/puzzle.rs @@ -9,8 +9,8 @@ use std::ops; use std::rc::Rc; use bit_set::BitSet; -use ::{Constraint,LinExpr,PsResult,Solution,Val,VarToken}; -use constraint; +use crate::{Constraint,LinExpr,PsResult,Solution,Val,VarToken}; +use crate::constraint; /// A collection of candidates. #[derive(Clone,Debug,Eq,PartialEq)] @@ -40,14 +40,14 @@ pub struct Puzzle { candidates: Vec, // The list of puzzle constraints. - constraints: Vec>, + constraints: Vec>, } /// The puzzle constraints, and the variables that wake them up. struct PuzzleConstraints { // The list of puzzle constraints, possibly with variables // substituted out. - constraints: Vec>, + constraints: Vec>, // The list of constraints that each variable affects. These will // be woken up when the variable's candidates are changed. @@ -78,7 +78,7 @@ impl Candidates { } /// Get an iterator over all of the candidates of a variable. - fn iter<'a>(&'a self) -> Box + 'a> { + fn iter<'a>(&'a self) -> Box + 'a> { match self { &Candidates::None => Box::new(iter::empty()), &Candidates::Value(val) => Box::new(iter::once(val)), @@ -467,7 +467,7 @@ impl PuzzleConstraints { let mut new_constraints = self.constraints.clone(); for cidx in self.wake[idx].iter() { - let rc = try!(self.constraints[cidx].substitute(from, to)); + let rc = r#try!(self.constraints[cidx].substitute(from, to)); new_constraints[cidx] = rc; } @@ -479,7 +479,7 @@ impl PuzzleConstraints { } /// Determine which variables wake up which constraints. - fn init_wake(constraints: &Vec>, num_vars: usize) + fn init_wake(constraints: &Vec>, num_vars: usize) -> Vec { let mut wake = vec![BitSet::new(); num_vars]; for cidx in 0..constraints.len() { @@ -539,7 +539,7 @@ impl<'a> PuzzleSearch<'a> { /// Get an iterator over the candidates to an unassigned variable. pub fn get_unassigned(&'a self, var: VarToken) - -> Box + 'a> { + -> Box + 'a> { let VarToken(idx) = var; match &self.vars[idx] { &VarState::Assigned(_) => Box::new(iter::empty()), @@ -731,7 +731,7 @@ impl<'a> PuzzleSearch<'a> { for cidx in 0..self.constraints.constraints.len() { if self.constraints.wake[idx].contains(cidx) { let constraint = self.constraints.constraints[cidx].clone(); - try!(constraint.on_assigned(self, var, val)); + r#try!(constraint.on_assigned(self, var, val)); } } @@ -761,7 +761,7 @@ impl<'a> PuzzleSearch<'a> { }; if let Some(val) = gimme { - try!(self.assign(idx, val)); + r#try!(self.assign(idx, val)); last_gimme = idx; } else if idx == last_gimme { break; @@ -775,7 +775,7 @@ impl<'a> PuzzleSearch<'a> { let wake = mem::replace(&mut self.wake, BitSet::new()); for cidx in wake.iter() { let constraint = self.constraints.constraints[cidx].clone(); - try!(constraint.on_updated(self)); + r#try!(constraint.on_updated(self)); } } } @@ -816,14 +816,14 @@ impl<'a> PuzzleSearch<'a> { let VarToken(replace) = to; // Create new constraints to reflect the unification. - let new_constraints = try!(self.constraints.substitute(from, to)); + let new_constraints = r#try!(self.constraints.substitute(from, to)); self.constraints = Rc::new(new_constraints); self.wake.union_with(&self.constraints.wake[replace]); assert!(self.constraints.wake[search].is_empty()); // Take intersection of the candidates. if let &VarState::Assigned(val) = &self.vars[search] { - try!(self.set_candidate(to, val)); + r#try!(self.set_candidate(to, val)); } else { if let (&mut VarState::Unassigned(Candidates::Set(ref mut rc1)), &mut VarState::Unassigned(Candidates::Set(ref mut rc2))) @@ -905,7 +905,7 @@ fn get_two_mut<'a, T>(slice: &'a mut [T], a: usize, b: usize) #[cfg(test)] mod tests { - use ::Puzzle; + use crate::Puzzle; #[test] fn test_no_vars() { diff --git a/tests/nonogram.rs b/tests/nonogram.rs index 87b5eb0..5987fd7 100644 --- a/tests/nonogram.rs +++ b/tests/nonogram.rs @@ -140,7 +140,7 @@ impl Nonogram { } impl Constraint for Nonogram { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { Box::new(self.vars.iter()) } @@ -162,9 +162,9 @@ impl Constraint for Nonogram { AutoFillResult::SolutionFound => for idx in 0..self.vars.len() { if accum[idx] == FLAG_UNKNOWN | FLAG_OFF { - try!(search.set_candidate(self.vars[idx], 0)); + r#try!(search.set_candidate(self.vars[idx], 0)); } else if accum[idx] == FLAG_UNKNOWN | FLAG_ON { - try!(search.set_candidate(self.vars[idx], 1)); + r#try!(search.set_candidate(self.vars[idx], 1)); } }, } @@ -173,7 +173,7 @@ impl Constraint for Nonogram { } fn substitute(&self, _search: VarToken, _replace: VarToken) - -> PsResult> { + -> PsResult> { unimplemented!(); } } diff --git a/tests/queens.rs b/tests/queens.rs index 2f59d44..9a5b2dd 100644 --- a/tests/queens.rs +++ b/tests/queens.rs @@ -12,7 +12,7 @@ struct NoDiagonal { } impl Constraint for NoDiagonal { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { Box::new(self.vars.iter()) } @@ -23,8 +23,8 @@ impl Constraint for NoDiagonal { if !search.is_assigned(var2) { let x1 = val; let dy = (y1 as Val) - (y2 as Val); - try!(search.remove_candidate(var2, x1 - dy)); - try!(search.remove_candidate(var2, x1 + dy)); + r#try!(search.remove_candidate(var2, x1 - dy)); + r#try!(search.remove_candidate(var2, x1 + dy)); } } @@ -32,7 +32,7 @@ impl Constraint for NoDiagonal { } fn substitute(&self, _from: VarToken, _to: VarToken) - -> PsResult> { + -> PsResult> { unimplemented!(); } } diff --git a/tests/takuzu.rs b/tests/takuzu.rs index 5a342e0..8359bbb 100644 --- a/tests/takuzu.rs +++ b/tests/takuzu.rs @@ -19,7 +19,7 @@ struct BinaryRepr { } impl Constraint for BinaryRepr { - fn vars<'a>(&'a self) -> Box + 'a> { + fn vars<'a>(&'a self) -> Box + 'a> { Box::new(iter::once(&self.value).chain(&self.bits)) } @@ -28,7 +28,7 @@ impl Constraint for BinaryRepr { if var == self.value { let mut val = val; for &var in self.bits.iter() { - try!(search.set_candidate(var, val & 1)); + r#try!(search.set_candidate(var, val & 1)); val = val >> 1; } } else if let Some(bitpos) = self.bits.iter().position(|&v| v == var) { @@ -38,7 +38,7 @@ impl Constraint for BinaryRepr { .collect::>(); for c in discard.into_iter() { - try!(search.remove_candidate(self.value, c)); + r#try!(search.remove_candidate(self.value, c)); } } @@ -46,7 +46,7 @@ impl Constraint for BinaryRepr { } fn substitute(&self, _from: VarToken, _to: VarToken) - -> PsResult> { + -> PsResult> { unimplemented!(); } } diff --git a/tests/zebra.rs b/tests/zebra.rs index 5d11f52..6eaf41a 100644 --- a/tests/zebra.rs +++ b/tests/zebra.rs @@ -26,7 +26,7 @@ enum Pet { Bird, Cat, Dog, Fish, Horse } #[test] fn zebra() { - use Nat::*; use Col::*; use Dri::*; use Smo::*; use Pet::*; + use crate::Nat::*; use crate::Col::*; use crate::Dri::*; use crate::Smo::*; use crate::Pet::*; // #1: There are five houses. let mut sys = Puzzle::new();