1#[derive(Debug, Clone, PartialEq)]
8pub struct Grid2<T = f64> {
9 x: Vec<f64>,
10 y: Vec<f64>,
11 data: Vec<T>,
12}
13
14impl<T> Grid2<T> {
15 pub fn new(x: Vec<f64>, y: Vec<f64>, data: Vec<T>) -> Self {
17 assert_eq!(
18 data.len(),
19 x.len() * y.len(),
20 "Grid2: {} values for {} × {}",
21 data.len(),
22 x.len(),
23 y.len()
24 );
25 Grid2 { x, y, data }
26 }
27
28 pub fn from_rows(x: Vec<f64>, y: Vec<f64>, rows: Vec<Vec<T>>) -> Self {
30 assert!(
31 rows.len() == y.len() && rows.iter().all(|r| r.len() == x.len()),
32 "Grid2: rows do not match {} × {}",
33 x.len(),
34 y.len()
35 );
36 let data = rows.into_iter().flatten().collect();
37 Grid2 { x, y, data }
38 }
39
40 pub fn nx(&self) -> usize {
42 self.x.len()
43 }
44
45 pub fn ny(&self) -> usize {
47 self.y.len()
48 }
49
50 pub fn x(&self) -> &[f64] {
52 &self.x
53 }
54
55 pub fn y(&self) -> &[f64] {
57 &self.y
58 }
59
60 pub fn data(&self) -> &[T] {
62 &self.data
63 }
64
65 pub fn data_mut(&mut self) -> &mut [T] {
66 &mut self.data
67 }
68
69 pub fn into_parts(self) -> (Vec<f64>, Vec<f64>, Vec<T>) {
71 (self.x, self.y, self.data)
72 }
73
74 pub fn get(&self, ix: usize, iy: usize) -> &T {
76 assert!(ix < self.nx(), "Grid2: ix {ix} ≥ {}", self.nx());
77 &self.data[iy * self.nx() + ix]
78 }
79
80 pub fn row(&self, iy: usize) -> &[T] {
82 let n = self.nx();
83 &self.data[iy * n..(iy + 1) * n]
84 }
85
86 pub fn rows(&self) -> impl ExactSizeIterator<Item = &[T]> + '_ {
88 (0..self.ny()).map(move |iy| self.row(iy))
90 }
91
92 pub fn map<U>(&self, f: impl FnMut(&T) -> U) -> Grid2<U> {
94 Grid2 {
95 x: self.x.clone(),
96 y: self.y.clone(),
97 data: self.data.iter().map(f).collect(),
98 }
99 }
100}
101
102impl<T> std::ops::Index<usize> for Grid2<T> {
104 type Output = [T];
105 fn index(&self, iy: usize) -> &[T] {
106 self.row(iy)
107 }
108}
109
110impl<T> std::ops::IndexMut<usize> for Grid2<T> {
111 fn index_mut(&mut self, iy: usize) -> &mut [T] {
112 let n = self.nx();
113 &mut self.data[iy * n..(iy + 1) * n]
114 }
115}
116
117impl<T: Clone> Grid2<T> {
118 pub fn to_rows(&self) -> Vec<Vec<T>> {
120 self.rows().map(<[T]>::to_vec).collect()
121 }
122}
123
124impl Grid2<f64> {
125 pub fn max(&self) -> f64 {
127 self.data
128 .iter()
129 .copied()
130 .filter(|v| v.is_finite())
131 .fold(f64::NEG_INFINITY, f64::max)
132 }
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138
139 #[test]
140 fn rows_and_flat_agree() {
141 let g = Grid2::from_rows(
142 vec![0.0, 1.0, 2.0],
143 vec![10.0, 20.0],
144 vec![vec![1, 2, 3], vec![4, 5, 6]],
145 );
146 assert_eq!((g.nx(), g.ny()), (3, 2));
147 assert_eq!(g.data(), &[1, 2, 3, 4, 5, 6]);
148 assert_eq!(*g.get(2, 1), 6);
149 assert_eq!(g.row(1), &[4, 5, 6]);
150 assert_eq!(g[1][2], 6);
151 assert_eq!(g.to_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
152 assert_eq!(g.map(|v| v * 2).data(), &[2, 4, 6, 8, 10, 12]);
153 let e: Grid2<f64> = Grid2::new(Vec::new(), vec![0.0, 1.0], Vec::new());
154 assert_eq!(e.rows().len(), 2);
155 }
156
157 #[test]
158 #[should_panic]
159 fn wrong_length_panics() {
160 Grid2::new(vec![0.0], vec![0.0, 1.0], vec![1.0]);
161 }
162}