Skip to content

Commit 6f96e01

Browse files
committed
Add {min,max}_set(_by{_key)?)? functions (5) comparator uses Ordering instead of bool
More canonical.
1 parent 834bcc5 commit 6f96e01

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/extrema_set.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::cmp::Ordering;
2+
13
/// Implementation guts for `min_set`, `min_set_by`, and `min_set_by_key`.
2-
pub fn min_set_impl<I, K, F, L>(mut it: I,
4+
pub fn min_set_impl<I, K, F, Compare>(mut it: I,
35
mut key_for: F,
4-
mut lt: L) -> Vec<I::Item>
6+
mut compare: Compare) -> Vec<I::Item>
57
where I: Iterator,
68
F: FnMut(&I::Item) -> K,
7-
L: FnMut(&I::Item, &I::Item, &K, &K) -> bool,
9+
Compare: FnMut(&I::Item, &I::Item, &K, &K) -> Ordering,
810
{
911
let (mut result, mut current_key) = match it.next() {
1012
None => return Vec::new(),
@@ -16,27 +18,32 @@ pub fn min_set_impl<I, K, F, L>(mut it: I,
1618

1719
it.for_each(|element| {
1820
let key = key_for(&element);
19-
if lt(&element, &result[0], &key, &current_key) {
20-
result.clear();
21-
result.push(element);
22-
current_key = key;
23-
} else if !lt(&result[0], &element, &current_key, &key) {
24-
result.push(element);
21+
match compare(&element, &result[0], &key, &current_key) {
22+
Ordering::Less => {
23+
result.clear();
24+
result.push(element);
25+
current_key = key;
26+
},
27+
Ordering::Equal => {
28+
result.push(element);
29+
},
30+
Ordering::Greater => {
31+
},
2532
}
2633
});
2734

2835
result
2936
}
3037

3138
/// Implementation guts for `ax_set`, `max_set_by`, and `max_set_by_key`.
32-
pub fn max_set_impl<I, K, F, L>(it: I,
39+
pub fn max_set_impl<I, K, F, Compare>(it: I,
3340
key_for: F,
34-
mut lt: L) -> Vec<I::Item>
41+
mut compare: Compare) -> Vec<I::Item>
3542
where I: Iterator,
3643
F: FnMut(&I::Item) -> K,
37-
L: FnMut(&I::Item, &I::Item, &K, &K) -> bool,
44+
Compare: FnMut(&I::Item, &I::Item, &K, &K) -> Ordering,
3845
{
39-
min_set_impl(it, key_for, |it1, it2, key1, key2| lt(it2, it1, key2, key1))
46+
min_set_impl(it, key_for, |it1, it2, key1, key2| compare(it2, it1, key2, key1))
4047
}
4148

4249

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ pub trait Itertools : Iterator {
29302930
fn min_set(self) -> Vec<Self::Item>
29312931
where Self: Sized, Self::Item: Ord
29322932
{
2933-
extrema_set::min_set_impl(self, |_| (), |x, y, _, _| x < y)
2933+
extrema_set::min_set_impl(self, |_| (), |x, y, _, _| x.cmp(y))
29342934
}
29352935

29362936
/// Return all minimum elements of an iterator, as determined by
@@ -2964,7 +2964,7 @@ pub trait Itertools : Iterator {
29642964
extrema_set::min_set_impl(
29652965
self,
29662966
|_| (),
2967-
|x, y, _, _| Ordering::Less == compare(x, y)
2967+
|x, y, _, _| compare(x, y)
29682968
)
29692969
}
29702970

@@ -2995,7 +2995,7 @@ pub trait Itertools : Iterator {
29952995
fn min_set_by_key<K, F>(self, key: F) -> Vec<Self::Item>
29962996
where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K
29972997
{
2998-
extrema_set::min_set_impl(self, key, |_, _, kx, ky| kx < ky)
2998+
extrema_set::min_set_impl(self, key, |_, _, kx, ky| kx.cmp(ky))
29992999
}
30003000

30013001
/// Return all maximum elements of an iterator.
@@ -3024,7 +3024,7 @@ pub trait Itertools : Iterator {
30243024
fn max_set(self) -> Vec<Self::Item>
30253025
where Self: Sized, Self::Item: Ord
30263026
{
3027-
extrema_set::max_set_impl(self, |_| (), |x, y, _, _| x < y)
3027+
extrema_set::max_set_impl(self, |_| (), |x, y, _, _| x.cmp(y))
30283028
}
30293029

30303030
/// Return all maximum elements of an iterator, as determined by
@@ -3058,7 +3058,7 @@ pub trait Itertools : Iterator {
30583058
extrema_set::max_set_impl(
30593059
self,
30603060
|_| (),
3061-
|x, y, _, _| Ordering::Less == compare(x, y)
3061+
|x, y, _, _| compare(x, y)
30623062
)
30633063
}
30643064

@@ -3089,7 +3089,7 @@ pub trait Itertools : Iterator {
30893089
fn max_set_by_key<K, F>(self, key: F) -> Vec<Self::Item>
30903090
where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K
30913091
{
3092-
extrema_set::max_set_impl(self, key, |_, _, kx, ky| kx < ky)
3092+
extrema_set::max_set_impl(self, key, |_, _, kx, ky| kx.cmp(ky))
30933093
}
30943094

30953095
/// Return the minimum and maximum elements in the iterator.

0 commit comments

Comments
 (0)