ospf_rust_multiarray/
multi_array.rs1use std::alloc::{Allocator, Global};
2use std::ops::{Index, IndexMut};
3
4use crate::shape::*;
5use crate::dummy_vector::*;
6
7pub struct MultiArray<T: Sized, S: Shape> {
8 list: Vec<T>,
9 pub shape: S,
10}
11
12impl<T: Sized, S: Shape> MultiArray<T, S> {
13 pub fn new(shape: S) -> Self where T: Default {
14 Self {
15 list: (0..shape.len())
16 .map(|_| T::default())
17 .collect(),
18 shape,
19 }
20 }
21
22 pub fn new_with(shape: S, value: T) -> Self
23 where
24 T: Clone,
25 {
26 Self {
27 list: (0..shape.len())
28 .map(|_| value.clone())
29 .collect(),
30 shape,
31 }
32 }
33
34 pub fn new_by<G>(shape: S, generator: G) -> Self
35 where
36 G: Fn(usize, &<S as Shape>::VectorType) -> T,
37 {
38 Self {
39 list: (0..shape.len())
40 .map(|index| generator(index, &shape.vector(index)))
41 .collect(),
42 shape,
43 }
44 }
45
46 pub fn get(&self, vector: S::DummyVectorType) -> Result<Vec<&T>, OutOfShapeError> {
47 let mut ret = Vec::new();
48 let policy = DummyAccessPolicy::new(&vector, &self.shape);
49 let mut iter = policy.iter();
50 loop {
51 match iter.next() {
52 Some(vector) => {
53 let index = self.shape.index(vector).unwrap();
54 ret.push(&self.list[index]);
55 }
56 None => {
57 break;
58 }
59 }
60 }
61 Ok(ret)
62 }
63
64 pub fn iter(&self) -> impl Iterator<Item = &T> {
65 self.list.iter()
66 }
67
68 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
69 self.list.iter_mut()
70 }
71
72 }
74
75impl<T: Sized, S: Shape> Index<usize> for MultiArray<T, S> {
76 type Output = T;
77
78 fn index(&self, index: usize) -> &T {
79 &self.list[index]
80 }
81}
82
83impl<T: Sized, S: Shape> IndexMut<usize> for MultiArray<T, S> {
84 fn index_mut(&mut self, index: usize) -> &mut T {
85 &mut self.list[index]
86 }
87}
88
89impl<T: Sized, S: Shape> Index<&S::VectorType> for MultiArray<T, S> {
90 type Output = T;
91
92 fn index(&self, vector: &S::VectorType) -> &T {
93 match self.shape.index(vector) {
94 Ok(index) => &self.list[index],
95 Err(err) => panic!("{}", err),
96 }
97 }
98}
99
100impl<T: Sized, S: Shape> IndexMut<&S::VectorType> for MultiArray<T, S> {
101 fn index_mut(&mut self, vector: &S::VectorType) -> &mut T {
102 match self.shape.index(vector) {
103 Ok(index) => &mut self.list[index],
104 Err(err) => panic!("{}", err),
105 }
106 }
107}
108
109type MultiArray1<T> = MultiArray<T, Shape1>;
110type MultiArray2<T> = MultiArray<T, Shape2>;
111type MultiArray3<T> = MultiArray<T, Shape3>;
112type MultiArray4<T> = MultiArray<T, Shape4>;
113type MultiArray5<T> = MultiArray<T, Shape5>;
114type MultiArray6<T> = MultiArray<T, Shape6>;
115type MultiArray7<T> = MultiArray<T, Shape7>;
116type MultiArray8<T> = MultiArray<T, Shape8>;
117type MultiArray9<T> = MultiArray<T, Shape9>;
118type MultiArray10<T> = MultiArray<T, Shape10>;
119type MultiArray11<T> = MultiArray<T, Shape11>;
120type MultiArray12<T> = MultiArray<T, Shape12>;
121type MultiArray13<T> = MultiArray<T, Shape13>;
122type MultiArray14<T> = MultiArray<T, Shape14>;
123type MultiArray15<T> = MultiArray<T, Shape15>;
124type MultiArray16<T> = MultiArray<T, Shape16>;
125type MultiArray17<T> = MultiArray<T, Shape17>;
126type MultiArray18<T> = MultiArray<T, Shape18>;
127type MultiArray19<T> = MultiArray<T, Shape19>;
128type MultiArray20<T> = MultiArray<T, Shape20>;
129type DynMultiArray<T> = MultiArray<T, DynShape>;
130
131macro_rules! vector_index {
132 ($x:literal) => {
133 $x as usize
134 };
135 ($x:expr) => {
136 $x as usize
137 };
138}
139
140#[macro_export]
141macro_rules! vector {
142 ($($x:expr),*) => {
143 &[$(vector_index!($x),)*]
144 };
145}
146
147#[macro_export]
148macro_rules! dyn_vector {
149 ($($x:expr),*) => {
150 vec!($(vector_index!($x),)*)
151 };
152}