From 0d15b431b9316b55f950a8c3339a09773bdc744b Mon Sep 17 00:00:00 2001 From: Clint White <104277906+clint-white@users.noreply.github.com> Date: Sun, 8 May 2022 13:25:48 -0400 Subject: [PATCH 1/3] Simplify the implementation of `most_common_tiebreaker()` Use match ergonomics to simplify the pattern matching. When matching a reference value with a non-reference pattern, the default binding mode is automatically set to `ref`. In this case, by removing the `&` on the tuples and the `ref` keywords on their fields, the variables `{a,b}_{item,count}` are bound as references. Replace the `match` expression with `Ordering::then_with()`, which does the same thing more succinctly. --- src/lib.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 66f26fe..12377de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::>(); - 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 } From 739207676507e63142221a2c576db1b35264f310 Mon Sep 17 00:00:00 2001 From: Clint White <104277906+clint-white@users.noreply.github.com> Date: Thu, 12 May 2022 20:32:27 -0400 Subject: [PATCH 2/3] Remove unnecessary `.into_iter()` in `for`-loops We can directly loop over containers of type `I: IntoIter` without having to call `IntoIter::into_iter()` explicitly; that is part of the syntactic sugar of `for`-loops. --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 12377de..c81b8f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -238,7 +238,7 @@ where where I: IntoIterator, { - for item in iterable.into_iter() { + for item in iterable { let entry = self.map.entry(item).or_insert_with(N::zero); *entry += N::one(); } @@ -266,7 +266,7 @@ where where I: IntoIterator, { - 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() { @@ -388,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; } @@ -445,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 { @@ -875,7 +875,7 @@ where /// ``` fn from_iter>(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; } @@ -921,7 +921,7 @@ where /// assert_eq!(counter.into_map(), expect); /// ``` fn extend>(&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; } @@ -947,7 +947,7 @@ where /// assert_eq!(counter.into_map(), expect); /// ``` fn extend>(&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(); } From df480c56597faee9d60739692a0e75af1eee30f0 Mon Sep 17 00:00:00 2001 From: Clint White <104277906+clint-white@users.noreply.github.com> Date: Thu, 12 May 2022 20:36:16 -0400 Subject: [PATCH 3/3] Replace redundant closure with method --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c81b8f5..298f32a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -350,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) } }