Struct movavg::MovAvg[][src]

pub struct MovAvg<T, A, const WINDOW_SIZE: usize> { /* fields omitted */ }

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

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

impl<T: Num + NumCast + Copy, A: Num + NumCast + Copy + MovAvgAccu<T>, const WINDOW_SIZE: usize> MovAvg<T, A, WINDOW_SIZE>[src]

pub fn new() -> MovAvg<T, A, WINDOW_SIZE>[src]

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

pub fn new_init(
    buffer: [T; WINDOW_SIZE],
    nr_populated: usize
) -> MovAvg<T, A, WINDOW_SIZE>
[src]

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 reference to preallocated buffer.
                     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);

pub fn try_feed(&mut self, value: T) -> Result<T, &str>[src]

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.

pub fn feed(&mut self, value: T) -> T[src]

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.

pub fn try_get(&self) -> Result<T, &str>[src]

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.

pub fn get(&self) -> T[src]

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.

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.