csp_solver/constraint/
dispatch.rs1use crate::domain::Domain;
4use crate::variable::Variable;
5
6use super::all_different::AllDifferent;
7use super::all_different_except::AllDifferentExcept;
8use super::not_equal::NotEqual;
9use super::soft::SoftLambdaConstraint;
10use super::traits::{Constraint, Revision, VarId};
11
12pub enum ConstraintEnum<D: Domain> {
21 NotEqual(NotEqual),
22 AllDifferent(AllDifferent),
23 AllDifferentExcept(AllDifferentExcept<D::Value>),
24 Soft(SoftLambdaConstraint<D>),
25 Custom(Box<dyn Constraint<D>>),
26}
27
28impl<D: Domain> std::fmt::Debug for ConstraintEnum<D> {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 match self {
31 Self::NotEqual(c) => c.fmt(f),
32 Self::AllDifferent(c) => c.fmt(f),
33 Self::AllDifferentExcept(c) => c.fmt(f),
34 Self::Soft(c) => c.fmt(f),
35 Self::Custom(c) => c.fmt(f),
36 }
37 }
38}
39
40impl<D: Domain> ConstraintEnum<D>
41where
42 D::Value: PartialEq,
43{
44 #[inline]
45 pub fn scope(&self) -> &[VarId] {
46 match self {
47 Self::NotEqual(c) => &c.scope,
48 Self::AllDifferent(c) => &c.scope,
49 Self::AllDifferentExcept(c) => &c.scope,
50 Self::Soft(c) => &c.scope,
51 Self::Custom(c) => c.scope(),
52 }
53 }
54
55 #[inline]
56 pub fn check(&self, assignment: &[Option<D::Value>]) -> bool {
57 match self {
58 Self::NotEqual(c) => c.check_impl(assignment),
59 Self::AllDifferent(c) => c.check_impl(assignment),
60 Self::AllDifferentExcept(c) => c.check_impl(assignment),
61 Self::Soft(_) => true, Self::Custom(c) => c.check(assignment),
63 }
64 }
65
66 #[inline]
69 pub fn soft_penalty(&self, assignment: &[Option<D::Value>]) -> f64 {
70 match self {
71 Self::Soft(c) => {
72 if c.is_satisfied(assignment) {
73 0.0
74 } else {
75 c.penalty
76 }
77 }
78 _ => 0.0,
79 }
80 }
81}
82
83impl<D: Domain> ConstraintEnum<D>
84where
85 D::Value: PartialEq + 'static,
86{
87 #[inline]
88 pub fn revise(&self, vars: &mut [Variable<D>], depth: usize) -> Revision {
89 match self {
90 Self::NotEqual(c) => c.revise_impl(vars, depth),
91 Self::AllDifferent(c) => c.revise_impl(vars, depth),
92 Self::AllDifferentExcept(c) => c.revise_impl(vars, depth),
93 Self::Soft(_) => Revision::Unchanged, Self::Custom(c) => c.revise(vars, depth),
95 }
96 }
97}