Struct emath::History

source ·
pub struct History<T> { /* private fields */ }
Expand description

This struct tracks recent values of some time series.

It can be used as a smoothing filter for e.g. latency, fps etc, or to show a log or graph of recent events.

It has a minimum and maximum length, as well as a maximum storage time.

  • The minimum length is to ensure you have enough data for an estimate.
  • The maximum length is to make sure the history doesn’t take up too much space.
  • The maximum age is to make sure the estimate isn’t outdated.

Time difference between values can be zero, but never negative.

This can be used for things like smoothed averages (for e.g. FPS) or for smoothed velocity (e.g. mouse pointer speed). All times are in seconds.

Implementations§

source§

impl<T> History<T>
where T: Copy,

source

pub fn new(length_range: Range<usize>, max_age: f32) -> Self

Example:

// Drop events that are older than one second,
// as long we keep at least two events. Never keep more than a hundred events.
let mut history = History::new(2..100, 1.0);
assert_eq!(history.average(), None);
history.add(now(), 40.0_f32);
history.add(now(), 44.0_f32);
assert_eq!(history.average(), Some(42.0));
source

pub fn max_len(&self) -> usize

source

pub fn max_age(&self) -> f32

source

pub fn is_empty(&self) -> bool

source

pub fn len(&self) -> usize

Current number of values kept in history

source

pub fn total_count(&self) -> u64

Total number of values seen. Includes those that have been discarded due to max_len or max_age.

source

pub fn latest(&self) -> Option<T>

source

pub fn latest_mut(&mut self) -> Option<&mut T>

source

pub fn duration(&self) -> f32

Amount of time contained from start to end in this History.

source

pub fn iter(&self) -> impl ExactSizeIterator<Item = (f64, T)> + '_

(time, value) pairs Time difference between values can be zero, but never negative.

source

pub fn values(&self) -> impl ExactSizeIterator<Item = T> + '_

source

pub fn clear(&mut self)

source

pub fn add(&mut self, now: f64, value: T)

Values must be added with a monotonically increasing time, or at least not decreasing.

source

pub fn mean_time_interval(&self) -> Option<f32>

Mean time difference between values in this History.

source

pub fn rate(&self) -> Option<f32>

source

pub fn flush(&mut self, now: f64)

Remove samples that are too old.

source§

impl<T> History<T>
where T: Copy + Sum + Div<f32, Output = T>,

source

pub fn sum(&self) -> T

source

pub fn average(&self) -> Option<T>

source§

impl<T> History<T>
where T: Copy + Sum + Div<f32, Output = T> + Mul<f32, Output = T>,

source

pub fn bandwidth(&self) -> Option<T>

Average times rate. If you are keeping track of individual sizes of things (e.g. bytes), this will estimate the bandwidth (bytes per second).

source§

impl<T, Vel> History<T>
where T: Copy + Sub<Output = Vel>, Vel: Div<f32, Output = Vel>,

source

pub fn velocity(&self) -> Option<Vel>

Calculate a smooth velocity (per second) over the entire time span. Calculated as the last value minus the first value over the elapsed time between them.

Trait Implementations§

source§

impl<T: Clone> Clone for History<T>

source§

fn clone(&self) -> History<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for History<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, T> Deserialize<'de> for History<T>
where T: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> Serialize for History<T>
where T: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for History<T>

§

impl<T> RefUnwindSafe for History<T>
where T: RefUnwindSafe,

§

impl<T> Send for History<T>
where T: Send,

§

impl<T> Sync for History<T>
where T: Sync,

§

impl<T> Unpin for History<T>
where T: Unpin,

§

impl<T> UnwindSafe for History<T>
where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,