diff --git a/src/lib.rs b/src/lib.rs index 4a62bbb..9cac62c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ pub enum Interpolator { /// - [`Strategy::LeftNearest`] /// - [`Strategy::RightNearest`] /// - [`Strategy::Nearest`] - /// + /// /// Applicable extrapolation strategies: /// - [`Extrapolate::Enable`] /// - [`Extrapolate::Clamp`] @@ -129,7 +129,7 @@ pub enum Interpolator { /// /// Applicable interpolation strategies: /// - [`Strategy::Linear`] - /// + /// /// Applicable extrapolation strategies: /// - [`Extrapolate::Clamp`] /// - [`Extrapolate::Error`] @@ -164,7 +164,7 @@ pub enum Interpolator { /// /// Applicable interpolation strategies: /// - [`Strategy::Linear`] - /// + /// /// Applicable extrapolation strategies: /// - [`Extrapolate::Clamp`] /// - [`Extrapolate::Error`] @@ -207,7 +207,7 @@ pub enum Interpolator { /// /// Applicable interpolation strategies: /// - [`Strategy::Linear`] - /// + /// /// Applicable extrapolation strategies: /// - [`Extrapolate::Clamp`] /// - [`Extrapolate::Error`] @@ -466,181 +466,237 @@ impl Interpolator { // Getters and setters impl Interpolator { - /// Get `strategy` field + /// Get `strategy` field from any interpolator. pub fn strategy(&self) -> Result<&Strategy, Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.strategy()), - Interpolator::Interp2D(interp) => Ok(interp.strategy()), - Interpolator::Interp3D(interp) => Ok(interp.strategy()), - Interpolator::InterpND(interp) => Ok(interp.strategy()), + Interpolator::Interp1D(interp) => Ok(&interp.strategy), + Interpolator::Interp2D(interp) => Ok(&interp.strategy), + Interpolator::Interp3D(interp) => Ok(&interp.strategy), + Interpolator::InterpND(interp) => Ok(&interp.strategy), _ => Err(Error::NoSuchField), } } - /// Set `strategy` field + /// Set `strategy` field on any interpolator. pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.set_strategy(strategy)?), - Interpolator::Interp2D(interp) => Ok(interp.set_strategy(strategy)?), - Interpolator::Interp3D(interp) => Ok(interp.set_strategy(strategy)?), - Interpolator::InterpND(interp) => Ok(interp.set_strategy(strategy)?), + Interpolator::Interp1D(interp) => { + interp.strategy = strategy; + Ok(interp.validate()?) + } + Interpolator::Interp2D(interp) => { + interp.strategy = strategy; + Ok(interp.validate()?) + } + Interpolator::Interp3D(interp) => { + interp.strategy = strategy; + Ok(interp.validate()?) + } + Interpolator::InterpND(interp) => { + interp.strategy = strategy; + Ok(interp.validate()?) + } _ => return Err(Error::NoSuchField), } } - /// Get `extrapolate` field + /// Get `extrapolate` field from any interpolator. pub fn extrapolate(&self) -> Result<&Extrapolate, Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.extrapolate()), - Interpolator::Interp2D(interp) => Ok(interp.extrapolate()), - Interpolator::Interp3D(interp) => Ok(interp.extrapolate()), - Interpolator::InterpND(interp) => Ok(interp.extrapolate()), + Interpolator::Interp1D(interp) => Ok(&interp.extrapolate), + Interpolator::Interp2D(interp) => Ok(&interp.extrapolate), + Interpolator::Interp3D(interp) => Ok(&interp.extrapolate), + Interpolator::InterpND(interp) => Ok(&interp.extrapolate), _ => Err(Error::NoSuchField), } } - /// Set `extrapolate` field + /// Set `extrapolate` field on any interpolator. pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.set_extrapolate(extrapolate)?), - Interpolator::Interp2D(interp) => Ok(interp.set_extrapolate(extrapolate)?), - Interpolator::Interp3D(interp) => Ok(interp.set_extrapolate(extrapolate)?), - Interpolator::InterpND(interp) => Ok(interp.set_extrapolate(extrapolate)?), + Interpolator::Interp1D(interp) => { + interp.extrapolate = extrapolate; + Ok(interp.validate()?) + } + Interpolator::Interp2D(interp) => { + interp.extrapolate = extrapolate; + Ok(interp.validate()?) + } + Interpolator::Interp3D(interp) => { + interp.extrapolate = extrapolate; + Ok(interp.validate()?) + } + Interpolator::InterpND(interp) => { + interp.extrapolate = extrapolate; + Ok(interp.validate()?) + } _ => return Err(Error::NoSuchField), } } - /// Get `x` field + /// Get `x` field from 1D/2D/3D interpolator. pub fn x(&self) -> Result<&[f64], Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.x()), - Interpolator::Interp2D(interp) => Ok(interp.x()), - Interpolator::Interp3D(interp) => Ok(interp.x()), + Interpolator::Interp1D(interp) => Ok(&interp.x), + Interpolator::Interp2D(interp) => Ok(&interp.x), + Interpolator::Interp3D(interp) => Ok(&interp.x), _ => Err(Error::NoSuchField), } } - /// Set `x` field + /// Set `x` field on 1D/2D/3D interpolator. pub fn set_x(&mut self, x: Vec) -> Result<(), Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.set_x(x)?), - Interpolator::Interp2D(interp) => Ok(interp.set_x(x)?), - Interpolator::Interp3D(interp) => Ok(interp.set_x(x)?), + Interpolator::Interp1D(interp) => { + interp.x = x; + Ok(interp.validate()?) + } + Interpolator::Interp2D(interp) => { + interp.x = x; + Ok(interp.validate()?) + } + Interpolator::Interp3D(interp) => { + interp.x = x; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `y` field + /// Get `y` field from 2D/3D interpolator. pub fn y(&self) -> Result<&[f64], Error> { match self { - Interpolator::Interp2D(interp) => Ok(interp.y()), - Interpolator::Interp3D(interp) => Ok(interp.y()), + Interpolator::Interp2D(interp) => Ok(&interp.y), + Interpolator::Interp3D(interp) => Ok(&interp.y), _ => Err(Error::NoSuchField), } } - /// Set `y` field - pub fn set_y(&mut self, new_y: Vec) -> Result<(), Error> { + /// Set `y` field on 2D/3D interpolator. + pub fn set_y(&mut self, y: Vec) -> Result<(), Error> { match self { - Interpolator::Interp2D(interp) => interp.set_y(new_y)?, - Interpolator::Interp3D(interp) => interp.set_y(new_y)?, + Interpolator::Interp2D(interp) => { + interp.y = y; + Ok(interp.validate()?) + } + Interpolator::Interp3D(interp) => { + interp.y = y; + Ok(interp.validate()?) + } _ => return Err(Error::NoSuchField), } - Ok(()) } - /// Get `z` field + /// Get `z` field from 3D interpolator. pub fn z(&self) -> Result<&[f64], Error> { match self { - Interpolator::Interp3D(interp) => Ok(interp.z()), + Interpolator::Interp3D(interp) => Ok(&interp.z), _ => Err(Error::NoSuchField), } } - /// Set `z` field - pub fn set_z(&mut self, new_z: Vec) -> Result<(), Error> { + /// Set `z` field on 3D interpolator. + pub fn set_z(&mut self, z: Vec) -> Result<(), Error> { match self { - Interpolator::Interp3D(interp) => Ok(interp.set_z(new_z)?), + Interpolator::Interp3D(interp) => { + interp.z = z; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `f_x` field + /// Get `f_x` field from 1D interpolator. pub fn f_x(&self) -> Result<&[f64], Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.f_x()), + Interpolator::Interp1D(interp) => Ok(&interp.f_x), _ => Err(Error::NoSuchField), } } - /// Set `f_x` field + /// Set `f_x` field on 1D interpolator. pub fn set_f_x(&mut self, f_x: Vec) -> Result<(), Error> { match self { - Interpolator::Interp1D(interp) => Ok(interp.set_f_x(f_x)?), + Interpolator::Interp1D(interp) => { + interp.f_x = f_x; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `f_xy` field + /// Get `f_xy` field from 2D interpolator. pub fn f_xy(&self) -> Result<&[Vec], Error> { match self { - Interpolator::Interp2D(interp) => Ok(interp.f_xy()), + Interpolator::Interp2D(interp) => Ok(&interp.f_xy), _ => Err(Error::NoSuchField), } } - /// Set `f_xy` field - pub fn set_f_xy(&mut self, new_f_xy: Vec>) -> Result<(), Error> { + /// Set `f_xy` field on 2D interpolator. + pub fn set_f_xy(&mut self, f_xy: Vec>) -> Result<(), Error> { match self { - Interpolator::Interp2D(interp) => Ok(interp.set_f_xy(new_f_xy)?), + Interpolator::Interp2D(interp) => { + interp.f_xy = f_xy; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `f_xyz` field + /// Get `f_xyz` field from 3D interpolator. pub fn f_xyz(&self) -> Result<&[Vec>], Error> { match self { - Interpolator::Interp3D(interp) => Ok(interp.f_xyz()), + Interpolator::Interp3D(interp) => Ok(&interp.f_xyz), _ => Err(Error::NoSuchField), } } - /// Set `f_xyz` field - pub fn set_f_xyz(&mut self, new_f_xyz: Vec>>) -> Result<(), Error> { + /// Set `f_xyz` field on 3D interpolator. + pub fn set_f_xyz(&mut self, f_xyz: Vec>>) -> Result<(), Error> { match self { - Interpolator::Interp3D(interp) => Ok(interp.set_f_xyz(new_f_xyz)?), + Interpolator::Interp3D(interp) => { + interp.f_xyz = f_xyz; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `grid` field + /// Get `grid` field from ND interpolator. pub fn grid(&self) -> Result<&[Vec], Error> { match self { - Interpolator::InterpND(interp) => Ok(interp.grid()), + Interpolator::InterpND(interp) => Ok(&interp.grid), _ => Err(Error::NoSuchField), } } - /// Set `grid` field - pub fn set_grid(&mut self, new_grid: Vec>) -> Result<(), Error> { + /// Set `grid` field on ND interpolator. + pub fn set_grid(&mut self, grid: Vec>) -> Result<(), Error> { match self { - Interpolator::InterpND(interp) => Ok(interp.set_grid(new_grid)?), + Interpolator::InterpND(interp) => { + interp.grid = grid; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } - /// Get `values` field + /// Get `values` field from ND interpolator. pub fn values(&self) -> Result<&ndarray::ArrayD, Error> { match self { - Interpolator::InterpND(interp) => Ok(interp.values()), + Interpolator::InterpND(interp) => Ok(&interp.values), _ => Err(Error::NoSuchField), } } - /// Set `values` gield - pub fn set_values(&mut self, new_values: ndarray::ArrayD) -> Result<(), Error> { + /// Set `values` field on ND interpolator. + pub fn set_values(&mut self, values: ndarray::ArrayD) -> Result<(), Error> { match self { - Interpolator::InterpND(interp) => Ok(interp.set_values(new_values)?), + Interpolator::InterpND(interp) => { + interp.values = values; + Ok(interp.validate()?) + } _ => Err(Error::NoSuchField), } } diff --git a/src/n.rs b/src/n.rs index e40d290..7890461 100644 --- a/src/n.rs +++ b/src/n.rs @@ -190,52 +190,6 @@ impl InterpMethods for InterpND { } } -// Getters and setters -impl InterpND { - /// Get `strategy` field - pub fn strategy(&self) -> &Strategy { - &self.strategy - } - - /// Set `strategy` field - pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), ValidationError> { - self.strategy = strategy; - self.validate() - } - - /// Get `extrapolate` field - pub fn extrapolate(&self) -> &Extrapolate { - &self.extrapolate - } - - /// Set `extrapolate` field - pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), ValidationError> { - self.extrapolate = extrapolate; - self.validate() - } - - /// Get `grid` field - pub fn grid(&self) -> &[Vec] { - &self.grid - } - - /// Set `grid` field - pub fn set_grid(&mut self, grid: Vec>) -> Result<(), ValidationError> { - self.grid = grid; - self.validate() - } - - /// Get `values` field - pub fn values(&self) -> &ndarray::ArrayD { - &self.values - } - - /// Set `values` field - pub fn set_values(&mut self, values: ndarray::ArrayD) -> Result<(), ValidationError> { - self.values = values; - self.validate() - } -} #[cfg(test)] mod tests { use super::*; diff --git a/src/one.rs b/src/one.rs index 1376c31..99917c4 100644 --- a/src/one.rs +++ b/src/one.rs @@ -119,53 +119,6 @@ impl InterpMethods for Interp1D { } } -// Getters and setters -impl Interp1D { - /// Get `strategy` field - pub fn strategy(&self) -> &Strategy { - &self.strategy - } - - /// Set `strategy` field - pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), ValidationError> { - self.strategy = strategy; - self.validate() - } - - /// Get `extrapolate` field - pub fn extrapolate(&self) -> &Extrapolate { - &self.extrapolate - } - - /// Set `extrapolate` field - pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), ValidationError> { - self.extrapolate = extrapolate; - self.validate() - } - - /// Get `x` field - pub fn x(&self) -> &[f64] { - &self.x - } - - /// Set `x` field - pub fn set_x(&mut self, x: Vec) -> Result<(), ValidationError> { - self.x = x; - self.validate() - } - - /// Get `f_x` field - pub fn f_x(&self) -> &[f64] { - &self.f_x - } - - /// Set `f_x` field - pub fn set_f_x(&mut self, f_x: Vec) -> Result<(), ValidationError> { - self.f_x = f_x; - self.validate() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/three.rs b/src/three.rs index 809317a..74de46c 100644 --- a/src/three.rs +++ b/src/three.rs @@ -119,75 +119,6 @@ impl InterpMethods for Interp3D { } } -// Getters and setters -impl Interp3D { - /// Get `strategy` field - pub fn strategy(&self) -> &Strategy { - &self.strategy - } - - /// Set `strategy` field - pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), ValidationError> { - self.strategy = strategy; - self.validate() - } - - /// Get `extrapolate` field - pub fn extrapolate(&self) -> &Extrapolate { - &self.extrapolate - } - - /// Set `extrapolate` field - pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), ValidationError> { - self.extrapolate = extrapolate; - self.validate() - } - - /// Get `x` field - pub fn x(&self) -> &[f64] { - &self.x - } - - /// Set `x` field - pub fn set_x(&mut self, x: Vec) -> Result<(), ValidationError> { - self.x = x; - self.validate() - } - - /// Get `y` field - pub fn y(&self) -> &[f64] { - &self.y - } - - /// Set `y` field - pub fn set_y(&mut self, y: Vec) -> Result<(), ValidationError> { - self.y = y; - self.validate() - } - - /// Get `z` field - pub fn z(&self) -> &[f64] { - &self.z - } - - /// Set `z` field - pub fn set_z(&mut self, z: Vec) -> Result<(), ValidationError> { - self.z = z; - self.validate() - } - - /// Get `f_xyz` field - pub fn f_xyz(&self) -> &[Vec>] { - &self.f_xyz - } - - /// Set `f_xyz` field - pub fn set_f_xyz(&mut self, f_xyz: Vec>>) -> Result<(), ValidationError> { - self.f_xyz = f_xyz; - self.validate() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/two.rs b/src/two.rs index 49f5faa..9ef31af 100644 --- a/src/two.rs +++ b/src/two.rs @@ -93,64 +93,6 @@ impl InterpMethods for Interp2D { } } -// Getters and setters -impl Interp2D { - /// Get `strategy` field - pub fn strategy(&self) -> &Strategy { - &self.strategy - } - - /// Set `strategy` field - pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), ValidationError> { - self.strategy = strategy; - self.validate() - } - - /// Get `extrapolate` field - pub fn extrapolate(&self) -> &Extrapolate { - &self.extrapolate - } - - /// Set `extrapolate` field - pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), ValidationError> { - self.extrapolate = extrapolate; - self.validate() - } - - /// Get `x` field - pub fn x(&self) -> &[f64] { - &self.x - } - - /// Set `x` field - pub fn set_x(&mut self, x: Vec) -> Result<(), ValidationError> { - self.x = x; - self.validate() - } - - /// Get `y` field - pub fn y(&self) -> &[f64] { - &self.y - } - - /// Set `y` field - pub fn set_y(&mut self, y: Vec) -> Result<(), ValidationError> { - self.y = y; - self.validate() - } - - /// Get `f_xy` field - pub fn f_xy(&self) -> &[Vec] { - &self.f_xy - } - - /// Set `f_xy` field - pub fn set_f_xy(&mut self, f_xy: Vec>) -> Result<(), ValidationError> { - self.f_xy = f_xy; - self.validate() - } -} - #[cfg(test)] mod tests { use super::*;