Function argmax_filtered

Source
pub fn argmax_filtered<I: IntoIterator, J: IntoIterator>(
    iter: I,
    tie_break: J,
    filter: impl Fn(usize, I::Item) -> bool,
) -> Option<usize>
where I::Item: PartialOrd + Copy, J::Item: PartialOrd + Copy,
Expand description

Returns the index of the maximum value approved by a filter in an iterator, or None if no element is approved by the filter.

In case of ties, this method returns the index for which the corresponding element in tie_break is minimized.

If the maximum appears several times with the same tie break, this methods returns the position of the first instance.

§Arguments

  • iter: the iterator.

  • tie_break: in case two elements of iter are the same, the corresponding elements in this iterator are used as secondary order.

  • filter: a closure that takes as arguments the index of the element and the element itself and returns true if the element is approved.

§Panics

If a comparison returns None.

§Examples

let v = vec![1, 2, 5, 2, 1, 2];
let tie = vec![1, 2, 3, 4, 5, 2];
let index = argmax_filtered(&v, &tie, |_, &element| element < 4);
// Tie break wins
assert_eq!(index, Some(3));

let v = vec![1, 2, 5, 2, 1, 2];
let tie = vec![1, 1, 3, 2, 5, 2];
let index = argmax_filtered(&v, &tie, |_, &element| element < 4);
// Enumeration order wins
assert_eq!(index, Some(3));