Skip to content

Commit 3be6a2f

Browse files
committed
auto merge of #17482 : hoeppnertill/rust/master, r=alexcrichton
Intended to prevent each user to write his own partial_min/max, possibly differing in slight details. @sfackler encouraged to PR this on IRC. (Let's hope this works... First PR.)
2 parents 3f8da69 + 29c2d3d commit 3be6a2f

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/libcore/cmp.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
4040
#![stable]
4141

42-
use option::{Option, Some};
42+
use option::{Option, Some, None};
4343

4444
/// Trait for values that can be compared for equality and inequality.
4545
///
@@ -268,6 +268,32 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
268268
if v1 > v2 { v1 } else { v2 }
269269
}
270270

271+
/// Compare and return the minimum of two values if there is one.
272+
///
273+
/// Returns the first argument if the comparison determines them to be equal.
274+
#[inline]
275+
#[experimental]
276+
pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
277+
match v1.partial_cmp(&v2) {
278+
Some(Less) | Some(Equal) => Some(v1),
279+
Some(Greater) => Some(v2),
280+
None => None
281+
}
282+
}
283+
284+
/// Compare and return the maximum of two values if there is one.
285+
///
286+
/// Returns the first argument if the comparison determines them to be equal.
287+
#[inline]
288+
#[experimental]
289+
pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
290+
match v1.partial_cmp(&v2) {
291+
Some(Less) => Some(v2),
292+
Some(Equal) | Some(Greater) => Some(v1),
293+
None => None
294+
}
295+
}
296+
271297
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
272298
mod impls {
273299
use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering,

src/libcoretest/cmp.rs

+67
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use core::cmp::lexical_ordering;
12+
use core::cmp::{ partial_min, partial_max };
1213

1314
#[test]
1415
fn test_int_totalord() {
@@ -56,6 +57,72 @@ fn test_lexical_ordering() {
5657
}
5758
}
5859

60+
#[test]
61+
fn test_partial_min() {
62+
use core::f64::NAN;
63+
let data_integer = [
64+
// a, b, result
65+
(0i, 0i, Some(0i)),
66+
(1i, 0i, Some(0i)),
67+
(0i, 1i, Some(0i)),
68+
(-1i, 0i, Some(-1i)),
69+
(0i, -1i, Some(-1i))
70+
];
71+
72+
let data_float = [
73+
// a, b, result
74+
(0.0f64, 0.0f64, Some(0.0f64)),
75+
(1.0f64, 0.0f64, Some(0.0f64)),
76+
(0.0f64, 1.0f64, Some(0.0f64)),
77+
(-1.0f64, 0.0f64, Some(-1.0f64)),
78+
(0.0f64, -1.0f64, Some(-1.0f64)),
79+
(NAN, NAN, None),
80+
(NAN, 1.0f64, None),
81+
(1.0f64, NAN, None)
82+
];
83+
84+
for &(a, b, result) in data_integer.iter() {
85+
assert!(partial_min(a, b) == result);
86+
}
87+
88+
for &(a, b, result) in data_float.iter() {
89+
assert!(partial_min(a, b) == result);
90+
}
91+
}
92+
93+
#[test]
94+
fn test_partial_max() {
95+
use core::f64::NAN;
96+
let data_integer = [
97+
// a, b, result
98+
(0i, 0i, Some(0i)),
99+
(1i, 0i, Some(1i)),
100+
(0i, 1i, Some(1i)),
101+
(-1i, 0i, Some(0i)),
102+
(0i, -1i, Some(0i))
103+
];
104+
105+
let data_float = [
106+
// a, b, result
107+
(0.0f64, 0.0f64, Some(0.0f64)),
108+
(1.0f64, 0.0f64, Some(1.0f64)),
109+
(0.0f64, 1.0f64, Some(1.0f64)),
110+
(-1.0f64, 0.0f64, Some(0.0f64)),
111+
(0.0f64, -1.0f64, Some(0.0f64)),
112+
(NAN, NAN, None),
113+
(NAN, 1.0f64, None),
114+
(1.0f64, NAN, None)
115+
];
116+
117+
for &(a, b, result) in data_integer.iter() {
118+
assert!(partial_max(a, b) == result);
119+
}
120+
121+
for &(a, b, result) in data_float.iter() {
122+
assert!(partial_max(a, b) == result);
123+
}
124+
}
125+
59126
#[test]
60127
fn test_user_defined_eq() {
61128
// Our type.

0 commit comments

Comments
 (0)