--- a/src/measures/discrete.rs Fri Apr 28 13:15:19 2023 +0300 +++ b/src/measures/discrete.rs Tue Dec 31 09:34:24 2024 -0500 @@ -59,6 +59,20 @@ self.spikes.len() } + /// Replace with the zero measure. + #[inline] + pub fn clear(&mut self) { + self.spikes.clear() + } + + /// Remove `i`:th spike, not maintaining order. + /// + /// Panics if indiex is out of bounds. + #[inline] + pub fn swap_remove(&mut self, i : usize) -> DeltaMeasure<Domain, F>{ + self.spikes.swap_remove(i) + } + /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure #[inline] pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> { @@ -109,6 +123,31 @@ pub fn prune(&mut self) { self.spikes.retain(|δ| δ.α != F::ZERO); } + + /// Add the spikes produced by `iter` to this measure. + #[inline] + pub fn extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( + &mut self, + iter : I + ) { + self.spikes.extend(iter); + } + + /// Add a spike to the measure + #[inline] + pub fn push(&mut self, δ : DeltaMeasure<Domain, F>) { + self.spikes.push(δ); + } +} + +impl<Domain, F : Num> IntoIterator for DiscreteMeasure<Domain, F> { + type Item = DeltaMeasure<Domain, F>; + type IntoIter = <Vec<DeltaMeasure<Domain, F>> as IntoIterator>::IntoIter; + + #[inline] + fn into_iter(self) -> Self::IntoIter { + self.spikes.into_iter() + } } impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { @@ -119,7 +158,7 @@ pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) { let mut μ2_get = 0; let mut μ2_insert = 0; - self.spikes.drain_filter(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| { + self.spikes.retain_mut(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| { // Get weight of spike in μ2, zero if out of bounds. let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass); μ2_get += 1; @@ -176,21 +215,47 @@ } } -impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> { - type Output = DeltaMeasure<Domain, F>; +// impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> { +// type Output = DeltaMeasure<Domain, F>; +// #[inline] +// fn index(&self, i : usize) -> &Self::Output { +// self.spikes.index(i) +// } +// } + +// impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> { +// #[inline] +// fn index_mut(&mut self, i : usize) -> &mut Self::Output { +// self.spikes.index_mut(i) +// } +// } + +impl< + Domain, + F : Num, + I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]> +> Index<I> +for DiscreteMeasure<Domain, F> { + type Output = <I as std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>>::Output; #[inline] - fn index(&self, i : usize) -> &Self::Output { + fn index(&self, i : I) -> &Self::Output { self.spikes.index(i) } } -impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> { +impl< + Domain, + F : Num, + I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]> +> IndexMut<I> +for DiscreteMeasure<Domain, F> { #[inline] - fn index_mut(&mut self, i : usize) -> &mut Self::Output { + fn index_mut(&mut self, i : I) -> &mut Self::Output { self.spikes.index_mut(i) } } + impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]> for DiscreteMeasure<Domain, F> { #[inline]