Skip to content

Commit

Permalink
Take empty counters into account in CounterMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tiram88 committed Apr 15, 2024
1 parent 51cd579 commit ee8153b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions notify/src/address/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ pub struct CounterMap {

/// Map of address index to reference count
indexes: HashMap<Index, RefCount>,

///
empty_entries: usize,
}

impl CounterMap {
pub fn new(tracker: Tracker) -> Self {
Self { tracker, indexes: HashMap::new() }
Self { tracker, indexes: HashMap::new(), empty_entries: 0 }
}

pub fn with_capacity(tracker: Tracker, capacity: usize) -> Self {
Self { tracker, indexes: HashMap::with_capacity(capacity) }
Self { tracker, indexes: HashMap::with_capacity(capacity), empty_entries: 0 }
}

/// Registers a vector of addresses, inserting them if not existing yet and increasing their reference count.
Expand Down Expand Up @@ -101,11 +104,11 @@ impl CounterMap {
}

pub fn len(&self) -> usize {
self.indexes.len()
self.indexes.len() - self.empty_entries
}

pub fn is_empty(&self) -> bool {
self.indexes.is_empty()
self.indexes.is_empty() || self.indexes.len() == self.empty_entries
}

pub fn capacity(&self) -> usize {
Expand Down Expand Up @@ -143,7 +146,11 @@ impl IndexerStorage for CounterMap {
.entry(index)
.and_modify(|x| {
*x += 1;
result = *x == 1;
if *x == 1 {
self.empty_entries -= 1;
} else {
result = false;
}
})
.or_insert(1);
result
Expand All @@ -154,7 +161,10 @@ impl IndexerStorage for CounterMap {
self.indexes.entry(index).and_modify(|x| {
if *x > 0 {
*x -= 1;
result = *x == 0
if *x == 0 {
self.empty_entries += 1;
result = true;
}
}
});
result
Expand All @@ -169,7 +179,7 @@ impl Clone for CounterMap {
// which associated ref counter here is non-zero
self.tracker.reference_indexes(indexes.iter().filter_map(|(index, count)| (*count > 0).then_some(index)));

Self { tracker: self.tracker.clone(), indexes }
Self { tracker: self.tracker.clone(), indexes, empty_entries: self.empty_entries }
}
}

Expand Down

0 comments on commit ee8153b

Please sign in to comment.