Skip to main content

VersionedCounts

Struct VersionedCounts 

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

A reusable counter keyed by usize with O(1) “clear” between sessions.

Storage is positional — bucket[idx] holds the count for source index idx. Each session bumps a version stamp; slots are considered live only when their stamp matches the current version, so a session “clear” is a single increment instead of a vec-wide write.

Designed for hot paths that previously allocated a fresh HashMap<usize, u32> per call. After the first begin(capacity) warms the buffer, subsequent sessions allocate nothing.

Implementations§

Source§

impl VersionedCounts

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn unique_count(&self) -> usize

Source

pub fn begin(&mut self, capacity: usize)

Start a new session. Resizes if needed and bumps the version so all previously-touched slots read as stale on the first bump.

Source

pub fn bump(&mut self, idx: usize) -> u32

Increment the count at idx, returning the new count for this session. Stale slots are reset to 1 before counting.

Source

pub fn get(&self, idx: usize) -> u32

Read the count at idx for the current session. Returns 0 if the slot is out of bounds or stale.

Source

pub fn iter_live(&self) -> impl Iterator<Item = (usize, u32)> + '_

Live (idx, count) pairs in ascending idx order.

Source

pub fn iter_live_rev(&self) -> impl Iterator<Item = (usize, u32)> + '_

Live (idx, count) pairs in descending idx order — convenient for callers that need to mutate an indexed collection without invalidating not-yet-processed indices (e.g. Vec::swap_remove).

Source

pub fn iter_pair_live_rev<'a>( &'a self, other: &'a Self, ) -> impl Iterator<Item = (usize, u32, u32)> + 'a

Walk both buffers in parallel in descending idx order, yielding (idx, count_self, count_other) for any idx where at least one side is live this session. Slots stale or out-of-bounds on either side contribute 0 for that side. Stops at max(self.len, other.len).

Trait Implementations§

Source§

impl Clone for VersionedCounts

Source§

fn clone(&self) -> VersionedCounts

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VersionedCounts

Source§

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

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

impl Default for VersionedCounts

Source§

fn default() -> Self

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

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.