Skip to content

Commit

Permalink
Merge pull request #23 from clint-white/improve-concision
Browse files Browse the repository at this point in the history
Improve concision
  • Loading branch information
coriolinus authored May 15, 2022
2 parents e600fd0 + df480c5 commit 623fb6c
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ where
where
I: IntoIterator<Item = T>,
{
for item in iterable.into_iter() {
for item in iterable {
let entry = self.map.entry(item).or_insert_with(N::zero);
*entry += N::one();
}
Expand Down Expand Up @@ -266,7 +266,7 @@ where
where
I: IntoIterator<Item = T>,
{
for item in iterable.into_iter() {
for item in iterable {
let mut remove = false;
if let Some(entry) = self.map.get_mut(&item) {
if *entry > N::zero() {
Expand Down Expand Up @@ -319,18 +319,15 @@ where
where
F: FnMut(&T, &T) -> ::std::cmp::Ordering,
{
use std::cmp::Ordering;

let mut items = self
.map
.iter()
.map(|(key, count)| (key.clone(), count.clone()))
.collect::<Vec<_>>();
items.sort_by(|&(ref a_item, ref a_count), &(ref b_item, ref b_count)| {
match b_count.cmp(a_count) {
Ordering::Equal => tiebreaker(a_item, b_item),
unequal => unequal,
}
items.sort_by(|(a_item, a_count), (b_item, b_count)| {
b_count
.cmp(a_count)
.then_with(|| tiebreaker(a_item, b_item))
});
items
}
Expand All @@ -353,7 +350,7 @@ where
/// assert_eq!(mc, expect);
/// ```
pub fn most_common_ordered(&self) -> Vec<(T, N)> {
self.most_common_tiebreaker(|a, b| a.cmp(b))
self.most_common_tiebreaker(Ord::cmp)
}
}

Expand Down Expand Up @@ -391,7 +388,7 @@ where
/// assert_eq!(c.into_map(), expect);
/// ```
fn add_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map.into_iter() {
for (key, value) in rhs.map {
let entry = self.map.entry(key).or_insert_with(N::zero);
*entry += value;
}
Expand Down Expand Up @@ -448,7 +445,7 @@ where
/// assert_eq!(c.into_map(), expect);
/// ```
fn sub_assign(&mut self, rhs: Self) {
for (key, value) in rhs.map.into_iter() {
for (key, value) in rhs.map {
let mut remove = false;
if let Some(entry) = self.map.get_mut(&key) {
if *entry >= value {
Expand Down Expand Up @@ -878,7 +875,7 @@ where
/// ```
fn from_iter<I: IntoIterator<Item = (T, N)>>(iter: I) -> Self {
let mut cnt = Counter::new();
for (item, item_count) in iter.into_iter() {
for (item, item_count) in iter {
let entry = cnt.map.entry(item).or_insert_with(N::zero);
*entry += item_count;
}
Expand Down Expand Up @@ -924,7 +921,7 @@ where
/// assert_eq!(counter.into_map(), expect);
/// ```
fn extend<I: IntoIterator<Item = (T, N)>>(&mut self, iter: I) {
for (item, item_count) in iter.into_iter() {
for (item, item_count) in iter {
let entry = self.map.entry(item).or_insert_with(N::zero);
*entry += item_count;
}
Expand All @@ -950,7 +947,7 @@ where
/// assert_eq!(counter.into_map(), expect);
/// ```
fn extend<I: IntoIterator<Item = (&'a T, &'a N)>>(&mut self, iter: I) {
for (item, item_count) in iter.into_iter() {
for (item, item_count) in iter {
let entry = self.map.entry(item.clone()).or_insert_with(N::zero);
*entry += item_count.clone();
}
Expand Down

0 comments on commit 623fb6c

Please sign in to comment.