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