Struct russell_stat::Histogram

source ·
pub struct Histogram<T>
where T: Num + Copy,
{ /* private fields */ }
Expand description

Implements a Histogram to count frequencies

The stations/bins are set as follows:

   [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] )
---|---------|---------|---------|---------|---------|---  x
 s[0]      s[1]      s[2]      s[3]      s[4]      s[5]

bin_i corresponds to station_i <= x < station_(i+1)

§Examples

use russell_stat::{Histogram, StrError};

fn main() -> Result<(), StrError> {
    let data = [
        -1.0, // outside
        10.0, 10.1, // outside
        9.0, // count = 1
        8.1, 8.2, // count = 2
        7.1, 7.2, 7.2, // count = 3
        6.0, 6.1, 6.1, 6.2, 6.99, // count = 5
        5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, // count = 9
        4.0, 4.1, 4.1, 4.2, 4.99, // count = 5
        3.1, 3.2, 3.2, // count = 3
        2.1, // count = 1
    ];

    let stations: [f64; 11] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];

    let mut hist = Histogram::new(&stations)?;

    hist.count(&data);
    assert_eq!(hist.get_counts(), &[0, 0, 1, 3, 5, 9, 5, 3, 2, 1]);

    hist.set_bar_max_len(10);
    assert_eq!(
        format!("{}", hist),
        "[ 0, 1) | 0 \n\
         [ 1, 2) | 0 \n\
         [ 2, 3) | 1 🟦\n\
         [ 3, 4) | 3 🟦🟦🟦\n\
         [ 4, 5) | 5 🟦🟦🟦🟦🟦\n\
         [ 5, 6) | 9 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦\n\
         [ 6, 7) | 5 🟦🟦🟦🟦🟦\n\
         [ 7, 8) | 3 🟦🟦🟦\n\
         [ 8, 9) | 2 🟦🟦\n\
         [ 9,10) | 1 🟦\n\
         \x20\x20\x20sum = 29\n"
    );
    Ok(())
}

Implementations§

source§

impl<T> Histogram<T>
where T: Num + Copy + PartialOrd,

source

pub fn new(stations: &[T]) -> Result<Self, StrError>

Creates a new Histogram

source

pub fn count(&mut self, data: &[T])

Counts how many items fall within each bin

source

pub fn reset(&mut self)

Erase all counts

source

pub fn get_counts(&self) -> &Vec<usize>

Returns a read-only access to the counts (frequencies)

source

pub fn set_bar_char(&mut self, bar_char: char) -> &mut Self

Sets the character used in histogram drawn by Display

source

pub fn set_bar_max_len(&mut self, bar_max_len: usize) -> &mut Self

Sets the maximum length (number of characters) used in histogram draw by Display

Trait Implementations§

source§

impl<T> Display for Histogram<T>
where T: Num + Copy + Display,

source§

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

Draws histogram

Auto Trait Implementations§

§

impl<T> Freeze for Histogram<T>

§

impl<T> RefUnwindSafe for Histogram<T>
where T: RefUnwindSafe,

§

impl<T> Send for Histogram<T>
where T: Send,

§

impl<T> Sync for Histogram<T>
where T: Sync,

§

impl<T> Unpin for Histogram<T>
where T: Unpin,

§

impl<T> UnwindSafe for Histogram<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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V