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
impl VersionedCounts
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn unique_count(&self) -> usize
Sourcepub fn begin(&mut self, capacity: usize)
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.
Sourcepub fn bump(&mut self, idx: usize) -> u32
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.
Sourcepub fn get(&self, idx: usize) -> u32
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.
Sourcepub fn iter_live(&self) -> impl Iterator<Item = (usize, u32)> + '_
pub fn iter_live(&self) -> impl Iterator<Item = (usize, u32)> + '_
Live (idx, count) pairs in ascending idx order.
Sourcepub fn iter_live_rev(&self) -> impl Iterator<Item = (usize, u32)> + '_
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).
Sourcepub fn iter_pair_live_rev<'a>(
&'a self,
other: &'a Self,
) -> impl Iterator<Item = (usize, u32, u32)> + 'a
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
impl Clone for VersionedCounts
Source§fn clone(&self) -> VersionedCounts
fn clone(&self) -> VersionedCounts
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more