fera_graph/
fun.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! Free functions.
6
7use params::IntoOwned;
8use prelude::*;
9use std::iter::Sum;
10
11/// Returns the iterator's item with minimum property value or `None` if the iterator is empty.
12///
13/// If several items have equally minimum property value, the first item with minimum property
14/// value is returned.
15///
16/// # Example
17///
18/// ```
19/// use fera_graph::prelude::*;
20/// use fera_graph::min_by_prop;
21///
22/// let g = CompleteGraph::new(4);
23/// let mut w = g.default_vertex_prop(0u32);
24/// w[0] = 5;
25/// w[1] = 0;
26/// w[2] = 3;
27/// w[3] = 0;
28/// assert_eq!(Some(1), min_by_prop(&w, g.vertices()));
29/// assert_eq!(None, min_by_prop(&w, g.vertices().take(0)));
30/// assert_eq!(Some(&2), min_by_prop(&w, &[0, 2]));
31/// ```
32#[inline]
33pub fn min_by_prop<I, P, K>(prop: P, iter: I) -> Option<I::Item>
34where
35    I: IntoIterator,
36    I::Item: Copy + IntoOwned<K>,
37    P: PropGet<K>,
38    P::Output: Ord,
39{
40    iter.into_iter()
41        .min_by_key(move |&v| prop.get(v.into_owned()))
42}
43
44/// Returns the iterator's item with maximum property value or `None` if the iterator is empty.
45///
46/// If several items have equally maximum property value, the last item with maximum property value
47/// is returned.
48///
49/// # Example
50///
51/// ```
52/// use fera_graph::prelude::*;
53/// use fera_graph::max_by_prop;
54///
55/// let g = CompleteGraph::new(4);
56/// let mut w = g.default_vertex_prop(0u32);
57/// w[0] = 5;
58/// w[1] = 10;
59/// w[2] = 3;
60/// w[3] = 10;
61/// assert_eq!(Some(3), max_by_prop(&w, g.vertices()));
62/// assert_eq!(None, max_by_prop(&w, g.vertices().take(0)));
63/// assert_eq!(Some(&0), max_by_prop(&w, &[0, 2]));
64/// ```
65#[inline]
66pub fn max_by_prop<I, P, K>(prop: P, iter: I) -> Option<I::Item>
67where
68    I: IntoIterator,
69    I::Item: Copy + IntoOwned<K>,
70    P: PropGet<K>,
71    P::Output: Ord,
72{
73    iter.into_iter()
74        .max_by_key(move |&v| prop.get(v.into_owned()))
75}
76
77/// Returns the minimum property value associated with the iterator's items or `None` if the
78/// iterator is empty.
79///
80/// # Example
81///
82/// ```
83/// use fera_graph::prelude::*;
84/// use fera_graph::min_prop;
85///
86/// let g = CompleteGraph::new(3);
87/// let mut w = g.default_vertex_prop(0u32);
88/// w[0] = 5;
89/// w[1] = 10;
90/// w[2] = 4;
91/// assert_eq!(Some(4), min_prop(&w, g.vertices()));
92/// assert_eq!(None, min_prop(&w, g.vertices().take(0)));
93/// assert_eq!(Some(5), min_prop(&w, &[1, 0]));
94/// ```
95#[inline]
96pub fn min_prop<P, K, I>(prop: P, iter: I) -> Option<P::Output>
97where
98    I: IntoIterator,
99    I::Item: IntoOwned<K>,
100    P: PropGet<K>,
101    P::Output: Ord,
102{
103    iter.into_iter()
104        .map(move |v| prop.get(v.into_owned()))
105        .min()
106}
107
108/// Returns the maximum property value associated with the iterator's items or `None` if the
109/// iterator is empty.
110///
111/// # Example
112///
113/// ```
114/// use fera_graph::prelude::*;
115/// use fera_graph::max_prop;
116///
117/// let g = CompleteGraph::new(3);
118/// let mut w = g.default_vertex_prop(0u32);
119/// w[0] = 5;
120/// w[1] = 10;
121/// w[2] = 4;
122/// assert_eq!(Some(10), max_prop(&w, g.vertices()));
123/// assert_eq!(None, max_prop(&w, g.vertices().take(0)));
124/// assert_eq!(Some(5), max_prop(&w, &[2, 0]));
125/// ```
126#[inline]
127pub fn max_prop<P, K, I>(prop: P, iter: I) -> Option<P::Output>
128where
129    I: IntoIterator,
130    I::Item: IntoOwned<K>,
131    P: PropGet<K>,
132    P::Output: Ord,
133{
134    iter.into_iter()
135        .map(move |v| prop.get(v.into_owned()))
136        .max()
137}
138
139/// Returns the sum of the property values of the iterator's items.
140///
141/// # Example
142///
143/// ```
144/// use fera_graph::prelude::*;
145/// use fera_graph::sum_prop;
146///
147/// let g = CompleteGraph::new(3);
148/// let mut w = g.default_vertex_prop(0u32);
149/// w[0] = 5;
150/// w[1] = 10;
151/// w[2] = 4;
152/// assert_eq!(19u32, sum_prop(&w, g.vertices()));
153/// assert_eq!(0u32, sum_prop(&w, g.vertices().take(0)));
154/// assert_eq!(15u32, sum_prop(&w, &[1, 0]));
155/// ```
156#[inline]
157pub fn sum_prop<P, K, O, I>(prop: P, iter: I) -> O
158where
159    I: IntoIterator,
160    I::Item: IntoOwned<K>,
161    P: PropGet<K>,
162    O: Sum<P::Output>,
163{
164    iter.into_iter()
165        .map(move |v| prop.get(v.into_owned()))
166        .sum()
167}