Add key getters (immutable & mutable).
This commit is contained in:
parent
03031a1e92
commit
7dbc85a312
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "splines"
|
||||
version = "1.1.0"
|
||||
version = "1.2.0"
|
||||
license = "BSD-3-Clause"
|
||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
||||
description = "Spline interpolation made easy"
|
||||
|
@ -199,6 +199,54 @@ impl<T, V> Spline<T, V> {
|
||||
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<F>(
|
||||
&mut self,
|
||||
index: usize,
|
||||
f: F
|
||||
) -> Option<Key<T, V>>
|
||||
where
|
||||
F: FnOnce(&Key<T, V>) -> Key<T, V>,
|
||||
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<T, V>> {
|
||||
self.0.get(index)
|
||||
}
|
||||
|
||||
/// Mutably get a key at a given index.
|
||||
pub fn get_mut(&mut self, index: usize) -> Option<KeyMut<T, V>> {
|
||||
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<T, V>,
|
||||
}
|
||||
|
||||
// Normalize a time ([0;1]) given two control points.
|
||||
|
Loading…
Reference in New Issue
Block a user