pub struct MovAvg<T, A, const WINDOW_SIZE: usize> { /* private fields */ }
Expand description
Simple Moving Average (SMA)
Examples
use movavg::MovAvg;
let mut avg: MovAvg<i32, i32, 3> = MovAvg::new(); // window size = 3
assert_eq!(avg.feed(10), 10);
assert_eq!(avg.feed(20), 15);
assert_eq!(avg.feed(30), 20);
assert_eq!(avg.feed(40), 30);
assert_eq!(avg.get(), 30);
MovAvg
also implements Default
:
use movavg::MovAvg;
let mut avg: MovAvg<i32, i32, 3> = Default::default();
assert_eq!(avg.feed(10), 10);
Type Generics
struct MovAvg<T, A, WINDOW_SIZE>
T
- The type of thefeed()
input value.A
- The type of the internal accumulator. This type must be bigger then or equal toT
.WINDOW_SIZE
- The size of the sliding window. In number of fed elements.
Implementations§
source§impl<T, A, const WINDOW_SIZE: usize> MovAvg<T, A, WINDOW_SIZE>where
T: Num + NumCast + Copy,
A: Num + NumCast + Copy + MovAvgAccu<T>,
impl<T, A, const WINDOW_SIZE: usize> MovAvg<T, A, WINDOW_SIZE>where T: Num + NumCast + Copy, A: Num + NumCast + Copy + MovAvgAccu<T>,
sourcepub fn new() -> MovAvg<T, A, WINDOW_SIZE>
pub fn new() -> MovAvg<T, A, WINDOW_SIZE>
Construct a new Simple Moving Average.
The internal accumulator defaults to zero.
Examples
use movavg::MovAvg;
let mut avg: MovAvg<i32, i32, 3> = MovAvg::new(); // window size = 3
assert_eq!(avg.feed(10), 10);
sourcepub fn new_init(
buffer: [T; WINDOW_SIZE],
nr_populated: usize
) -> MovAvg<T, A, WINDOW_SIZE>
pub fn new_init( buffer: [T; WINDOW_SIZE], nr_populated: usize ) -> MovAvg<T, A, WINDOW_SIZE>
Construct a new Simple Moving Average from a pre-allocated buffer and initialize its internal state.
buffer
- (Partially) pre-populated window buffer. Contains the window values. The length of this buffer defines the Moving Average window size.nr_populated
- The number of pre-populated Moving Average window elements inbuffer
.nr_populated
must be less than or equal tobuffer.len()
. The populated values inbuffer
must begin at index 0. The values of unpopulated elements inbuffer
does not matter.
Panics
Panics, if:
nr_populated
is bigger thanbuffer.len()
.- The initial accumulator calculation fails. (e.g. due to overflow).
Examples
use movavg::MovAvg;
let mut buf = [10, 20, 30, // populated
0, 0]; // unpopulated
let mut avg: MovAvg<i32, i32, 5> =
MovAvg::new_init(buf, // Pass buffer ownership.
3); // The first three elements of buf are pre-populated.
assert_eq!(avg.get(), 20);
assert_eq!(avg.feed(60), 30);
assert_eq!(avg.feed(30), 30);
assert_eq!(avg.feed(60), 40);
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the Moving Average.
This resets the accumulator and the number of accumulated items to 0, as if this instance was re-created with MovAvg::new.
Note
This does not actually overwrite the buffered items in the internal buffer.
sourcepub fn try_feed(&mut self, value: T) -> Result<T, &str>
pub fn try_feed(&mut self, value: T) -> Result<T, &str>
Try to feed a new value into the Moving Average and return the new average.
value
- The new value to feed into the Moving Average.
On success, returns Ok(T)
with the new Moving Average result.
Returns Err
, if the internal accumulator overflows, or if any value conversion fails.
Value conversion does not fail, if the types are big enough to hold the values.
sourcepub fn feed(&mut self, value: T) -> T
pub fn feed(&mut self, value: T) -> T
Feed a new value into the Moving Average and return the new average.
value
- The new value to feed into the Moving Average.
Returns the new Moving Average result.
Panics
Panics, if the internal accumulator overflows, or if any value conversion fails. Value conversion does not fail, if the types are big enough to hold the values.
sourcepub fn try_get(&self) -> Result<T, &str>
pub fn try_get(&self) -> Result<T, &str>
Try to get the current Moving Average value. This method does not modify the internal state.
Returns Err
, if the internal state is empty.
That is if no values have been fed into MovAvg.
Returns Err
, if any value conversion fails.
Value conversion does not fail, if the types are big enough to hold the values.
sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Get the current Moving Average value. This method does not modify the internal state.
Panics
Panics, if the internal state is empty. That is if no values have been fed into MovAvg.
Panics, if any value conversion fails. Value conversion does not fail, if the types are big enough to hold the values.