1#![doc = include_str!("../README.md")]
2#![no_std]
3
4use core::cmp::Ordering;
5
6#[doc = include_str!("../README.md")]
7pub trait IterFirstMaxExt: Iterator + Sized {
8 #[inline]
10 fn first_max(self) -> Option<Self::Item>
11 where Self::Item: Ord,
12 {
13 self.first_max_by(Ord::cmp)
14 }
15
16 #[inline]
18 fn first_max_by<F>(self, mut f: F) -> Option<Self::Item>
19 where F: FnMut(&Self::Item, &Self::Item) -> Ordering,
20 {
21 self.reduce(move |a, b| {
22 if f(&a, &b).is_lt() {
23 b
24 } else {
25 a
26 }
27 })
28 }
29
30 #[inline]
32 fn first_max_by_key<B, F>(self, mut f: F) -> Option<Self::Item>
33 where F: FnMut(&Self::Item) -> B,
34 B: Ord,
35 {
36 #[inline]
37 fn cmp<B: Ord, T>((a, _): &(B, T), (b, _): &(B, T)) -> Ordering {
38 a.cmp(b)
39 }
40 self.map(move |elem| (f(&elem), elem))
41 .first_max_by(cmp)?
42 .1
43 .into()
44 }
45}
46impl<I: Iterator> IterFirstMaxExt for I { }
47
48
49pub trait IterLastMinExt: Iterator + Sized {
63 #[inline]
65 fn last_min(self) -> Option<Self::Item>
66 where Self::Item: Ord,
67 {
68 self.last_min_by(Ord::cmp)
69 }
70
71 #[inline]
73 fn last_min_by<F>(self, mut f: F) -> Option<Self::Item>
74 where F: FnMut(&Self::Item, &Self::Item) -> Ordering,
75 {
76 self.reduce(move |a, b| {
77 if f(&a, &b).is_ge() {
78 b
79 } else {
80 a
81 }
82 })
83 }
84
85 #[inline]
87 fn last_min_by_key<B, F>(self, mut f: F) -> Option<Self::Item>
88 where F: FnMut(&Self::Item) -> B,
89 B: Ord,
90 {
91 #[inline]
92 fn cmp<B: Ord, T>((a, _): &(B, T), (b, _): &(B, T)) -> Ordering {
93 a.cmp(b)
94 }
95 self.map(move |elem| (f(&elem), elem))
96 .last_min_by(cmp)?
97 .1
98 .into()
99 }
100}
101impl<I: Iterator> IterLastMinExt for I { }