1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* SPDX-FileCopyrightText: 2024 Matteo Dell'Acqua
* SPDX-FileCopyrightText: 2025 Sebastiano Vigna
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
/// Returns the index of the minimum value in an iterator, or [`None`] if the
/// iterator is empty.
///
/// If the minimum appears several times, this methods returns the position of
/// the first instance.
///
/// # Arguments
///
/// * `iter`: the iterator.
///
/// # Panics
///
/// If a comparison returns [`None`].
///
/// # Examples
///
/// ```rust
/// # use webgraph_algo::utils::math::argmin;
/// let v = vec![4, 3, 1, 0, 5, 0];
/// let index = argmin(&v);
/// assert_eq!(index, Some(3));
/// ```
/// Returns the index of the minimum 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 minimum 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
///
/// ```rust
/// # use webgraph_algo::utils::math::argmin_filtered;
/// let v = vec![3, 2, 5, 2, 3, 2];
/// let tie = vec![5, 4, 3, 2, 1, 1];
/// let index = argmin_filtered(&v, &tie, |_, &element| element > 1);
/// // Tie break wins
/// assert_eq!(index, Some(5));
///
/// let v = vec![3, 2, 5, 2, 3, 2];
/// let tie = vec![5, 4, 3, 2, 1, 2];
/// // Enumeration order wins
/// let index = argmin_filtered(&v, &tie, |_, &element| element > 1);
/// assert_eq!(index, Some(3));
/// ```