Move LinExpr into linexpr module

This commit is contained in:
Andrey Tkachenko 2021-12-09 22:48:36 +04:00
parent 25a6634069
commit d8323a144f
5 changed files with 54 additions and 50 deletions

View File

@ -4,7 +4,7 @@ use num_rational::Ratio;
use num_traits::Zero; use num_traits::Zero;
use std::rc::Rc; use std::rc::Rc;
use crate::{Constraint, Error, LinExpr, PsResult, PuzzleSearch, Val, VarToken}; use crate::{Constraint, Error, linexpr::LinExpr, PsResult, PuzzleSearch, Val, VarToken};
#[derive(Debug)] #[derive(Debug)]
pub struct Equality { pub struct Equality {

View File

@ -8,16 +8,14 @@ mod linexpr;
mod puzzle; mod puzzle;
mod ranges; mod ranges;
use core::fmt;
use num_rational::Rational32; use num_rational::Rational32;
use num_traits::Signed;
use std::collections::HashMap;
use std::ops; use std::ops;
pub use constraint::Constraint; pub use constraint::Constraint;
pub use error::Error; pub use error::Error;
pub use puzzle::Puzzle; pub use puzzle::Puzzle;
pub use puzzle::PuzzleSearch; pub use puzzle::PuzzleSearch;
pub use linexpr::LinExpr;
/// A puzzle variable token. /// A puzzle variable token.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
@ -29,48 +27,6 @@ pub type Val = i32;
/// The type of the coefficients in a linear expression. /// The type of the coefficients in a linear expression.
pub type Coef = Rational32; pub type Coef = Rational32;
/// A linear expression.
///
/// ```text
/// constant + coef1 * var1 + coef2 * var2 + ...
/// ```
#[derive(Clone)]
pub struct LinExpr {
constant: Coef,
// The non-zero coefficients in the linear expression. If, after
// some manipulations, the coefficient is 0, then it must be
// removed from the map.
coef: HashMap<VarToken, Coef>,
}
impl fmt::Display for LinExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.constant)?;
for (tok, coef) in self.coef.iter() {
if coef.is_negative() {
if coef.abs() == Rational32::from_integer(1) {
write!(f, " - x{}", tok.0)?;
} else {
write!(f, " - {} * x{}", coef.abs(), tok.0)?;
}
} else if coef.abs() == Rational32::from_integer(1) {
write!(f, " + x{}", tok.0)?;
} else {
write!(f, " + {} * x{}", coef, tok.0)?;
}
}
Ok(())
}
}
impl fmt::Debug for LinExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LinExpr {{ {} }}", self)
}
}
/// A result during a puzzle solution search (Err = contradiction). /// A result during a puzzle solution search (Err = contradiction).
pub type PsResult<T> = Result<T, Error>; pub type PsResult<T> = Result<T, Error>;

View File

@ -1,13 +1,57 @@
//! Linear expressions. //! Linear expressions.
use num_rational::{Ratio, Rational32}; use num_rational::{Ratio, Rational32};
use num_traits::{One, Zero}; use num_traits::{One, Signed, Zero};
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::From; use std::convert::From;
use std::fmt;
use std::ops::{Add, Mul, Neg, Sub}; use std::ops::{Add, Mul, Neg, Sub};
use crate::{Coef, LinExpr, VarToken}; use crate::{Coef, VarToken};
/// A linear expression.
///
/// ```text
/// constant + coef1 * var1 + coef2 * var2 + ...
/// ```
#[derive(Clone)]
pub struct LinExpr {
pub(crate) constant: Coef,
// The non-zero coefficients in the linear expression. If, after
// some manipulations, the coefficient is 0, then it must be
// removed from the map.
pub(crate) coef: HashMap<VarToken, Coef>,
}
impl fmt::Display for LinExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.constant)?;
for (tok, coef) in self.coef.iter() {
if coef.is_negative() {
if coef.abs() == Rational32::from_integer(1) {
write!(f, " - x{}", tok.0)?;
} else {
write!(f, " - {} * x{}", coef.abs(), tok.0)?;
}
} else if coef.abs() == Rational32::from_integer(1) {
write!(f, " + x{}", tok.0)?;
} else {
write!(f, " + {} * x{}", coef, tok.0)?;
}
}
Ok(())
}
}
impl fmt::Debug for LinExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LinExpr {{ {} }}", self)
}
}
macro_rules! impl_commutative_op { macro_rules! impl_commutative_op {
($LHS:ident + $RHS:ident) => { ($LHS:ident + $RHS:ident) => {

View File

@ -12,8 +12,8 @@ use std::rc::Rc;
use crate::constraint; use crate::constraint;
use crate::Error; use crate::Error;
use crate::{Constraint, LinExpr, PsResult, Solution, Val, VarToken}; use crate::{Constraint, PsResult, Solution, Val, VarToken};
use crate::linexpr::LinExpr;
use crate::ranges::Ranges; use crate::ranges::Ranges;
/// A collection of candidates. /// A collection of candidates.

View File

@ -183,6 +183,10 @@ impl Constraint for Nonogram {
fn substitute(&self, _search: VarToken, _replace: VarToken) -> PsResult<Rc<dyn Constraint>> { fn substitute(&self, _search: VarToken, _replace: VarToken) -> PsResult<Rc<dyn Constraint>> {
unimplemented!(); unimplemented!();
} }
fn propagate(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()> {
Ok(())
}
} }
/*--------------------------------------------------------------*/ /*--------------------------------------------------------------*/