Skip to content

Commit 5f18568

Browse files
committed
bug fix for InterpDataOwned, implement into_owned
1 parent de778e6 commit 5f18568

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ninterp"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
edition = "2021"
55
description = "Numerical interpolation for N-dimensional rectilinear grids"
66
repository = "https://github.com/NREL/ninterp"

src/interpolator/data.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
/// [`InterpData`] that views data.
4646
pub type InterpDataViewed<T, const N: usize> = InterpData<ViewRepr<T>, N>;
4747
/// [`InterpData`] that owns data.
48-
pub type InterpDataOwned<T, const N: usize> = InterpData<ViewRepr<T>, N>;
48+
pub type InterpDataOwned<T, const N: usize> = InterpData<OwnedRepr<T>, N>;
4949

5050
impl<D, const N: usize> PartialEq for InterpData<D, N>
5151
where
@@ -90,9 +90,21 @@ where
9090

9191
/// View interpolator data.
9292
pub fn view(&self) -> InterpDataViewed<&D::Elem, N> {
93-
InterpData {
93+
InterpDataViewed {
9494
grid: std::array::from_fn(|i| self.grid[i].view()),
9595
values: self.values.view(),
9696
}
9797
}
98+
99+
/// Turn the data into an [`InterpDataOwned`], cloning the array elements if necessary.
100+
pub fn into_owned(self) -> InterpDataOwned<D::Elem, N>
101+
where
102+
Dim<[Ix; N]>: Dimension,
103+
D::Elem: Clone,
104+
{
105+
InterpDataOwned {
106+
grid: self.grid.map(|arr| arr.into_owned()),
107+
values: self.values.into_owned(),
108+
}
109+
}
98110
}

src/interpolator/enums.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ where
189189
extrapolate,
190190
)?))
191191
}
192+
193+
/// Return an interpolator with viewed data.
194+
pub fn view(&self) -> InterpolatorEnumViewed<&D::Elem> {
195+
match self {
196+
InterpolatorEnum::Interp0D(interp) => InterpolatorEnum::Interp0D(interp.clone()),
197+
InterpolatorEnum::Interp1D(interp) => InterpolatorEnum::Interp1D(interp.view()),
198+
InterpolatorEnum::Interp2D(interp) => InterpolatorEnum::Interp2D(interp.view()),
199+
InterpolatorEnum::Interp3D(interp) => InterpolatorEnum::Interp3D(interp.view()),
200+
InterpolatorEnum::InterpND(interp) => InterpolatorEnum::InterpND(interp.view()),
201+
}
202+
}
203+
204+
/// Turn the interpolator into an [`InterpolatorEnumOwned`], cloning the array elements if necessary.
205+
pub fn into_owned(self) -> InterpolatorEnumOwned<D::Elem>
206+
where
207+
D::Elem: Clone,
208+
{
209+
match self {
210+
InterpolatorEnum::Interp0D(interp) => InterpolatorEnum::Interp0D(interp.clone()),
211+
InterpolatorEnum::Interp1D(interp) => InterpolatorEnum::Interp1D(interp.into_owned()),
212+
InterpolatorEnum::Interp2D(interp) => InterpolatorEnum::Interp2D(interp.into_owned()),
213+
InterpolatorEnum::Interp3D(interp) => InterpolatorEnum::Interp3D(interp.into_owned()),
214+
InterpolatorEnum::InterpND(interp) => InterpolatorEnum::InterpND(interp.into_owned()),
215+
}
216+
}
192217
}
193218

194219
impl<D> Interpolator<D::Elem> for InterpolatorEnum<D>

src/interpolator/n/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,22 @@ where
111111

112112
/// View interpolator data.
113113
pub fn view(&self) -> InterpDataNDViewed<&D::Elem> {
114-
InterpDataND {
114+
InterpDataNDViewed {
115115
grid: self.grid.iter().map(|g| g.view()).collect(),
116116
values: self.values.view(),
117117
}
118118
}
119+
120+
/// Turn the data into an [`InterpDataNDOwned`], cloning the array elements if necessary.
121+
pub fn into_owned(self) -> InterpDataNDOwned<D::Elem>
122+
where
123+
D::Elem: Clone,
124+
{
125+
InterpDataNDOwned {
126+
grid: self.grid.into_iter().map(|g| g.into_owned()).collect(),
127+
values: self.values.into_owned(),
128+
}
129+
}
119130
}
120131

121132
/// N-D interpolator
@@ -233,12 +244,25 @@ where
233244
S: for<'a> StrategyND<ViewRepr<&'a D::Elem>>,
234245
D::Elem: Clone,
235246
{
236-
InterpND {
247+
InterpNDViewed {
237248
data: self.data.view(),
238249
strategy: self.strategy.clone(),
239250
extrapolate: self.extrapolate.clone(),
240251
}
241252
}
253+
254+
/// Turn the interpolator into an [`InterpNDOwned`], cloning the array elements if necessary.
255+
pub fn into_owned(self) -> InterpNDOwned<D::Elem, S>
256+
where
257+
S: StrategyND<OwnedRepr<D::Elem>>,
258+
D::Elem: Clone,
259+
{
260+
InterpNDOwned {
261+
data: self.data.into_owned(),
262+
strategy: self.strategy.clone(),
263+
extrapolate: self.extrapolate.clone(),
264+
}
265+
}
242266
}
243267

244268
impl<D, S> Interpolator<D::Elem> for InterpND<D, S>

src/interpolator/one/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,25 @@ where
129129
S: for<'a> Strategy1D<ViewRepr<&'a D::Elem>>,
130130
D::Elem: Clone,
131131
{
132-
Interp1D {
132+
Interp1DViewed {
133133
data: self.data.view(),
134134
strategy: self.strategy.clone(),
135135
extrapolate: self.extrapolate.clone(),
136136
}
137137
}
138+
139+
/// Turn the interpolator into an [`Interp1DOwned`], cloning the array elements if necessary.
140+
pub fn into_owned(self) -> Interp1DOwned<D::Elem, S>
141+
where
142+
S: Strategy1D<OwnedRepr<D::Elem>>,
143+
D::Elem: Clone,
144+
{
145+
Interp1DOwned {
146+
data: self.data.into_owned(),
147+
strategy: self.strategy.clone(),
148+
extrapolate: self.extrapolate.clone(),
149+
}
150+
}
138151
}
139152

140153
impl<D, S> Interpolator<D::Elem> for Interp1D<D, S>

src/interpolator/three/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,25 @@ where
150150
S: for<'a> Strategy3D<ViewRepr<&'a D::Elem>>,
151151
D::Elem: Clone,
152152
{
153-
Interp3D {
153+
Interp3DViewed {
154154
data: self.data.view(),
155155
strategy: self.strategy.clone(),
156156
extrapolate: self.extrapolate.clone(),
157157
}
158158
}
159+
160+
/// Turn the interpolator into an [`Interp3DOwned`], cloning the array elements if necessary.
161+
pub fn into_owned(self) -> Interp3DOwned<D::Elem, S>
162+
where
163+
S: Strategy3D<OwnedRepr<D::Elem>>,
164+
D::Elem: Clone,
165+
{
166+
Interp3DOwned {
167+
data: self.data.into_owned(),
168+
strategy: self.strategy.clone(),
169+
extrapolate: self.extrapolate.clone(),
170+
}
171+
}
159172
}
160173

161174
impl<D, S> Interpolator<D::Elem> for Interp3D<D, S>

src/interpolator/two/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,25 @@ where
138138
S: for<'a> Strategy2D<ViewRepr<&'a D::Elem>>,
139139
D::Elem: Clone,
140140
{
141-
Interp2D {
141+
Interp2DViewed {
142142
data: self.data.view(),
143143
strategy: self.strategy.clone(),
144144
extrapolate: self.extrapolate.clone(),
145145
}
146146
}
147+
148+
/// Turn the interpolator into an [`Interp2DOwned`], cloning the array elements if necessary.
149+
pub fn into_owned(self) -> Interp2DOwned<D::Elem, S>
150+
where
151+
S: Strategy2D<OwnedRepr<D::Elem>>,
152+
D::Elem: Clone,
153+
{
154+
Interp2DOwned {
155+
data: self.data.into_owned(),
156+
strategy: self.strategy.clone(),
157+
extrapolate: self.extrapolate.clone(),
158+
}
159+
}
147160
}
148161

149162
impl<D, S> Interpolator<D::Elem> for Interp2D<D, S>

0 commit comments

Comments
 (0)