Skip to main content

CategoryCounter

Struct CategoryCounter 

Source
pub struct CategoryCounter<T: Hash + Eq> { /* private fields */ }
Expand description

Accumulator for categorical data (counts by category)

Memory: O(k) where k is the number of unique categories

Implementations§

Source§

impl<T: Hash + Eq> CategoryCounter<T>

Source

pub fn new() -> Self

Create a new category counter

Source

pub fn increment(&mut self, category: T)

Increment count for a category

Source

pub fn increment_by(&mut self, category: T, amount: usize)

Increment count for a category by a specific amount

Source

pub fn get(&self, category: &T) -> usize

Get count for a specific category

Source

pub fn total(&self) -> usize

Get total count across all categories

Source

pub fn num_categories(&self) -> usize

Get the number of unique categories

Source

pub fn frequency(&self, category: &T) -> f64

Get frequency (proportion) for a category

Source

pub fn categories(&self) -> &HashMap<T, usize>

Get all categories and their counts

Source

pub fn iter(&self) -> impl Iterator<Item = (&T, &usize)>

Iterate over (category, count) pairs

Source§

impl CategoryCounter<String>

Specialized implementation for String to allow efficient &str increments

Source

pub fn increment_str(&mut self, category: &str)

Optimized increment for &str - only allocates when category is new

This is much more efficient than increment(category.to_string()) because it only allocates a String when inserting a new category, not on every call.

For existing categories, this does a HashMap lookup with &str (no allocation), and only allocates a new String when the category is seen for the first time.

Trait Implementations§

Source§

impl<T: Clone + Hash + Eq> Clone for CategoryCounter<T>

Source§

fn clone(&self) -> CategoryCounter<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: Debug + Hash + Eq> Debug for CategoryCounter<T>

Source§

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

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

impl<T: Default + Hash + Eq> Default for CategoryCounter<T>

Source§

fn default() -> CategoryCounter<T>

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

impl<T: Hash + Eq + Send> Mergeable for CategoryCounter<T>

Implementation of Mergeable for parallel statistics computation

Merges category counts from multiple threads by summing counts for each category.

Source§

fn merge(&mut self, other: Self)

Merge another stats object into this one Read more
Source§

fn merge_all(stats: Vec<Self>) -> Option<Self>

Merge multiple stats objects into one (convenience method)

Auto Trait Implementations§

§

impl<T> Freeze for CategoryCounter<T>

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for CategoryCounter<T>

§

impl<T> UnwindSafe for CategoryCounter<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.