pub mod xcsp3_core {
use crate::constraints::xconstraint_trait::xcsp3_core::XConstraintTrait;
use crate::utils::utils_functions::xcsp3_utils::{list_to_vec_var_val, tuple_to_vector};
use crate::variables::xdomain::xcsp3_core::XDomainInteger;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
use std::slice::Iter;
pub struct XExtension<'a> {
scope: Vec<XVarVal>,
map: HashMap<String, &'a XDomainInteger>,
set: &'a XVariableSet,
tuples: Vec<Vec<i32>>,
is_support: bool,
}
impl Display for XExtension<'_> {
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 self.is_support {
ret.push_str("supports = ")
} else {
ret.push_str("conflicts = ")
}
write!(f, "XExtension: list = {}{:?}", ret, self.tuples)
}
}
impl XConstraintTrait for XExtension<'_> {
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> XExtension<'a> {
pub fn from_str(
list: &str,
tuple: &str,
is_support: bool,
set: &'a XVariableSet,
) -> Result<Self, Xcsp3Error> {
let a = match list_to_vec_var_val(list) {
Ok(scope_vec_str) => match tuple_to_vector(tuple, !tuple.contains('(')) {
Ok(tuples) => {
Ok(XExtension::new(scope_vec_str, set, tuples, is_support))
}
Err(e) => Err(e),
},
Err(e) => Err(e),
};
a
}
pub fn new(
scope: Vec<XVarVal>,
set: &'a XVariableSet,
tuples: Vec<Vec<i32>>,
is_support: bool,
) -> Self {
XExtension {
scope,
map: Default::default(),
set,
tuples,
is_support,
}
}
pub fn supports_iter(&self) -> Option<Iter<'_, Vec<i32>>> {
if self.is_support {
Some(self.tuples.iter())
} else {
None
}
}
pub fn conflicts_iter(&self) -> Option<Iter<'_, Vec<i32>>> {
if !self.is_support {
Some(self.tuples.iter())
} else {
None
}
}
}
}