use super::{Problem, VariableIndex};
use deepsize::DeepSizeOf;
use dyn_clone::DynClone;
use std::any::Any;
use std::hash::Hasher;
pub trait Constraint: DeepSizeOf + DynClone + Send + Sync {
fn update_variable_ordering(&mut self, order: &[VariableIndex]);
fn structural_key(&self, problem: &Problem) -> ConstraintShapeKey;
fn precedence_edges(&self) -> Vec<(VariableIndex, VariableIndex)> {
vec![]
}
fn is_layer_in_scope(&self, layer: usize) -> bool;
fn iter_scope(&self) -> Box<dyn Iterator<Item = VariableIndex> + '_>;
fn is_satisfied(&self, assignment: &[isize]) -> bool;
fn name(&self) -> &'static str;
fn as_any(&self) -> &dyn Any;
fn identity_property(&self) -> Box<dyn ConstraintProperty>;
fn empty_property(&self) -> Box<dyn ConstraintProperty> {
self.identity_property()
}
fn empty_property_backward(&self) -> Box<dyn ConstraintProperty> {
self.empty_property()
}
fn is_assignment_invalid(
&self,
parent: &dyn ConstraintProperty,
child: &dyn ConstraintProperty,
layer: usize,
assignment: isize,
) -> bool;
}
dyn_clone::clone_trait_object!(Constraint);
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ConstraintShapeKey {
AllDifferent {
arity: usize,
},
NotEquals,
Among {
arity: usize,
values: Vec<isize>,
lb: usize,
ub: usize,
},
AtLeast {
arity: usize,
values: Vec<isize>,
lb: usize,
},
Gcc {
arity: usize,
bounds: Vec<(isize, usize, usize)>,
},
Regular {
arity: usize,
transitions: Vec<Vec<Option<usize>>>,
initial_state: usize,
accepting_states: Vec<usize>,
},
Sum {
arity: usize,
target: isize,
},
}
impl ConstraintShapeKey {
pub fn arity(&self) -> usize {
match self {
Self::NotEquals => 2,
Self::AllDifferent { arity }
| Self::Among { arity, .. }
| Self::AtLeast { arity, .. }
| Self::Gcc { arity, .. }
| Self::Regular { arity, .. }
| Self::Sum { arity, .. } => *arity,
}
}
}
pub trait ConstraintProperty: DeepSizeOf + DynClone + Send + Sync {
fn update(&mut self, other: &dyn ConstraintProperty, assignment: isize, in_scope: bool);
fn update_backward(
&mut self,
other: &dyn ConstraintProperty,
assignment: isize,
in_scope: bool,
) {
self.update(other, assignment, in_scope);
}
fn merge(&mut self, other: &dyn ConstraintProperty);
fn hash(&self, hasher: &mut dyn Hasher);
fn eq(&self, other: &dyn ConstraintProperty) -> bool;
fn order_key(&self) -> Vec<f64>;
fn is_single_state(&self) -> bool;
fn as_any(&self) -> &dyn Any;
fn name(&self) -> &'static str;
}
dyn_clone::clone_trait_object!(ConstraintProperty);
impl std::hash::Hash for dyn ConstraintProperty {
fn hash<H: Hasher>(&self, state: &mut H) {
ConstraintProperty::hash(self, state)
}
}
impl PartialEq for dyn ConstraintProperty {
fn eq(&self, other: &Self) -> bool {
ConstraintProperty::eq(self, other)
}
}
impl Eq for dyn ConstraintProperty {}