1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use itertools::Itertools;
use super::raw_constraint::RawConstraint;
use crate::model::Component;
use std::{collections::HashMap, fmt::Debug};
#[derive(PartialEq, Debug)]
pub struct RawComponent<T> {
name: String,
variables: Vec<String>,
values: Vec<T>,
constraints: Vec<RawConstraint<T>>,
}
impl<T> RawComponent<T> {
pub fn new<S: Into<String>>(
name: S,
variables: Vec<S>,
values: Vec<T>,
constraints: Vec<RawConstraint<T>>,
) -> Self {
Self {
name: name.into(),
variables: variables.into_iter().map_into().collect(),
values,
constraints,
}
}
pub fn indices(&self) -> HashMap<String, usize> {
let mut name_to_index = HashMap::new();
for (i, v) in self.variables.iter().enumerate() {
name_to_index.insert(v.to_string(), i);
}
name_to_index
}
pub fn add_constraint(&mut self, c: RawConstraint<T>) {
self.constraints.push(c);
}
pub fn into_component(self) -> Component<T>
where
T: Clone,
{
let var_to_idx = self.indices();
let constraints = self
.constraints
.into_iter()
.map(|c| c.into_constraint(&var_to_idx))
.collect();
Component::new_with_map(
self.name.to_string(),
var_to_idx.into_iter().map(|(k, v)| (k, v)).collect(),
self.values,
constraints,
)
}
}