emath

Struct 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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>,