| 1 |
use alloc::{vec, vec::Vec}; |
| 2 |
use std::cmp::Ordering; |
| 3 |
|
| 4 |
/// Implementation guts for `min_set`, `min_set_by`, and `min_set_by_key`. |
| 5 |
pub fn min_set_impl<I, K, F, Compare>( |
| 6 |
mut it: I, |
| 7 |
mut key_for: F, |
| 8 |
mut compare: Compare, |
| 9 |
) -> Vec<I::Item> |
| 10 |
where |
| 11 |
I: Iterator, |
| 12 |
F: FnMut(&I::Item) -> K, |
| 13 |
Compare: FnMut(&I::Item, &I::Item, &K, &K) -> Ordering, |
| 14 |
{ |
| 15 |
match it.next() { |
| 16 |
None => Vec::new(), |
| 17 |
Some(element) => { |
| 18 |
let mut current_key = key_for(&element); |
| 19 |
let mut result = vec![element]; |
| 20 |
it.for_each(|element| { |
| 21 |
let key = key_for(&element); |
| 22 |
match compare(&element, &result[0], &key, ¤t_key) { |
| 23 |
Ordering::Less => { |
| 24 |
result.clear(); |
| 25 |
result.push(element); |
| 26 |
current_key = key; |
| 27 |
} |
| 28 |
Ordering::Equal => { |
| 29 |
result.push(element); |
| 30 |
} |
| 31 |
Ordering::Greater => {} |
| 32 |
} |
| 33 |
}); |
| 34 |
result |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Implementation guts for `ax_set`, `max_set_by`, and `max_set_by_key`. |
| 40 |
pub fn max_set_impl<I, K, F, Compare>(it: I, key_for: F, mut compare: Compare) -> Vec<I::Item> |
| 41 |
where |
| 42 |
I: Iterator, |
| 43 |
F: FnMut(&I::Item) -> K, |
| 44 |
Compare: FnMut(&I::Item, &I::Item, &K, &K) -> Ordering, |
| 45 |
{ |
| 46 |
min_set_impl(it, key_for, |it1, it2, key1, key2| { |
| 47 |
compare(it2, it1, key2, key1) |
| 48 |
}) |
| 49 |
} |