Skip to content

Commit

Permalink
Cache length of multiset
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Oct 22, 2024
1 parent b987f89 commit f090848
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/sort/multiset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ mod inner {
pub(crate) struct MultiSet<T: Hash + Ord + Clone>(
/// All values should be > 0
OrdMap<T, usize>,
/// cached length
usize,
);

impl<T: Hash + Ord + Clone> MultiSet<T> {
/// Create a new empty multiset.
pub(crate) fn new() -> Self {
MultiSet(OrdMap::new())
MultiSet(OrdMap::new(), 0)
}

/// Check if the multiset contains a key.
Expand All @@ -31,7 +33,7 @@ mod inner {

/// Return the total number of elements in the multiset.
pub(crate) fn len(&self) -> usize {
self.0.iter().map(|(_, v)| *v).sum()
self.1
}

/// Return an iterator over all elements in the multiset.
Expand Down Expand Up @@ -64,6 +66,7 @@ mod inner {
/// Remove a value from the multiset, taking ownership of it and returning a new multiset.
pub(crate) fn remove(mut self, value: &T) -> Option<MultiSet<T>> {
if let Some(v) = self.0.get(value) {
self.1 -= 1;
if *v == 1 {
self.0.remove(value);
} else {
Expand All @@ -76,6 +79,7 @@ mod inner {
}

fn insert_multiple_mut(&mut self, value: T, n: usize) {
self.1 += n;
if let Some(v) = self.0.get(&value) {
self.0.insert(value, v + n);
} else {
Expand Down

0 comments on commit f090848

Please sign in to comment.