Unsorted

Struct Unsorted 

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

Source

pub fn new() -> Unsorted<T>

Create initial empty state.

Source

pub fn add(&mut self, v: T)

Add a new element to the set.

Source

pub const fn len(&self) -> usize

Return the number of data points.

Source

pub const fn is_empty(&self) -> bool

Source

pub fn add_bulk(&mut self, values: Vec<T>)

Add multiple elements efficiently

Source

pub fn shrink_to_fit(&mut self)

Shrink capacity to fit current data

Source

pub fn with_capacity(capacity: usize) -> Self

Create with specific capacity

Source

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>

Source

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>

Source

pub fn mode(&mut self) -> Option<T>

Returns the mode of the data.

Source

pub fn modes_antimodes( &mut self, ) -> ((Vec<T>, usize, u32), (Vec<T>, usize, u32))

Returns the modes and antimodes of the data. antimodes_result only returns the first 10 antimodes

Source§

impl<T: PartialOrd + ToPrimitive> Unsorted<T>

Source

pub fn median(&mut self) -> Option<f64>

Returns the median of the data.

Source§

impl<T: PartialOrd + ToPrimitive> Unsorted<T>

Source

pub fn mad(&mut self, existing_median: Option<f64>) -> Option<f64>

Returns the Median Absolute Deviation (MAD) of the data.

Source§

impl<T: PartialOrd + ToPrimitive> Unsorted<T>

Source

pub fn quartiles(&mut self) -> Option<(f64, f64, f64)>

Returns the quartiles of the data using the traditional sorting approach.

This method sorts the data first and then computes quartiles. Time complexity: O(n log n)

Source§

impl<T: PartialOrd + ToPrimitive + Sync> Unsorted<T>

Source

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)

Source

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)

Source

pub fn percentile_rank<V>(&mut self, value: V) -> Option<f64>

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)

Source

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>

Source

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>

Source

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>

Source

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
  • None if the data is empty or if any percentile is > 100
  • Some(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: Clone> Clone for Unsorted<T>

Source§

fn clone(&self) -> Unsorted<T>

Returns a duplicate 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: PartialOrd> Commute for Unsorted<T>

Source§

fn merge(&mut self, v: Unsorted<T>)

Merges the value other into self.
Source§

fn consume<I: Iterator<Item = Self>>(&mut self, other: I)

Merges the values in the iterator into self.
Source§

impl<T: PartialOrd> Default for Unsorted<T>

Source§

fn default() -> Unsorted<T>

Returns the “default value” for a type. Read more
Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: PartialOrd> Extend<T> for Unsorted<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: PartialOrd> FromIterator<T> for Unsorted<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Unsorted<T>

Creates a value from an iterator. Read more
Source§

impl<T: PartialEq> PartialEq for Unsorted<T>

Source§

fn eq(&self, other: &Unsorted<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Unsorted<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
Source§

impl<T: Eq> Eq for Unsorted<T>

Source§

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> 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, dest: *mut u8)

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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>,