pub struct Unsorted<T> { /* private fields */ }Expand description
A commutative data structure for lazily sorted sequences of data.
The sort does not occur until statistics need to be computed.
Note that this works on types that do not define a total ordering like
f32 and f64. When an ordering is not defined, an arbitrary order
is returned.
Implementations§
Source§impl<T: PartialOrd> Unsorted<T>
impl<T: PartialOrd> Unsorted<T>
pub const fn is_empty(&self) -> bool
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink capacity to fit current data
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create with specific capacity
Sourcepub fn push_ascending(&mut self, value: T)
pub fn push_ascending(&mut self, value: T)
Add a value assuming it’s greater than all existing values
Source§impl<T: PartialOrd + PartialEq + Clone> Unsorted<T>
impl<T: PartialOrd + PartialEq + Clone> Unsorted<T>
Sourcepub fn cardinality(&mut self, sorted: bool, parallel_threshold: usize) -> u64
pub fn cardinality(&mut self, sorted: bool, parallel_threshold: usize) -> u64
Returns the cardinality of the data.
Set sorted to true if the data is already sorted.
Set parallel_threshold to 0 to force sequential processing.
Set parallel_threshold to 1 to use the default parallel threshold (10_000).
Set parallel_threshold to 2 to force parallel processing.
Set parallel_threshold to any other value to use a custom parallel threshold
greater than the default threshold of 10_000.
Source§impl<T: PartialOrd + Clone> Unsorted<T>
impl<T: PartialOrd + Clone> Unsorted<T>
Source§impl<T: PartialOrd + ToPrimitive> Unsorted<T>
impl<T: PartialOrd + ToPrimitive> Unsorted<T>
Source§impl<T: PartialOrd + ToPrimitive> Unsorted<T>
impl<T: PartialOrd + ToPrimitive> Unsorted<T>
Source§impl<T: PartialOrd + ToPrimitive> Unsorted<T>
impl<T: PartialOrd + ToPrimitive> Unsorted<T>
Source§impl<T: PartialOrd + ToPrimitive + Sync> Unsorted<T>
impl<T: PartialOrd + ToPrimitive + Sync> Unsorted<T>
Sourcepub fn gini(&mut self) -> Option<f64>
pub fn gini(&mut self) -> Option<f64>
Returns the Gini Coefficient of the data.
The Gini Coefficient measures inequality in a distribution, ranging from 0 (perfect equality) to 1 (perfect inequality). This method sorts the data first and then computes the Gini coefficient. Time complexity: O(n log n)
Sourcepub fn kurtosis(&mut self) -> Option<f64>
pub fn kurtosis(&mut self) -> Option<f64>
Returns the kurtosis (excess kurtosis) of the data.
Kurtosis measures the “tailedness” of a distribution. Excess kurtosis is kurtosis - 3, where 0 indicates a normal distribution, positive values indicate heavy tails, and negative values indicate light tails. This method sorts the data first and then computes kurtosis. Time complexity: O(n log n)
Sourcepub fn percentile_rank<V>(&mut self, value: V) -> Option<f64>where
V: PartialOrd + ToPrimitive,
pub fn percentile_rank<V>(&mut self, value: V) -> Option<f64>where
V: PartialOrd + ToPrimitive,
Returns the percentile rank of a value in the data.
Returns the percentile rank (0-100) of the given value. If the value is less than all values, returns 0.0. If greater than all, returns 100.0. This method sorts the data first and then computes the percentile rank. Time complexity: O(n log n)
Sourcepub fn atkinson(&mut self, epsilon: f64) -> Option<f64>
pub fn atkinson(&mut self, epsilon: f64) -> Option<f64>
Returns the Atkinson Index of the data.
The Atkinson Index measures inequality with an inequality aversion parameter ε. It ranges from 0 (perfect equality) to 1 (perfect inequality). Higher ε values give more weight to inequality at the lower end of the distribution. This method sorts the data first and then computes the Atkinson index. Time complexity: O(n log n)
§Arguments
epsilon- Inequality aversion parameter (must be >= 0). Common values:- 0.0: No inequality aversion (returns 0)
- 0.5: Moderate aversion
- 1.0: Uses geometric mean (special case)
- 2.0: High aversion
Source§impl<T: PartialOrd + ToPrimitive + Clone> Unsorted<T>
impl<T: PartialOrd + ToPrimitive + Clone> Unsorted<T>
Sourcepub fn quartiles_with_selection(&mut self) -> Option<(f64, f64, f64)>
pub fn quartiles_with_selection(&mut self) -> Option<(f64, f64, f64)>
Returns the quartiles of the data using selection algorithm.
This implementation uses a selection algorithm (quickselect) to find quartiles in O(n) average time complexity instead of O(n log n) sorting. Requires T to implement Clone to create a working copy of the data.
Performance Note: While theoretically O(n) vs O(n log n), this implementation is often slower than the sorting-based approach for small to medium datasets due to:
- Need to find multiple order statistics (3 separate quickselect calls)
- Overhead of copying data to avoid mutation
- Rayon’s highly optimized parallel sorting
Source§impl<T: PartialOrd + ToPrimitive> Unsorted<T>
impl<T: PartialOrd + ToPrimitive> Unsorted<T>
Sourcepub fn quartiles_zero_copy(&self) -> Option<(f64, f64, f64)>
pub fn quartiles_zero_copy(&self) -> Option<(f64, f64, f64)>
Returns the quartiles using zero-copy selection algorithm.
This implementation avoids copying data by working with indices instead, providing better performance than the clone-based selection approach. The algorithm is O(n) average time and only allocates a vector of indices (usize).
Source§impl<T: PartialOrd + Clone> Unsorted<T>
impl<T: PartialOrd + Clone> Unsorted<T>
Sourcepub fn custom_percentiles(&mut self, percentiles: &[u8]) -> Option<Vec<T>>
pub fn custom_percentiles(&mut self, percentiles: &[u8]) -> Option<Vec<T>>
Returns the requested percentiles of the data.
Uses the nearest-rank method to compute percentiles. Each returned value is an actual value from the dataset.
§Arguments
percentiles- A slice of u8 values representing percentiles to compute (0-100)
§Returns
Noneif the data is empty or if any percentile is > 100Some(Vec<T>)containing percentile values in the same order as requested
§Example
use stats::Unsorted;
let mut data = Unsorted::new();
data.extend(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let percentiles = vec![25, 50, 75];
let results = data.custom_percentiles(&percentiles).unwrap();
assert_eq!(results, vec![3, 5, 8]);Trait Implementations§
Source§impl<T: PartialOrd> Commute for Unsorted<T>
impl<T: PartialOrd> Commute for Unsorted<T>
Source§impl<T: PartialOrd> Default for Unsorted<T>
impl<T: PartialOrd> Default for Unsorted<T>
Source§impl<'de, T> Deserialize<'de> for Unsorted<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Unsorted<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> Extend<T> for Unsorted<T>
impl<T: PartialOrd> Extend<T> for Unsorted<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> FromIterator<T> for Unsorted<T>
impl<T: PartialOrd> FromIterator<T> for Unsorted<T>
impl<T: Eq> Eq for Unsorted<T>
impl<T> StructuralPartialEq for Unsorted<T>
Auto Trait Implementations§
impl<T> Freeze for Unsorted<T>
impl<T> RefUnwindSafe for Unsorted<T>where
T: RefUnwindSafe,
impl<T> Send for Unsorted<T>
impl<T> Sync for Unsorted<T>
impl<T> Unpin for Unsorted<T>where
T: Unpin,
impl<T> UnwindSafe for Unsorted<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