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,
impl<T> History<T>where
T: Copy,
Sourcepub fn new(length_range: Range<usize>, max_age: f32) -> Self
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));
pub fn max_len(&self) -> usize
pub fn max_age(&self) -> f32
pub fn is_empty(&self) -> bool
Sourcepub fn total_count(&self) -> u64
pub fn total_count(&self) -> u64
Total number of values seen.
Includes those that have been discarded due to max_len
or max_age
.
pub fn latest(&self) -> Option<T>
pub fn latest_mut(&mut self) -> Option<&mut T>
Sourcepub fn iter(&self) -> impl ExactSizeIterator<Item = (f64, T)> + '_
pub fn iter(&self) -> impl ExactSizeIterator<Item = (f64, T)> + '_
(time, value)
pairs
Time difference between values can be zero, but never negative.
pub fn values(&self) -> impl ExactSizeIterator<Item = T> + '_
pub fn clear(&mut self)
Sourcepub fn add(&mut self, now: f64, value: T)
pub fn add(&mut self, now: f64, value: T)
Values must be added with a monotonically increasing time, or at least not decreasing.
Sourcepub fn mean_time_interval(&self) -> Option<f32>
pub fn mean_time_interval(&self) -> Option<f32>
Mean time difference between values in this History
.