pub struct MinMax<T> { /* private fields */ }Expand description
A commutative data structure for tracking minimum and maximum values and detecting sort order in a stream of data.
Implementations§
Source§impl<T: PartialOrd + Clone> MinMax<T>
impl<T: PartialOrd + Clone> MinMax<T>
Sourcepub fn add(&mut self, sample: T)
pub fn add(&mut self, sample: T)
Add a sample to the data and track min/max, the sort order & “sortiness”.
Sourcepub fn add_ref(&mut self, sample: &T)
pub fn add_ref(&mut self, sample: &T)
Add a sample by reference, only cloning when necessary to update min, max, first_value, or last_value.
This is more efficient than add() when the caller has a reference
and most samples don’t update min/max, because it avoids the upfront
allocation that add() requires from the caller.
For last_value, the existing allocation is reused when possible
by clearing and cloning into it rather than replacing.
Sourcepub const fn min(&self) -> Option<&T>
pub const fn min(&self) -> Option<&T>
Returns the minimum of the data set.
None is returned if and only if the number of samples is 0.
Sourcepub const fn max(&self) -> Option<&T>
pub const fn max(&self) -> Option<&T>
Returns the maximum of the data set.
None is returned if and only if the number of samples is 0.
Sourcepub fn sort_order(&self) -> SortOrder
pub fn sort_order(&self) -> SortOrder
Returns the current sort order of the data.
Sourcepub fn sortiness(&self) -> f64
pub fn sortiness(&self) -> f64
Calculates a “sortiness” score for the data, indicating how close it is to being sorted.
Returns a value between -1.0 and 1.0:
- 1.0 indicates perfectly ascending order
- -1.0 indicates perfectly descending order
- Values in between indicate the general tendency towards ascending or descending order
- 0.0 indicates either no clear ordering or empty/single-element collections
§Examples
use stats::MinMax;
let mut asc: MinMax<i32> = vec![1, 2, 3, 4, 5].into_iter().collect();
assert_eq!(asc.sortiness(), 1.0);
let mut desc: MinMax<i32> = vec![5, 4, 3, 2, 1].into_iter().collect();
assert_eq!(desc.sortiness(), -1.0);
let mut mostly_asc: MinMax<i32> = vec![1, 2, 4, 3, 5].into_iter().collect();
assert!(mostly_asc.sortiness() > 0.0); // Positive but less than 1.0Source§impl MinMax<Vec<u8>>
impl MinMax<Vec<u8>>
Sourcepub fn add_bytes(&mut self, sample: &[u8])
pub fn add_bytes(&mut self, sample: &[u8])
Add a byte slice sample, avoiding heap allocation when the value doesn’t
update min, max, or first_value. Only last_value is always updated
(reusing its existing allocation via clone_from).
This is significantly more efficient than add(sample.to_vec()) for large
datasets where most values don’t update min/max — avoiding ~99% of
allocations in the common case.
Trait Implementations§
Source§impl<T: PartialOrd> Default for MinMax<T>
impl<T: PartialOrd> Default for MinMax<T>
Source§impl<'de, T> Deserialize<'de> for MinMax<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for MinMax<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: PartialOrd + Clone> Extend<T> for MinMax<T>
impl<T: PartialOrd + Clone> Extend<T> for MinMax<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: PartialOrd + Clone> FromIterator<T> for MinMax<T>
impl<T: PartialOrd + Clone> FromIterator<T> for MinMax<T>
impl<T: Copy> Copy for MinMax<T>
impl<T: Eq> Eq for MinMax<T>
impl<T> StructuralPartialEq for MinMax<T>
Auto Trait Implementations§
impl<T> Freeze for MinMax<T>where
T: Freeze,
impl<T> RefUnwindSafe for MinMax<T>where
T: RefUnwindSafe,
impl<T> Send for MinMax<T>where
T: Send,
impl<T> Sync for MinMax<T>where
T: Sync,
impl<T> Unpin for MinMax<T>where
T: Unpin,
impl<T> UnsafeUnpin for MinMax<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for MinMax<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§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more