pub struct SingleSumSMA<T, D = T> {
window: VecDeque<T>,
window_size: usize,
sum: T,
_div: PhantomData<D>,
}Expand description
Basic implementation of Simple Moving Average (SMA).
The maximum window size is bound by 2^32 - 1.
Useful mainly for integer types, as it does accumulate floating point error with each sample.
Requires O(N) of memory and O(1) for average computation, N being window size.
The divisor argument D is used only for such types T that do not implement From<u32> (such as Duration,…).
Fields§
§window: VecDeque<T>§window_size: usize§sum: T§_div: PhantomData<D>Implementations§
Source§impl<T, D> SingleSumSMA<T, D>
impl<T, D> SingleSumSMA<T, D>
Sourcepub fn new(window_size: usize) -> Self
pub fn new(window_size: usize) -> Self
Creates an empty SMA instance with the given window size. The maximum window size is u32::MAX and must be greater than 1.
Sourcepub fn new_with_samples<I>(window_size: usize, initial_samples: I) -> Selfwhere
I: IntoIterator<Item = T>,
pub fn new_with_samples<I>(window_size: usize, initial_samples: I) -> Selfwhere
I: IntoIterator<Item = T>,
Creates SMA instance given window size and some initial samples.
Trait Implementations§
Source§impl<T: Clone, D: Clone> Clone for SingleSumSMA<T, D>
impl<T: Clone, D: Clone> Clone for SingleSumSMA<T, D>
Source§fn clone(&self) -> SingleSumSMA<T, D>
fn clone(&self) -> SingleSumSMA<T, D>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T, D> Display for SingleSumSMA<T, D>
impl<T, D> Display for SingleSumSMA<T, D>
Source§impl<T, D> SMA<T> for SingleSumSMA<T, D>
impl<T, D> SMA<T> for SingleSumSMA<T, D>
Source§fn average(&self) -> Option<T>
fn average(&self) -> Option<T>
Calculates the moving average value.
Returns
None if no samples were added.Source§fn window_size(&self) -> usize
fn window_size(&self) -> usize
Returns the window size.
Source§fn len(&self) -> usize
fn len(&self) -> usize
Returns the number of elements in the window.
This value is always between 0 and
window_size().Source§fn is_window_full(&self) -> bool
fn is_window_full(&self) -> bool
Indicates whether the window is fully occupied
with samples.
impl<T, D> StructuralPartialEq for SingleSumSMA<T, D>
Auto Trait Implementations§
impl<T, D> Freeze for SingleSumSMA<T, D>where
T: Freeze,
impl<T, D> RefUnwindSafe for SingleSumSMA<T, D>where
T: RefUnwindSafe,
D: RefUnwindSafe,
impl<T, D> Send for SingleSumSMA<T, D>
impl<T, D> Sync for SingleSumSMA<T, D>
impl<T, D> Unpin for SingleSumSMA<T, D>
impl<T, D> UnwindSafe for SingleSumSMA<T, D>where
T: UnwindSafe,
D: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.