Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve concision #23

Merged
merged 3 commits into from
May 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Fn(&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