Tue, 09 Sep 2025 08:24:27 -0500
Remove Norm trait bound from BoundedLinear
| 5 | 1 | /*! |
| 2 | Linear grids. | |
| 3 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
4 | These are multi-dimensional intervals $\prod\_{i=1}^N [a\_i, b\_i]$ divided along each dimension |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
5 | into $n\_i$ equally-spaced nodes, with $a\_i$ the first node and $b\_i$ the last node along each |
| 5 | 6 | dimension. |
| 7 | ||
| 8 | The [`LinSpace`]s provided by this module are similar to [`num::range_step_inclusive`], but as an | |
| 9 | iterator they are [restartable][RestartableIterator] and parametrised by the number of nodes | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
10 | instead of a step. This way it can be ensured that $a\_i$ and $b\_i$ are the last and the first |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
11 | node. |
| 5 | 12 | |
| 13 | The starting points for the use of this module are the [`linspace`], [`lingrid`], and | |
| 14 | [`lingrid_centered`] functions. They return a [`LinSpace`]s that implements [`IntoIterator`] for | |
| 15 | iteration over the grid. Additional utility functions are in the [`Grid`] trait. | |
| 16 | */ | |
| 0 | 17 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
18 | use crate::iter::{RestartableIterator, StatefulIterator}; |
| 0 | 19 | use crate::loc::Loc; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
20 | use crate::maputil::{map2, map4}; |
| 0 | 21 | use crate::sets::Cube; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
22 | use crate::types::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
23 | use serde::{Deserialize, Serialize}; |
| 0 | 24 | |
| 25 | // TODO: rewrite this using crate::sets::Cube. | |
| 26 | ||
| 5 | 27 | /// An abstraction of possibly multi-dimensional linear grids. |
| 28 | /// | |
| 29 | /// `U` is typically a `F` for a `Float` `F` for one-dimensional grids created by `linspace`, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
30 | /// or [`Loc`]`<N, F>` for multi-dimensional grids created by `lingrid`. |
| 5 | 31 | /// In the first case `count` of nodes is `usize`, and in the second case `[usize; N]`. |
| 32 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
| 33 | pub struct LinSpace<U, I> { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
34 | pub start: U, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
35 | pub end: U, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
36 | pub count: I, |
| 0 | 37 | } |
| 38 | ||
| 5 | 39 | /// A `N`-dimensional interval divided into an indicated number of equally-spaced nodes along |
| 40 | /// each dimension. | |
| 0 | 41 | #[allow(type_alias_bounds)] // Need it to access F::CompatibleSize. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
42 | pub type LinGrid<const N: usize, F: Float = f64> = LinSpace<Loc<N, F>, [usize; N]>; |
| 0 | 43 | |
| 44 | /// Creates a [`LinSpace`] on the real line. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
45 | pub fn linspace<F: Float>(start: F, end: F, count: usize) -> LinSpace<F, usize> { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
46 | LinSpace { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
47 | start: start, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
48 | end: end, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
49 | count: count, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
50 | } |
| 0 | 51 | } |
| 52 | ||
| 5 | 53 | /// Creates a multi-dimensional linear grid. |
| 0 | 54 | /// |
| 55 | /// The first and last point in each dimension are the boundaries of the corresponding | |
| 56 | /// dimensions of `cube`, and there are `count` nodes along each dimension. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
57 | pub fn lingrid<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
58 | cube: &Cube<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
59 | count: &[usize; N], |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
60 | ) -> LinSpace<Loc<N, F>, [usize; N]> { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
61 | LinSpace { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
62 | start: cube.span_start(), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
63 | end: cube.span_end(), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
64 | count: *count, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
65 | } |
| 0 | 66 | } |
| 67 | ||
| 68 | /// Create a multi-dimensional linear grid with centered nodes. | |
| 69 | /// | |
| 70 | /// There are `count` along each dimension and each node has equally-sized subcube surrounding it | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
71 | /// inside `cube`. Thus, if $w\_i$ is the width of the cube along dimension $i$, and $n_i$ the number |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
72 | /// of nodes, the width of the subcube along this dimension is $h_i = w\_i/(n\_i+1)$, and the first |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
73 | /// and last nodes are at a distance $h\_i/2$ from the closest boundary. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
74 | pub fn lingrid_centered<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
75 | cube: &Cube<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
76 | count: &[usize; N], |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
77 | ) -> LinSpace<Loc<N, F>, [usize; N]> { |
| 0 | 78 | let h_div_2 = map2(cube.width(), count, |w, &n| w / F::cast_from(2 * (n + 1))); |
| 79 | let span_start = map2(cube.span_start(), &h_div_2, |a, &t| a + t).into(); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
80 | let span_end = map2(cube.span_end(), &h_div_2, |b, &t| b - t).into(); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
81 | LinSpace { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
82 | start: span_start, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
83 | end: span_end, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
84 | count: *count, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
85 | } |
| 0 | 86 | } |
| 87 | ||
| 88 | /// Iterator over a `LinSpace`. | |
| 89 | #[derive(Clone, Debug)] | |
| 90 | pub struct LinSpaceIterator<F, I> { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
91 | lingrid: LinSpace<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
92 | current: Option<I>, |
| 0 | 93 | } |
| 94 | ||
| 5 | 95 | /// Abstraction of a linear grid over space `U` with multi-dimensional index set `I`. |
| 96 | pub trait Grid<U, I> { | |
| 97 | /// Converts a linear index `i` into a grid point. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
98 | fn entry_linear_unchecked(&self, i: usize) -> U; |
| 5 | 99 | // Converts a multi-dimensional index `i` into a grid point. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
100 | fn entry_unchecked(&self, i: &I) -> U; |
| 5 | 101 | |
| 102 | // fn entry(&self, i : I) -> Option<F> | |
| 0 | 103 | } |
| 104 | ||
| 5 | 105 | /// Helper trait for iteration of [`Grid`]s. |
| 0 | 106 | pub trait GridIteration<F, I> { |
| 5 | 107 | /// Returns the next multi-dimensional index (not yet converted into grid point). |
| 0 | 108 | fn next_index(&mut self) -> Option<I>; |
| 109 | } | |
| 110 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
111 | impl<F: Float + CastFrom<I>, I: Unsigned> Grid<F, I> for LinSpace<F, I> { |
| 0 | 112 | /*fn entry(&self, i : I) -> Option<F> { |
| 113 | if i < self.count { | |
| 114 | Some(self.entry_unchecked(i)) | |
| 115 | } else { | |
| 116 | None | |
| 117 | } | |
| 118 | }*/ | |
| 119 | ||
| 120 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
121 | fn entry_linear_unchecked(&self, i: usize) -> F { |
| 0 | 122 | self.entry_unchecked(&I::cast_from(i)) |
| 123 | } | |
| 124 | ||
| 125 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
126 | fn entry_unchecked(&self, i: &I) -> F { |
| 0 | 127 | let idx = F::cast_from(*i); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
128 | let scale = F::cast_from(self.count - I::ONE); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
129 | self.start + (self.end - self.start) * idx / scale |
| 0 | 130 | } |
| 131 | } | |
| 132 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
133 | impl<F: Float + CastFrom<I>, I: Unsigned> GridIteration<F, I> for LinSpaceIterator<F, I> { |
| 0 | 134 | #[inline] |
| 135 | fn next_index(&mut self) -> Option<I> { | |
| 136 | match self.current { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
137 | None if I::ZERO < self.lingrid.count => { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
138 | self.current = Some(I::ZERO); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
139 | self.current |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
140 | } |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
141 | Some(v) if v + I::ONE < self.lingrid.count => { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
142 | self.current = Some(v + I::ONE); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
143 | self.current |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
144 | } |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
145 | _ => None, |
| 0 | 146 | } |
| 147 | } | |
| 148 | } | |
| 149 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
150 | impl<F: Float + CastFrom<I>, I: Unsigned, const N: usize> Grid<Loc<N, F>, [I; N]> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
151 | for LinSpace<Loc<N, F>, [I; N]> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
152 | { |
| 0 | 153 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
154 | fn entry_linear_unchecked(&self, i_: usize) -> Loc<N, F> { |
| 0 | 155 | let mut i = I::cast_from(i_); |
| 156 | let mut tmp = [I::ZERO; N]; | |
| 157 | for k in 0..N { | |
| 158 | tmp[k] = i % self.count[k]; | |
| 159 | i /= self.count[k]; | |
| 160 | } | |
| 161 | self.entry_unchecked(&tmp) | |
| 162 | } | |
| 163 | ||
| 164 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
165 | fn entry_unchecked(&self, i: &[I; N]) -> Loc<N, F> { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
166 | let LinSpace { start, end, count } = self; |
| 0 | 167 | map4(i, start, end, count, |&ik, &sk, &ek, &ck| { |
| 168 | let idx = F::cast_from(ik); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
169 | let scale = F::cast_from(ck - I::ONE); |
| 0 | 170 | sk + (ek - sk) * idx / scale |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
171 | }) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
172 | .into() |
| 0 | 173 | } |
| 174 | } | |
| 175 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
176 | impl<F: Float + CastFrom<I>, I: Unsigned, const N: usize> GridIteration<Loc<N, F>, [I; N]> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
177 | for LinSpaceIterator<Loc<N, F>, [I; N]> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
178 | { |
| 0 | 179 | #[inline] |
| 180 | fn next_index(&mut self) -> Option<[I; N]> { | |
| 181 | match self.current { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
182 | None if self.lingrid.count.iter().all(|v| I::ZERO < *v) => { |
| 0 | 183 | self.current = Some([I::ZERO; N]); |
| 184 | self.current | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
185 | } |
| 0 | 186 | Some(ref mut v) => { |
| 187 | for k in 0..N { | |
| 188 | let a = v[k] + I::ONE; | |
| 189 | if a < self.lingrid.count[k] { | |
| 190 | v[k] = a; | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
191 | return self.current; |
| 0 | 192 | } else { |
| 193 | v[k] = I::ZERO; | |
| 194 | } | |
| 195 | } | |
| 196 | None | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
197 | } |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
198 | _ => None, |
| 0 | 199 | } |
| 200 | } | |
| 201 | } | |
| 202 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
203 | impl<F, I> IntoIterator for LinSpace<F, I> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
204 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
205 | LinSpace<F, I>: Grid<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
206 | LinSpaceIterator<F, I>: GridIteration<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
207 | { |
| 0 | 208 | type Item = F; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
209 | type IntoIter = LinSpaceIterator<F, I>; |
| 0 | 210 | |
| 211 | #[inline] | |
| 212 | fn into_iter(self) -> Self::IntoIter { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
213 | LinSpaceIterator { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
214 | lingrid: self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
215 | current: None, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
216 | } |
| 0 | 217 | } |
| 218 | } | |
| 219 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
220 | impl<F, I> Iterator for LinSpaceIterator<F, I> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
221 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
222 | LinSpace<F, I>: Grid<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
223 | LinSpaceIterator<F, I>: GridIteration<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
224 | { |
| 0 | 225 | type Item = F; |
| 226 | #[inline] | |
| 227 | fn next(&mut self) -> Option<F> { | |
| 228 | self.next_index().map(|v| self.lingrid.entry_unchecked(&v)) | |
| 229 | } | |
| 230 | } | |
| 231 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
232 | impl<F, I> StatefulIterator for LinSpaceIterator<F, I> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
233 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
234 | LinSpace<F, I>: Grid<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
235 | LinSpaceIterator<F, I>: GridIteration<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
236 | { |
| 0 | 237 | #[inline] |
| 238 | fn current(&self) -> Option<F> { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
239 | self.current |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
240 | .as_ref() |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
241 | .map(|c| self.lingrid.entry_unchecked(c)) |
| 0 | 242 | } |
| 243 | } | |
| 244 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
245 | impl<F, I> RestartableIterator for LinSpaceIterator<F, I> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
246 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
247 | LinSpace<F, I>: Grid<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
248 | LinSpaceIterator<F, I>: GridIteration<F, I>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
249 | { |
| 0 | 250 | #[inline] |
| 251 | fn restart(&mut self) -> Option<F> { | |
| 252 | self.current = None; | |
| 253 | self.next() | |
| 254 | } | |
| 255 | } |