Skip to main content

SparseHistogram

Struct SparseHistogram 

Source
pub struct SparseHistogram { /* private fields */ }
Expand description

A sparse, columnar representation of a histogram.

Significantly smaller than a Histogram when many buckets are zero. Each non-zero bucket is stored as a pair (index[i], count[i]) where index[i] is the bucket index and count[i] is its count, in ascending index order.

Implementations§

Source§

impl SparseHistogram

Source

pub fn new(grouping_power: u8, max_value_power: u8) -> Result<Self, Error>

Construct a new histogram from the provided parameters. See the documentation for crate::Config to understand their meaning.

Source

pub fn with_config(config: &Config) -> Self

Creates a new histogram using a provided crate::Config.

Source

pub fn from_parts( config: Config, index: Vec<u32>, count: Vec<u64>, ) -> Result<Self, Error>

Creates a sparse histogram from its raw parts.

Returns an error if:

  • index and count have different lengths
  • any index is out of range for the config
  • the indices are not in strictly ascending order
Source

pub fn into_parts(self) -> (Config, Vec<u32>, Vec<u64>)

Consumes the histogram, returning the config, index, and count vectors.

Source

pub fn config(&self) -> Config

Returns the bucket configuration.

Source

pub fn index(&self) -> &[u32]

Returns a slice of the non-zero bucket indices.

Source

pub fn count(&self) -> &[u64]

Returns a slice of the bucket counts.

Source

pub fn checked_add(&self, h: &SparseHistogram) -> Result<SparseHistogram, Error>

Adds the other histogram to this histogram and returns the result as a new histogram.

Returns Err(Error::IncompatibleParameters) if the configs don’t match, or Err(Error::Overflow) if any bucket overflows.

Source

pub fn wrapping_add( &self, h: &SparseHistogram, ) -> Result<SparseHistogram, Error>

Adds the other histogram to this histogram and returns the result as a new histogram.

Returns Err(Error::IncompatibleParameters) if the configs don’t match. Buckets which have values in both histograms are allowed to wrap.

Source

pub fn checked_sub(&self, h: &SparseHistogram) -> Result<SparseHistogram, Error>

Subtracts the other histogram from this histogram and returns the result as a new histogram. The other histogram is expected to be a subset of the current histogram, i.e., for every bucket in the other histogram should have a count less than or equal to the corresponding bucket in this histogram.

Returns Err(Error::IncompatibleParameters) if the configs don’t match, Err(Error::InvalidSubset) if the other histogram has buckets not present in this one, or Err(Error::Underflow) if any bucket would underflow.

Source

pub fn wrapping_sub( &self, h: &SparseHistogram, ) -> Result<SparseHistogram, Error>

Subtracts the other histogram from this histogram and returns the result as a new histogram.

Returns Err(Error::IncompatibleParameters) if the configs don’t match, or Err(Error::InvalidSubset) if the other histogram has buckets not present in this one. Buckets are allowed to wrap on underflow.

Source

pub fn percentiles( &self, percentiles: &[f64], ) -> Result<Option<Vec<(f64, Bucket)>>, Error>

Return a collection of percentiles from this histogram.

Each percentile should be in the inclusive range 0.0..=1.0. For example, the 50th percentile (median) can be found using 0.5.

The results will be sorted by the percentile.

Source

pub fn percentile(&self, percentile: f64) -> Result<Option<Bucket>, Error>

Return a single percentile from this histogram.

The percentile should be in the inclusive range 0.0..=1.0. For example, the 50th percentile (median) can be found using 0.5.

Source

pub fn downsample(&self, grouping_power: u8) -> Result<SparseHistogram, Error>

Returns a new histogram with a reduced grouping power. The reduced grouping power should lie in the range (0..existing grouping power).

Returns an error if the requested grouping power is not less than the current grouping power.

This works by iterating over every bucket in the existing histogram and inserting the contained values into the new histogram. While we do not know the exact values of the data points (only that they lie within the bucket’s range), it does not matter since the bucket is not split during downsampling and any value can be used.

Source

pub fn iter(&self) -> Iter<'_>

Returns an iterator across the non-zero histogram buckets.

Trait Implementations§

Source§

impl Clone for SparseHistogram

Source§

fn clone(&self) -> SparseHistogram

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 Debug for SparseHistogram

Source§

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

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

impl From<&Histogram> for SparseHistogram

Source§

fn from(histogram: &Histogram) -> Self

Converts to this type from the input type.
Source§

impl From<&SparseHistogram> for Histogram

Source§

fn from(other: &SparseHistogram) -> Self

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for &'a SparseHistogram

Source§

type Item = Bucket

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for SparseHistogram

Source§

fn eq(&self, other: &SparseHistogram) -> 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 Eq for SparseHistogram

Source§

impl StructuralPartialEq for SparseHistogram

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