Struct tdigest_ch::TDigest

source ·
pub struct TDigest { /* private fields */ }
Expand description

T-digest data structure for approximating the quantiles of a distribution.

§Examples

use tdigest_ch::TDigest;

let mut digest = TDigest::new();

// Add some elements.
digest.insert(1.0);
digest.insert(2.0);
digest.insert(3.0);

// Get the median of the distribution.
let quantile = digest.quantile(0.5);
assert_eq!(quantile, 2.0);

Implementations§

source§

impl TDigest

source

pub fn new() -> Self

Creates an empty TDigest.

§Examples
use tdigest_ch::TDigest;
let digest = TDigest::new();
source

pub fn builder() -> TDigestBuilder

Creates a TDigestBuilder to configure a TDigest.

This is the same as TDigestBuilder::new().

source

pub fn append(&mut self, other: &mut TDigest)

Moves all the elements of other into self, leaving other empty.

§Examples
use tdigest_ch::TDigest;

let mut a = TDigest::from([-10.0, 1.0, 2.0, 2.0, 3.0]);
let mut b = TDigest::from([-20.0, 5.0, 43.0]);

a.append(&mut b);

assert_eq!(a.len(), 8);
assert!(b.is_empty());
source

pub fn len(&self) -> usize

Returns the number of elements in the t-digest.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::new();
assert_eq!(digest.len(), 0);
digest.insert(1.0);
assert_eq!(digest.len(), 1);
source

pub fn is_empty(&self) -> bool

Returns true if the t-digest contains no elements.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::new();
assert!(digest.is_empty());
digest.insert(1.0);
assert!(!digest.is_empty());
source

pub fn clear(&mut self)

Clears the t-digest, removing all values.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::new();
digest.insert(1.0);
digest.clear();
assert!(digest.is_empty());
source

pub fn quantile(&mut self, level: f64) -> f32

Returns the estimated quantile of the t-digest.

This method expects self to be mutable, since the t-digest may be compressed. If you require an immutable, shared reference to compute quantiles, consider using quantiles instead.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::from([1.0, 2.0, 3.0, 4.0, 5.0]);
assert_eq!(digest.quantile(0.0), 1.0);
assert_eq!(digest.quantile(0.5), 3.0);
assert_eq!(digest.quantile(1.0), 5.0);
source

pub fn quantiles(&mut self) -> Quantiles<'_>

Creates an immutable quantile estimator from the t-digest.

§Examples
use std::thread;

use tdigest_ch::TDigest;

let mut digest = TDigest::from([1.0, 2.0, 3.0, 4.0, 5.0]);
let quantiles = digest.quantiles();

thread::scope(|s| {
    s.spawn(|| {
        assert_eq!(quantiles.get(0.0), 1.0);
    });
    s.spawn(|| {
        assert_eq!(quantiles.get(0.5), 3.0);
    });
    s.spawn(|| {
        assert_eq!(quantiles.get(1.0), 5.0);
    });
});
source

pub fn insert(&mut self, value: f32)

Adds a value to the t-digest.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::new();

digest.insert(1.0);
digest.insert(2.0);
assert_eq!(digest.len(), 2);
source

pub fn insert_many(&mut self, value: f32, count: usize)

Adds multiple values to the t-digest.

§Examples
use tdigest_ch::TDigest;

let mut digest = TDigest::new();

digest.insert_many(1.0, 1);
digest.insert_many(2.0, 2);
assert_eq!(digest.len(), 3);

Trait Implementations§

source§

impl BitOr<&TDigest> for &TDigest

source§

fn bitor(self, rhs: &TDigest) -> TDigest

Returns the union of self and rhs as a new TDigest.

§Examples
use tdigest_ch::TDigest;

let a = TDigest::from([1.0, 2.0, 3.0]);
let b = TDigest::from([3.0, 4.0, 5.0]);

let mut c = &a | &b;

assert_eq!(c.len(), 6);
assert_eq!(c.quantile(0.5), 3.0);
§

type Output = TDigest

The resulting type after applying the | operator.
source§

impl BitOrAssign<&TDigest> for TDigest

source§

fn bitor_assign(&mut self, rhs: &TDigest)

Merges self and rhs into self.

§Examples
use tdigest_ch::TDigest;

let mut a = TDigest::from([1.0, 2.0, 3.0]);
let b = TDigest::from([3.0, 4.0, 5.0]);

a |= &b;

assert_eq!(a.len(), 6);
assert_eq!(a.quantile(0.5), 3.0);
source§

impl Clone for TDigest

source§

fn clone(&self) -> TDigest

Returns a copy 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 Debug for TDigest

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for TDigest

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for TDigest

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 Extend<f32> for TDigest

source§

fn extend<I: IntoIterator<Item = f32>>(&mut self, iter: 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<const N: usize> From<[f32; N]> for TDigest

source§

fn from(array: [f32; N]) -> Self

§Examples
use tdigest_ch::TDigest;

let digest1 = TDigest::from([1.0, 2.0, 3.0, 4.0]);
let digest2: TDigest = [1.0, 2.0, 3.0, 4.0].into();
assert_eq!(digest1, digest2);
source§

impl FromIterator<f32> for TDigest

source§

fn from_iter<I: IntoIterator<Item = f32>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl PartialEq for TDigest

source§

fn eq(&self, other: &TDigest) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for TDigest

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 StructuralPartialEq for TDigest

Auto Trait Implementations§

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> 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> ToOwned for T
where T: Clone,

§

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

§

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

§

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