Skip to content

Commit

Permalink
Simplify multiset add_elem method
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorenesduarte committed Mar 9, 2019
1 parent 13c725b commit e2b5577
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fmt:
rustfmt tests/* benches/* src/*
rustfmt src/* tests/* benches/*

coverage:
cargo coverage
Expand Down
13 changes: 3 additions & 10 deletions src/multiset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,9 @@ impl<T: Ord> MultiSet<T> {
/// assert_eq!(mset.count(&17), 1);
/// ```
pub fn add_elem(&mut self, elem: T) {
match self.occurrences.get_mut(&elem) {
Some(count) => {
// if the element exists, increase its count
*count += 1;
}
None => {
// otherwise, insert it
self.occurrences.insert(elem, 1);
}
}
// increase element count
let count = self.occurrences.entry(elem).or_insert(0);
*count += 1;
}

/// Returns the number of occurrences of an element.
Expand Down

0 comments on commit e2b5577

Please sign in to comment.