Skip to content

Commit cf42104

Browse files
committed
Merge branch 'main' into cubic
2 parents d984a22 + e0f5c85 commit cf42104

12 files changed

+95
-89
lines changed

src/data.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct InterpData<D, const N: usize>
2020
where
2121
Dim<[Ix; N]>: Dimension,
2222
D: Data + RawDataClone + Clone,
23-
D::Elem: Num + PartialOrd + Copy + Debug,
23+
D::Elem: PartialEq + Debug,
2424
{
2525
pub grid: [ArrayBase<D, Ix1>; N],
2626
pub values: ArrayBase<D, Dim<[Ix; N]>>,
@@ -32,13 +32,12 @@ impl<D, const N: usize> InterpData<D, N>
3232
where
3333
Dim<[Ix; N]>: Dimension,
3434
D: Data + RawDataClone + Clone,
35-
D::Elem: Num + PartialOrd + Copy + Debug,
35+
D::Elem: PartialOrd + Debug,
3636
{
3737
pub fn validate(&self) -> Result<(), ValidateError> {
3838
for i in 0..N {
3939
let i_grid_len = self.grid[i].len();
4040
// Check that each grid dimension has elements
41-
// Indexing `grid` directly is okay because empty dimensions are caught at compilation
4241
if i_grid_len == 0 {
4342
return Err(ValidateError::EmptyGrid(i));
4443
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ macro_rules! extrapolate_impl {
228228
impl<D, S> $InterpType<D, S>
229229
where
230230
D: Data + RawDataClone + Clone,
231-
D::Elem: Num + PartialOrd + Copy + Debug,
231+
D::Elem: PartialEq + Debug,
232232
S: $Strategy<D> + Clone,
233233
{
234234
/// Set [`Extrapolate`] variant, checking validity.

src/n/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod strategies;
1818
pub struct InterpDataND<D>
1919
where
2020
D: Data + RawDataClone + Clone,
21-
D::Elem: Num + PartialOrd + Copy + Debug,
21+
D::Elem: PartialEq + Debug,
2222
{
2323
pub grid: Vec<ArrayBase<D, Ix1>>,
2424
pub values: ArrayBase<D, IxDyn>,
@@ -31,7 +31,7 @@ pub type InterpDataNDOwned<T> = InterpDataND<ndarray::OwnedRepr<T>>;
3131
impl<D> InterpDataND<D>
3232
where
3333
D: Data + RawDataClone + Clone,
34-
D::Elem: Num + PartialOrd + Copy + Debug,
34+
D::Elem: PartialOrd + Debug,
3535
{
3636
pub fn ndim(&self) -> usize {
3737
if self.values.len() == 1 {
@@ -94,7 +94,7 @@ where
9494
pub struct InterpND<D, S>
9595
where
9696
D: Data + RawDataClone + Clone,
97-
D::Elem: Num + PartialOrd + Copy + Debug,
97+
D::Elem: PartialEq + Debug,
9898
S: StrategyND<D> + Clone,
9999
{
100100
pub data: InterpDataND<D>,
@@ -116,7 +116,7 @@ extrapolate_impl!(InterpND, StrategyND);
116116
impl<D, S> InterpND<D, S>
117117
where
118118
D: Data + RawDataClone + Clone,
119-
D::Elem: Num + PartialOrd + Copy + Debug,
119+
D::Elem: PartialOrd + Debug,
120120
S: StrategyND<D> + Clone,
121121
{
122122
/// Instantiate N-dimensional (any dimensionality) interpolator.
@@ -172,15 +172,15 @@ where
172172
strategy,
173173
extrapolate,
174174
};
175-
interpolator.check_extrapolate(&extrapolate)?;
175+
interpolator.check_extrapolate(&interpolator.extrapolate)?;
176176
Ok(interpolator)
177177
}
178178
}
179179

180180
impl<D, S> Interpolator<D::Elem> for InterpND<D, S>
181181
where
182182
D: Data + RawDataClone + Clone,
183-
D::Elem: Num + PartialOrd + Copy + Debug,
183+
D::Elem: PartialOrd + Debug + Clone,
184184
S: StrategyND<D> + Clone,
185185
{
186186
fn ndim(&self) -> usize {
@@ -205,17 +205,18 @@ where
205205
{
206206
match &self.extrapolate {
207207
Extrapolate::Enable => {}
208-
Extrapolate::Fill(value) => return Ok(*value),
208+
Extrapolate::Fill(value) => return Ok(value.clone()),
209209
Extrapolate::Clamp => {
210210
let clamped_point: Vec<_> = point
211211
.iter()
212212
.enumerate()
213213
.map(|(dim, pt)| {
214-
*clamp(
214+
clamp(
215215
pt,
216216
self.data.grid[dim].first().unwrap(),
217217
self.data.grid[dim].last().unwrap(),
218218
)
219+
.clone()
219220
})
220221
.collect();
221222
return self.strategy.interpolate(&self.data, &clamped_point);
@@ -239,7 +240,7 @@ where
239240
impl<D> InterpND<D, Box<dyn StrategyND<D>>>
240241
where
241242
D: Data + RawDataClone + Clone,
242-
D::Elem: Num + PartialOrd + Copy + Debug,
243+
D::Elem: PartialEq + Debug,
243244
{
244245
/// Update strategy dynamically.
245246
pub fn set_strategy(&mut self, strategy: Box<dyn StrategyND<D>>) -> Result<(), ValidateError> {

src/n/strategies.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ where
6565
} else if &point[dim] > grid[dim].last().unwrap() {
6666
grid[dim].len() - 2
6767
} else {
68-
find_nearest_index(grid[dim].view(), point[dim])
68+
find_nearest_index(grid[dim].view(), &point[dim])
6969
};
7070
let interp_diff = (point[dim] - grid[dim][lower_idx])
7171
/ (grid[dim][lower_idx + 1] - grid[dim][lower_idx]);
@@ -157,7 +157,7 @@ where
157157
let mut lower_idxs = Vec::with_capacity(n);
158158
let mut lower_closers = Vec::with_capacity(n);
159159
for dim in 0..n {
160-
let lower_idx = find_nearest_index(grid[dim].view(), point[dim]);
160+
let lower_idx = find_nearest_index(grid[dim].view(), &point[dim]);
161161
let lower_closer =
162162
point[dim] - grid[dim][lower_idx] < grid[dim][lower_idx + 1] - point[dim];
163163
lower_idxs.push(lower_idx);

src/one/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub type InterpData1DOwned<T> = InterpData1D<ndarray::OwnedRepr<T>>;
1515
impl<D> InterpData1D<D>
1616
where
1717
D: Data + RawDataClone + Clone,
18-
D::Elem: Num + PartialOrd + Copy + Debug,
18+
D::Elem: PartialOrd + Debug,
1919
{
2020
pub fn new(x: ArrayBase<D, Ix1>, f_x: ArrayBase<D, Ix1>) -> Result<Self, ValidateError> {
2121
let data = Self {
@@ -41,7 +41,7 @@ where
4141
pub struct Interp1D<D, S>
4242
where
4343
D: Data + RawDataClone + Clone,
44-
D::Elem: Num + PartialOrd + Copy + Debug,
44+
D::Elem: PartialEq + Debug,
4545
S: Strategy1D<D> + Clone,
4646
{
4747
pub data: InterpData1D<D>,
@@ -63,7 +63,7 @@ extrapolate_impl!(Interp1D, Strategy1D);
6363
impl<D, S> Interp1D<D, S>
6464
where
6565
D: Data + RawDataClone + Clone,
66-
D::Elem: Num + PartialOrd + Copy + Debug,
66+
D::Elem: PartialOrd + Debug,
6767
S: Strategy1D<D> + Clone,
6868
{
6969
/// Instantiate one-dimensional interpolator.
@@ -107,15 +107,15 @@ where
107107
strategy,
108108
extrapolate,
109109
};
110-
interpolator.check_extrapolate(&extrapolate)?;
110+
interpolator.check_extrapolate(&interpolator.extrapolate)?;
111111
Ok(interpolator)
112112
}
113113
}
114114

115115
impl<D, S> Interpolator<D::Elem> for Interp1D<D, S>
116116
where
117117
D: Data + RawDataClone + Clone,
118-
D::Elem: Num + PartialOrd + Copy + Debug,
118+
D::Elem: PartialOrd + Debug + Clone,
119119
S: Strategy1D<D> + Clone,
120120
{
121121
/// Returns `1`.
@@ -136,15 +136,16 @@ where
136136
if !(self.data.grid[0].first().unwrap()..=self.data.grid[0].last().unwrap())
137137
.contains(&&point[0])
138138
{
139-
match self.extrapolate {
139+
match &self.extrapolate {
140140
Extrapolate::Enable => {}
141-
Extrapolate::Fill(value) => return Ok(value),
141+
Extrapolate::Fill(value) => return Ok(value.clone()),
142142
Extrapolate::Clamp => {
143143
let clamped_point = &[clamp(
144-
point[0],
145-
*self.data.grid[0].first().unwrap(),
146-
*self.data.grid[0].last().unwrap(),
147-
)];
144+
&point[0],
145+
self.data.grid[0].first().unwrap(),
146+
self.data.grid[0].last().unwrap(),
147+
)
148+
.clone()];
148149
return self.strategy.interpolate(&self.data, clamped_point);
149150
}
150151
Extrapolate::Error => {
@@ -162,7 +163,7 @@ where
162163
impl<D> Interp1D<D, Box<dyn Strategy1D<D>>>
163164
where
164165
D: Data + RawDataClone + Clone,
165-
D::Elem: Num + PartialOrd + Copy + Debug,
166+
D::Elem: PartialEq + Debug,
166167
{
167168
/// Update strategy dynamically.
168169
pub fn set_strategy(&mut self, strategy: Box<dyn Strategy1D<D>>) -> Result<(), ValidateError> {

src/one/strategies.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
} else if &point[0] > data.grid[0].last().unwrap() {
2424
data.grid[0].len() - 2
2525
} else {
26-
find_nearest_index(data.grid[0].view(), point[0])
26+
find_nearest_index(data.grid[0].view(), &point[0])
2727
};
2828
let x_u = x_l + 1;
2929
let x_diff = (point[0] - data.grid[0][x_l]) / (data.grid[0][x_u] - data.grid[0][x_l]);
@@ -49,7 +49,7 @@ where
4949
if let Some(i) = data.grid[0].iter().position(|&x_val| x_val == point[0]) {
5050
return Ok(data.values[i]);
5151
}
52-
let x_l = find_nearest_index(data.grid[0].view(), point[0]);
52+
let x_l = find_nearest_index(data.grid[0].view(), &point[0]);
5353
let x_u = x_l + 1;
5454
let i = if point[0] - data.grid[0][x_l] < data.grid[0][x_u] - point[0] {
5555
x_l
@@ -78,7 +78,7 @@ where
7878
if let Some(i) = data.grid[0].iter().position(|&x_val| x_val == point[0]) {
7979
return Ok(data.values[i]);
8080
}
81-
let x_l = find_nearest_index(data.grid[0].view(), point[0]);
81+
let x_l = find_nearest_index(data.grid[0].view(), &point[0]);
8282
Ok(data.values[x_l])
8383
}
8484

@@ -101,7 +101,7 @@ where
101101
if let Some(i) = data.grid[0].iter().position(|&x_val| x_val == point[0]) {
102102
return Ok(data.values[i]);
103103
}
104-
let x_u = find_nearest_index(data.grid[0].view(), point[0]) + 1;
104+
let x_u = find_nearest_index(data.grid[0].view(), &point[0]) + 1;
105105
Ok(data.values[x_u])
106106
}
107107

src/strategy.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::*;
55
pub trait Strategy1D<D>: Debug + DynClone
66
where
77
D: Data + RawDataClone + Clone,
8-
D::Elem: Num + PartialOrd + Copy + Debug,
8+
D::Elem: PartialEq + Debug,
99
{
1010
fn interpolate(
1111
&self,
@@ -21,7 +21,7 @@ clone_trait_object!(<D> Strategy1D<D>);
2121
impl<D> Strategy1D<D> for Box<dyn Strategy1D<D>>
2222
where
2323
D: Data + RawDataClone + Clone,
24-
D::Elem: Num + PartialOrd + Copy + Debug,
24+
D::Elem: PartialEq + Debug,
2525
{
2626
fn interpolate(
2727
&self,
@@ -38,7 +38,7 @@ where
3838
pub trait Strategy2D<D>: Debug + DynClone
3939
where
4040
D: Data + RawDataClone + Clone,
41-
D::Elem: Num + PartialOrd + Copy + Debug,
41+
D::Elem: PartialEq + Debug,
4242
{
4343
fn interpolate(
4444
&self,
@@ -54,7 +54,7 @@ clone_trait_object!(<D> Strategy2D<D>);
5454
impl<D> Strategy2D<D> for Box<dyn Strategy2D<D>>
5555
where
5656
D: Data + RawDataClone + Clone,
57-
D::Elem: Num + PartialOrd + Copy + Debug,
57+
D::Elem: PartialEq + Debug,
5858
{
5959
fn interpolate(
6060
&self,
@@ -71,7 +71,7 @@ where
7171
pub trait Strategy3D<D>: Debug + DynClone
7272
where
7373
D: Data + RawDataClone + Clone,
74-
D::Elem: Num + PartialOrd + Copy + Debug,
74+
D::Elem: PartialEq + Debug,
7575
{
7676
fn interpolate(
7777
&self,
@@ -87,7 +87,7 @@ clone_trait_object!(<D> Strategy3D<D>);
8787
impl<D> Strategy3D<D> for Box<dyn Strategy3D<D>>
8888
where
8989
D: Data + RawDataClone + Clone,
90-
D::Elem: Num + PartialOrd + Copy + Debug,
90+
D::Elem: PartialEq + Debug,
9191
{
9292
fn interpolate(
9393
&self,
@@ -104,7 +104,7 @@ where
104104
pub trait StrategyND<D>: Debug + DynClone
105105
where
106106
D: Data + RawDataClone + Clone,
107-
D::Elem: Num + PartialOrd + Copy + Debug,
107+
D::Elem: PartialEq + Debug,
108108
{
109109
fn interpolate(
110110
&self,
@@ -120,7 +120,7 @@ clone_trait_object!(<D> StrategyND<D>);
120120
impl<D> StrategyND<D> for Box<dyn StrategyND<D>>
121121
where
122122
D: Data + RawDataClone + Clone,
123-
D::Elem: Num + PartialOrd + Copy + Debug,
123+
D::Elem: PartialEq + Debug,
124124
{
125125
fn interpolate(
126126
&self,
@@ -137,8 +137,8 @@ where
137137
// This method contains code from RouteE Compass, another open-source NREL-developed tool
138138
// <https://www.nrel.gov/transportation/route-energy-prediction-model.html>
139139
// <https://github.com/NREL/routee-compass/>
140-
pub fn find_nearest_index<T: PartialOrd>(arr: ArrayView1<T>, target: T) -> usize {
141-
if &target == arr.last().unwrap() {
140+
pub fn find_nearest_index<T: PartialOrd>(arr: ArrayView1<T>, target: &T) -> usize {
141+
if target == arr.last().unwrap() {
142142
return arr.len() - 2;
143143
}
144144

@@ -148,14 +148,14 @@ pub fn find_nearest_index<T: PartialOrd>(arr: ArrayView1<T>, target: T) -> usize
148148
while low < high {
149149
let mid = low + (high - low) / 2;
150150

151-
if arr[mid] >= target {
151+
if &arr[mid] >= target {
152152
high = mid;
153153
} else {
154154
low = mid + 1;
155155
}
156156
}
157157

158-
if low > 0 && arr[low] >= target {
158+
if low > 0 && &arr[low] >= target {
159159
low - 1
160160
} else {
161161
low

0 commit comments

Comments
 (0)