pub mod xcsp3_core {
use crate::constraints::xconstraint_trait::xcsp3_core::XConstraintTrait;
use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
use crate::data_structs::xrelational_operator::xcsp3_core::Operator;
use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
use crate::variables::xdomain::xcsp3_core::XDomainInteger;
use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
pub struct XOrdered<'a> {
scope: Vec<XVarVal>,
map: HashMap<String, &'a XDomainInteger>,
set: &'a XVariableSet,
lengths: Option<Vec<XVarVal>>,
operator: Operator,
}
impl Display for XOrdered<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut ret = String::default();
for e in self.scope.iter() {
ret.push('(');
ret.push_str(&e.to_string());
ret.push_str("), ")
}
if let Some(vc) = &self.lengths {
ret.push_str("lengths = (");
for (i, e) in vc.iter().enumerate() {
ret.push_str(&e.to_string());
if i != vc.len() - 1 {
ret.push_str(", ")
}
}
ret.push_str("), ")
}
write!(
f,
"XOrdered: scope = {} operator = {:?}",
ret, self.operator
)
}
}
impl XConstraintTrait for XOrdered<'_> {
fn get_scope_string(&self) -> &Vec<XVarVal> {
&self.scope
}
fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
for e in &self.scope {
if let XVarVal::IntVar(s) = e {
if !self.map.contains_key(s) {
if let Ok(vec) = self.set.construct_scope(&[s]) {
for (vs, vv) in vec.into_iter() {
self.map.insert(vs, vv);
}
}
}
}
}
let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
for e in self.map.iter() {
scope_vec_var.push((e.0, e.1))
}
scope_vec_var
}
}
impl<'a> XOrdered<'a> {
pub fn from_str(
list: &str,
lengths_str: &str,
operator: &str,
set: &'a XVariableSet,
) -> Result<Self, Xcsp3Error> {
match list_to_vec_var_val(list) {
Ok(scope_vec_str) => match list_to_vec_var_val(lengths_str) {
Ok(length_vec_str) => match Operator::get_operator_by_str(operator) {
None => Err(Xcsp3Error::get_constraint_list_of_values_error(
"parse the list of values error. ",
)),
Some(ope) => {
Ok(XOrdered::new(scope_vec_str, set, Some(length_vec_str), ope))
}
},
Err(e) => Err(e),
},
Err(e) => Err(e),
}
}
pub fn from_str_without_lengths(
list: &str,
operator: &str,
set: &'a XVariableSet,
) -> Result<Self, Xcsp3Error> {
match list_to_vec_var_val(list) {
Ok(scope_vec_str) => match Operator::get_operator_by_str(operator) {
None => Err(Xcsp3Error::get_constraint_list_of_values_error(
"parse the list of values error. ",
)),
Some(ope) => Ok(XOrdered::new(scope_vec_str, set, None, ope)),
},
Err(e) => Err(e),
}
}
pub fn new(
scope: Vec<XVarVal>,
set: &'a XVariableSet,
lengths: Option<Vec<XVarVal>>,
operator: Operator,
) -> Self {
XOrdered {
scope,
map: Default::default(),
set,
lengths,
operator,
}
}
pub fn get_lengths(&self) -> &Option<Vec<XVarVal>> {
&self.lengths
}
pub fn get_operator(&self) -> &Operator {
&self.operator
}
}
}