pub struct History<T> {
min_len: usize,
max_len: usize,
max_age: f32,
total_count: u64,
values: VecDeque<(f64, T)>,
}
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.
Fields§
§min_len: usize
In elements, i.e. of values.len()
.
The length is initially zero, but once past min_len
will not shrink below it.
max_len: usize
In elements, i.e. of values.len()
.
max_age: f32
In seconds.
total_count: u64
Total number of elements seen ever
values: VecDeque<(f64, T)>
(time, value) pairs, oldest front, newest back. Time difference between values can be zero, but never negative.
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
.
pub fn rate(&self) -> Option<f32>
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)