Add support for removing a key. #24

This commit is contained in:
Dimitri Sabadie
2019-09-21 14:17:56 +02:00
parent c818b4c810
commit c98b493993
4 changed files with 47 additions and 2 deletions

View File

@ -5,7 +5,7 @@
/// Available kind of interpolations.
///
/// Feel free to visit each variant for more documentation.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub enum Interpolation<T> {

View File

@ -17,7 +17,7 @@ use crate::interpolation::Interpolation;
/// key and the next one if existing. Have a look at [`Interpolation`] for further details.
///
/// [`Interpolation`]: crate::interpolation::Interpolation
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub struct Key<T, V> {

View File

@ -52,6 +52,18 @@ impl<T, V> Spline<T, V> {
&self.0
}
/// Number of keys.
#[inline(always)]
pub fn len(&self) -> usize {
self.0.len()
}
/// Check whether the spline has no key.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Sample a spline at a given time.
///
/// The current implementation, based on immutability, cannot perform in constant time. This means
@ -146,6 +158,15 @@ impl<T, V> Spline<T, V> {
}
})
}
/// Remove a ke from the spline.
pub fn remove(&mut self, index: usize) -> Option<Key<T, V>> {
if index >= self.0.len() {
None
} else {
Some(self.0.remove(index))
}
}
}
// Normalize a time ([0;1]) given two control points.