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
pub mod index;
pub use index::*;
use opensrdk_linear_algebra::indices_cartesian_product;
use crate::Expression;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ExpressionArray {
sizes: Vec<usize>,
elems: HashMap<Vec<usize>, Expression>,
default: Box<Expression>,
}
impl ExpressionArray {
pub fn new(sizes: Vec<usize>) -> Self {
Self {
sizes,
elems: HashMap::new(),
default: Box::new(0.0.into()),
}
}
pub fn from_factory(sizes: Vec<usize>, factory: impl Fn(&[usize]) -> Expression) -> Self {
let mut elems = HashMap::new();
let indices = indices_cartesian_product(&sizes);
indices.iter().for_each(|index| {
elems.insert(index.clone(), factory(&index));
});
Self {
sizes,
elems,
default: Box::new(0.0.into()),
}
}
pub fn sizes(&self) -> &[usize] {
&self.sizes
}
pub fn elems(&self) -> &HashMap<Vec<usize>, Expression> {
&self.elems
}
pub fn eject(self) -> (Vec<usize>, HashMap<Vec<usize>, Expression>) {
(self.sizes, self.elems)
}
}