xcsp3_rust/constraints/
xordered.rs1pub mod xcsp3_core {
42 use crate::constraints::xconstraint_trait::xcsp3_core::XConstraintTrait;
43 use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
44 use crate::data_structs::xrelational_operator::xcsp3_core::Operator;
45 use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
46 use std::collections::HashMap;
47 use std::fmt::{Display, Formatter};
48
49 use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
50 use crate::variables::xdomain::xcsp3_core::XDomainInteger;
51 use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
52
53 pub struct XOrdered<'a> {
55 scope: Vec<XVarVal>,
56 map: HashMap<String, &'a XDomainInteger>,
57 set: &'a XVariableSet,
58 lengths: Option<Vec<XVarVal>>,
59 operator: Operator,
60 }
61
62 impl Display for XOrdered<'_> {
63 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64 let mut ret = String::default();
65 for e in self.scope.iter() {
66 ret.push('(');
67 ret.push_str(&e.to_string());
68 ret.push_str("), ")
69 }
70 if let Some(vc) = &self.lengths {
71 ret.push_str("lengths = (");
72 for (i, e) in vc.iter().enumerate() {
73 ret.push_str(&e.to_string());
74 if i != vc.len() - 1 {
75 ret.push_str(", ")
76 }
77 }
78 ret.push_str("), ")
79 }
80 write!(
81 f,
82 "XOrdered: scope = {} operator = {:?}",
83 ret, self.operator
84 )
85 }
86 }
87
88 impl XConstraintTrait for XOrdered<'_> {
89 fn get_scope_string(&self) -> &Vec<XVarVal> {
90 &self.scope
91 }
92
93 fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
94 for e in &self.scope {
95 if let XVarVal::IntVar(s) = e {
96 if !self.map.contains_key(s) {
97 if let Ok(vec) = self.set.construct_scope(&[s]) {
98 for (vs, vv) in vec.into_iter() {
99 self.map.insert(vs, vv);
100 }
101 }
102 }
103 }
104 }
105 let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
106 for e in self.map.iter() {
107 scope_vec_var.push((e.0, e.1))
108 }
109 scope_vec_var
110 }
111 }
112
113 impl<'a> XOrdered<'a> {
114 pub fn from_str(
115 list: &str,
116 lengths_str: &str,
117 operator: &str,
118 set: &'a XVariableSet,
119 ) -> Result<Self, Xcsp3Error> {
120 match list_to_vec_var_val(list) {
121 Ok(scope_vec_str) => match list_to_vec_var_val(lengths_str) {
122 Ok(length_vec_str) => match Operator::get_operator_by_str(operator) {
123 None => Err(Xcsp3Error::get_constraint_list_of_values_error(
124 "parse the list of values error. ",
125 )),
126 Some(ope) => {
127 Ok(XOrdered::new(scope_vec_str, set, Some(length_vec_str), ope))
128 }
129 },
130 Err(e) => Err(e),
131 },
132 Err(e) => Err(e),
133 }
134 }
135
136 pub fn from_str_without_lengths(
137 list: &str,
138 operator: &str,
139 set: &'a XVariableSet,
140 ) -> Result<Self, Xcsp3Error> {
141 match list_to_vec_var_val(list) {
142 Ok(scope_vec_str) => match Operator::get_operator_by_str(operator) {
143 None => Err(Xcsp3Error::get_constraint_list_of_values_error(
144 "parse the list of values error. ",
145 )),
146 Some(ope) => Ok(XOrdered::new(scope_vec_str, set, None, ope)),
147 },
148 Err(e) => Err(e),
149 }
150 }
151
152 pub fn new(
153 scope: Vec<XVarVal>,
154 set: &'a XVariableSet,
155 lengths: Option<Vec<XVarVal>>,
156 operator: Operator,
157 ) -> Self {
158 XOrdered {
159 scope,
160 map: Default::default(),
161 set,
162 lengths,
163 operator,
164 }
165 }
166 pub fn get_lengths(&self) -> &Option<Vec<XVarVal>> {
167 &self.lengths
168 }
169 pub fn get_operator(&self) -> &Operator {
170 &self.operator
171 }
172 }
173}