diff --git a/Cargo.toml b/Cargo.toml index 3dae0ce..577627f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "splines" -version = "1.1.0" +version = "1.2.0" license = "BSD-3-Clause" authors = ["Dimitri Sabadie "] description = "Spline interpolation made easy" diff --git a/src/spline.rs b/src/spline.rs index 1e20e95..6a1665e 100644 --- a/src/spline.rs +++ b/src/spline.rs @@ -199,6 +199,54 @@ impl Spline { Some(self.0.remove(index)) } } + + /// Update a key and return the key already present. + /// + /// The key is updated — if present — with the provided function. + /// + /// # Notes + /// + /// That function makes sense only if you want to change the interpolator (i.e. [`Key::t`]) of + /// your key. If you just want to change the interpolation mode or the carried value, consider + /// using the [`Spline::get_mut`] method instead as it will be way faster. + pub fn replace( + &mut self, + index: usize, + f: F + ) -> Option> + where + F: FnOnce(&Key) -> Key, + T: PartialOrd + { + let key = self.remove(index)?; + self.add(f(&key)); + Some(key) + } + + /// Get a key at a given index. + pub fn get(&self, index: usize) -> Option<&Key> { + self.0.get(index) + } + + /// Mutably get a key at a given index. + pub fn get_mut(&mut self, index: usize) -> Option> { + self.0.get_mut(index).map(|key| KeyMut { + value: &mut key.value, + interpolation: &mut key.interpolation + }) + } +} + +/// A mutable [`Key`]. +/// +/// Mutable keys allow to edit the carried values and the interpolation mode but not the actual +/// interpolator value as it would invalidate the internal structure of the [`Spline`]. If you +/// want to achieve this, you’re advised to use [`Spline::replace`]. +pub struct KeyMut<'a, T, V> { + /// Carried value. + pub value: &'a mut V, + /// Interpolation mode to use for that key. + pub interpolation: &'a mut Interpolation, } // Normalize a time ([0;1]) given two control points.