Skip to content

Commit

Permalink
Add len and cardinality to MultiSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
porcuquine committed Jan 8, 2024
1 parent 8965f96 commit 36d2404
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/coprocessor/memoset/multiset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,57 @@ use std::default::Default;
use std::hash::Hash;

#[derive(PartialEq, Eq, Debug, Default, Clone)]
pub(crate) struct MultiSet<T: Hash + Eq>(pub(crate) HashMap<T, usize>);
pub(crate) struct MultiSet<T: Hash + Eq> {
map: HashMap<T, usize>,
cardinality: usize,
}

impl<T: Hash + Eq> MultiSet<T> {
pub(crate) fn new() -> Self {
Self(Default::default())
Self {
map: Default::default(),
cardinality: 0,
}
}
pub(crate) fn add(&mut self, element: T) {
*self.0.entry(element).or_insert(0) += 1;
*self.map.entry(element).or_insert(0) += 1;
self.cardinality += 1;
}

pub(crate) fn get(&self, element: &T) -> Option<usize> {
self.0.get(element).copied()
self.map.get(element).copied()
}

#[allow(dead_code)]
pub(crate) fn cardinality(&self) -> usize {
self.cardinality
}

#[allow(dead_code)]
pub(crate) fn len(&self) -> usize {
self.map.len()
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_multiset() {
let mut m = MultiSet::<usize>::new();
let mut c = 0;
let n = 5;

for i in 1..n {
for _ in 0..i {
m.add(i);
}
c += i;
assert_eq!(i, m.len());
assert_eq!(c, m.cardinality());
assert_eq!(Some(i), m.get(&i));
assert_eq!(None, m.get(&(i + n)));
}
}
}

0 comments on commit 36d2404

Please sign in to comment.