Add support for removing a key. #24
This commit is contained in:
		| @@ -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> { | ||||
|   | ||||
| @@ -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> { | ||||
|   | ||||
| @@ -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. | ||||
|   | ||||
							
								
								
									
										24
									
								
								tests/mod.rs
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								tests/mod.rs
									
									
									
									
									
								
							| @@ -172,3 +172,27 @@ fn nalgebra_vector_interpolation() { | ||||
|   assert_eq!(Interpolate::lerp(start, end, 1.0), end); | ||||
|   assert_eq!(Interpolate::lerp(start, end, 0.5), mid); | ||||
| } | ||||
|  | ||||
| #[test] | ||||
| fn remove_element_empty() { | ||||
|   let mut spline: Spline<f32, f32> = Spline::from_vec(vec![]); | ||||
|   let removed = spline.remove(0); | ||||
|  | ||||
|   assert_eq!(removed, None); | ||||
|   assert!(spline.is_empty()); | ||||
| } | ||||
|  | ||||
| #[test] | ||||
| fn remove_element() { | ||||
|   let start = Key::new(0., 0., Interpolation::Step(0.5)); | ||||
|   let k1 = Key::new(1., 5., Interpolation::Linear); | ||||
|   let k2 = Key::new(2., 0., Interpolation::Step(0.1)); | ||||
|   let k3 = Key::new(3., 1., Interpolation::Linear); | ||||
|   let k4 = Key::new(10., 2., Interpolation::Linear); | ||||
|   let end = Key::new(11., 4., Interpolation::default()); | ||||
|   let mut spline = Spline::from_vec(vec![start, k1, k2.clone(), k3, k4, end]); | ||||
|   let removed = spline.remove(2); | ||||
|  | ||||
|   assert_eq!(removed, Some(k2)); | ||||
|   assert_eq!(spline.len(), 5); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user