Struct movavg::MovAvg

source ·
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 the feed() input value.
  • A - The type of the internal accumulator. This type must be bigger then or equal to T.
  • 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>,

source

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);
source

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 in buffer. nr_populated must be less than or equal to buffer.len(). The populated values in buffer must begin at index 0. The values of unpopulated elements in buffer does not matter.
Panics

Panics, if:

  • nr_populated is bigger than buffer.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);
source

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.

source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl<A, T, const WINDOW_SIZE: usize> Default for MovAvg<T, A, WINDOW_SIZE>where T: Num + NumCast + Copy, A: Num + NumCast + Copy + MovAvgAccu<T>,

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, A, const WINDOW_SIZE: usize> RefUnwindSafe for MovAvg<T, A, WINDOW_SIZE>where A: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, A, const WINDOW_SIZE: usize> Send for MovAvg<T, A, WINDOW_SIZE>where A: Send, T: Send,

§

impl<T, A, const WINDOW_SIZE: usize> Sync for MovAvg<T, A, WINDOW_SIZE>where A: Sync, T: Sync,

§

impl<T, A, const WINDOW_SIZE: usize> Unpin for MovAvg<T, A, WINDOW_SIZE>where A: Unpin, T: Unpin,

§

impl<T, A, const WINDOW_SIZE: usize> UnwindSafe for MovAvg<T, A, WINDOW_SIZE>where A: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.