Struct sampling::histogram::HistogramFast
source · [−]pub struct HistogramFast<T> { /* private fields */ }
Expand description
Faster version of HistogramInt for Integers
provided the bins should be: (left, left +1, …, right - 1) then you should use this version!
Implementations
sourceimpl<T> HistogramFast<T>where
T: Copy,
impl<T> HistogramFast<T>where
T: Copy,
sourceimpl<T> HistogramFast<T>where
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
impl<T> HistogramFast<T>where
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
sourcepub fn new(left: T, right: T) -> Result<Self, HistErrors>
pub fn new(left: T, right: T) -> Result<Self, HistErrors>
Create a new interval
- same as
Self::new_inclusive(left, right - 1)
though with checks - That makes
left
an inclusive andright
an exclusive border
sourcepub fn new_inclusive(left: T, right: T) -> Result<Self, HistErrors>
pub fn new_inclusive(left: T, right: T) -> Result<Self, HistErrors>
Create new histogram with inclusive borders
Err
ifleft > right
left
is inclusive borderright
is inclusive border
sourcepub fn bin_iter(&self) -> impl Iterator<Item = T>
pub fn bin_iter(&self) -> impl Iterator<Item = T>
Iterator over all the bins
In HistogramFast is hit only when a value matches the corresponding bin exactly, which means there exists a map between bins and corresponding values that would hit them. So a bin is perfectly defined by one value. That is what we are iterating over here
This iterates over these values
Example
use sampling::histogram::HistogramFast;
let hist = HistogramFast::<u8>::new_inclusive(2, 5).unwrap();
let vec: Vec<u8> = hist.bin_iter().collect();
assert_eq!(&vec, &[2, 3, 4, 5]);
sourcepub fn bin_hits_iter(&self) -> impl Iterator<Item = (T, usize)> + '_
pub fn bin_hits_iter(&self) -> impl Iterator<Item = (T, usize)> + '_
Iterator over all the bins
In HistogramFast is hit only when a value matches the corresponding bin exactly, which means there exists a map between bins and corresponding values that would hit them.
This iterates over these values as well as the corresponding hit count (i.e., how often the corresponding bin was hit)
Item of Iterator
(value_corresponding_to_bin, number_of_hits)
Example
use sampling::histogram::HistogramFast;
let mut hist = HistogramFast::<u8>::new_inclusive(2, 5).unwrap();
hist.increment(4).unwrap();
hist.increment(5).unwrap();
hist.increment(5).unwrap();
let vec: Vec<(u8, usize)> = hist.bin_hits_iter().collect();
assert_eq!(&vec, &[(2, 0), (3, 0), (4, 1), (5, 2)]);
sourcepub fn equal_range(&self, other: &Self) -> boolwhere
T: Eq,
pub fn equal_range(&self, other: &Self) -> boolwhere
T: Eq,
checks if the range of two Histograms is equal, i.e., if they have the same bin borders
sourcepub fn try_add(&mut self, other: &Self) -> Result<(), ()>where
T: Eq,
pub fn try_add(&mut self, other: &Self) -> Result<(), ()>where
T: Eq,
Add other histogram to self
- will fail if the ranges are not equal, i.e., if equal_range returns false
- Otherwise the hitcount of the bins of self will be increased by the corresponding hitcount of other.
- other will be unchanged
sourcepub fn increment<V: Borrow<T>>(&mut self, val: V) -> Result<usize, HistErrors>
pub fn increment<V: Borrow<T>>(&mut self, val: V) -> Result<usize, HistErrors>
Increment hit count
If val
is inside the histogram, the corresponding bin count will be increased
by 1 and the index corresponding to the bin in returned: Ok(index)
.
Otherwise an Error is returned
Note
This is the same as HistogramVal::count_val
sourcepub fn increment_quiet<V: Borrow<T>>(&mut self, val: V)
pub fn increment_quiet<V: Borrow<T>>(&mut self, val: V)
Increment hit count
Increments the hit count of the bin corresponding to val
.
If no bin corresponding to val
exists, nothing happens
Trait Implementations
sourceimpl<T: Clone> Clone for HistogramFast<T>
impl<T: Clone> Clone for HistogramFast<T>
sourcefn clone(&self) -> HistogramFast<T>
fn clone(&self) -> HistogramFast<T>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl<T: Debug> Debug for HistogramFast<T>
impl<T: Debug> Debug for HistogramFast<T>
sourceimpl<'de, T> Deserialize<'de> for HistogramFast<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for HistogramFast<T>where
T: Deserialize<'de>,
sourcefn 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>,
sourceimpl<T> Histogram for HistogramFast<T>
impl<T> Histogram for HistogramFast<T>
sourcefn count_multiple_index(
&mut self,
index: usize,
count: usize
) -> Result<(), HistErrors>
fn count_multiple_index(
&mut self,
index: usize,
count: usize
) -> Result<(), HistErrors>
self.hist[index] += count
, Err()
if index
out of boundssourcefn count_index(&mut self, index: usize) -> Result<(), HistErrors>
fn count_index(&mut self, index: usize) -> Result<(), HistErrors>
self.hist[index] += 1
, Err()
if index
out of boundssourcefn any_bin_zero(&self) -> bool
fn any_bin_zero(&self) -> bool
sourceimpl<T> HistogramCombine for HistogramFast<T>where
Self: HistogramVal<T>,
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
impl<T> HistogramCombine for HistogramFast<T>where
Self: HistogramVal<T>,
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
sourcefn encapsulating_hist<S>(hists: &[S]) -> Result<Self, HistErrors>where
S: Borrow<Self>,
fn encapsulating_hist<S>(hists: &[S]) -> Result<Self, HistErrors>where
S: Borrow<Self>,
sourceimpl<T> HistogramIntervalDistance<T> for HistogramFast<T>where
Self: HistogramVal<T>,
T: PartialOrd + Sub<Output = T> + NumCast + Copy,
impl<T> HistogramIntervalDistance<T> for HistogramFast<T>where
Self: HistogramVal<T>,
T: PartialOrd + Sub<Output = T> + NumCast + Copy,
sourcefn interval_distance_overlap<V: Borrow<T>>(
&self,
val: V,
overlap: NonZeroUsize
) -> usize
fn interval_distance_overlap<V: Borrow<T>>(
&self,
val: V,
overlap: NonZeroUsize
) -> usize
sourceimpl<T> HistogramPartition for HistogramFast<T>where
T: PrimInt + CheckedSub + ToPrimitive + CheckedAdd + One + FromPrimitive + HasUnsignedVersion + Bounded,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes, Unsigned = T::Unsigned> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned> + FromPrimitive + WrappingSub,
impl<T> HistogramPartition for HistogramFast<T>where
T: PrimInt + CheckedSub + ToPrimitive + CheckedAdd + One + FromPrimitive + HasUnsignedVersion + Bounded,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes, Unsigned = T::Unsigned> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned> + FromPrimitive + WrappingSub,
sourcefn overlapping_partition(
&self,
n: usize,
overlap: usize
) -> Result<Vec<Self>, HistErrors>
fn overlapping_partition(
&self,
n: usize,
overlap: usize
) -> Result<Vec<Self>, HistErrors>
sourceimpl<T> HistogramVal<T> for HistogramFast<T>where
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
impl<T> HistogramVal<T> for HistogramFast<T>where
T: PrimInt + HasUnsignedVersion,
T::Unsigned: Bounded + HasUnsignedVersion<LeBytes = T::LeBytes> + WrappingAdd + ToPrimitive + Sub<Output = T::Unsigned>,
sourcefn borders_clone(&self) -> Result<Vec<T>, HistErrors>
fn borders_clone(&self) -> Result<Vec<T>, HistErrors>
Creates a vector containing borders
How to understand the borders?
Lets say we have a Vector containing [a,b,c]
,
then the first bin contains all values T
for which
a <= T < b
, the second bin all values T
for which
b <= T < c
. In this case, there are only two bins.
Errors
- returns
Err(Overflow)
if right border isT::MAX
- creates and returns borders otherwise
- Note: even if
Err(Overflow)
is returned, this does not provide any problems for the rest of the implementation, as the border vector is not used internally forHistogramFast
sourcefn first_border(&self) -> T
fn first_border(&self) -> T
sourcefn second_last_border(&self) -> T
fn second_last_border(&self) -> T
let b = self.borders_clone().expect("overflow"); assert_eq!(self.second_last_border(), b[b.len()-2])
sourcefn distance<V: Borrow<T>>(&self, val: V) -> f64
fn distance<V: Borrow<T>>(&self, val: V) -> f64
sourcefn get_bin_index<V: Borrow<T>>(&self, val: V) -> Result<usize, HistErrors>
fn get_bin_index<V: Borrow<T>>(&self, val: V) -> Result<usize, HistErrors>
sourcefn not_inside<V: Borrow<T>>(&self, val: V) -> bool
fn not_inside<V: Borrow<T>>(&self, val: V) -> bool
is_inside