#![doc = include_str!("../README.md")]
#![no_std]
use core::cmp::Ordering;
#[doc = include_str!("../README.md")]
pub trait IterFirstMaxExt: Iterator + Sized {
#[inline]
fn first_max(self) -> Option<Self::Item>
where Self::Item: Ord,
{
self.first_max_by(Ord::cmp)
}
#[inline]
fn first_max_by<F>(self, mut f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
self.reduce(move |a, b| {
if f(&a, &b).is_lt() {
b
} else {
a
}
})
}
#[inline]
fn first_max_by_key<B, F>(self, mut f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> B,
B: Ord,
{
#[inline]
fn cmp<B: Ord, T>((a, _): &(B, T), (b, _): &(B, T)) -> Ordering {
a.cmp(b)
}
self.map(move |elem| (f(&elem), elem))
.first_max_by(cmp)?
.1
.into()
}
}
impl<I: Iterator> IterFirstMaxExt for I { }
pub trait IterLastMinExt: Iterator + Sized {
#[inline]
fn last_min(self) -> Option<Self::Item>
where Self::Item: Ord,
{
self.last_min_by(Ord::cmp)
}
#[inline]
fn last_min_by<F>(self, mut f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
self.reduce(move |a, b| {
if f(&a, &b).is_ge() {
b
} else {
a
}
})
}
#[inline]
fn last_min_by_key<B, F>(self, mut f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> B,
B: Ord,
{
#[inline]
fn cmp<B: Ord, T>((a, _): &(B, T), (b, _): &(B, T)) -> Ordering {
a.cmp(b)
}
self.map(move |elem| (f(&elem), elem))
.last_min_by(cmp)?
.1
.into()
}
}
impl<I: Iterator> IterLastMinExt for I { }