Skip to content

Commit

Permalink
Add partial_min/max to libcore/cmp
Browse files Browse the repository at this point in the history
Add partial_min/max to libcore/cmp

Match against None and mark as experimental

Shortened documentation.

Removed whitespace
  • Loading branch information
tilpner committed Sep 25, 2014
1 parent d299baf commit 29c2d3d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/libcore/cmp.rs
Expand Up @@ -39,7 +39,7 @@

#![stable]

use option::{Option, Some};
use option::{Option, Some, None};

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

/// Compare and return the minimum of two values if there is one.
///
/// Returns the first argument if the comparison determines them to be equal.
#[inline]
#[experimental]
pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
match v1.partial_cmp(&v2) {
Some(Less) | Some(Equal) => Some(v1),
Some(Greater) => Some(v2),
None => None
}
}

/// Compare and return the maximum of two values if there is one.
///
/// Returns the first argument if the comparison determines them to be equal.
#[inline]
#[experimental]
pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
match v1.partial_cmp(&v2) {
Some(Less) => Some(v2),
Some(Equal) | Some(Greater) => Some(v1),
None => None
}
}

// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
mod impls {
use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering,
Expand Down
67 changes: 67 additions & 0 deletions src/libcoretest/cmp.rs
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use core::cmp::lexical_ordering;
use core::cmp::{ partial_min, partial_max };

#[test]
fn test_int_totalord() {
Expand Down Expand Up @@ -56,6 +57,72 @@ fn test_lexical_ordering() {
}
}

#[test]
fn test_partial_min() {
use core::f64::NAN;
let data_integer = [
// a, b, result
(0i, 0i, Some(0i)),
(1i, 0i, Some(0i)),
(0i, 1i, Some(0i)),
(-1i, 0i, Some(-1i)),
(0i, -1i, Some(-1i))
];

let data_float = [
// a, b, result
(0.0f64, 0.0f64, Some(0.0f64)),
(1.0f64, 0.0f64, Some(0.0f64)),
(0.0f64, 1.0f64, Some(0.0f64)),
(-1.0f64, 0.0f64, Some(-1.0f64)),
(0.0f64, -1.0f64, Some(-1.0f64)),
(NAN, NAN, None),
(NAN, 1.0f64, None),
(1.0f64, NAN, None)
];

for &(a, b, result) in data_integer.iter() {
assert!(partial_min(a, b) == result);
}

for &(a, b, result) in data_float.iter() {
assert!(partial_min(a, b) == result);
}
}

#[test]
fn test_partial_max() {
use core::f64::NAN;
let data_integer = [
// a, b, result
(0i, 0i, Some(0i)),
(1i, 0i, Some(1i)),
(0i, 1i, Some(1i)),
(-1i, 0i, Some(0i)),
(0i, -1i, Some(0i))
];

let data_float = [
// a, b, result
(0.0f64, 0.0f64, Some(0.0f64)),
(1.0f64, 0.0f64, Some(1.0f64)),
(0.0f64, 1.0f64, Some(1.0f64)),
(-1.0f64, 0.0f64, Some(0.0f64)),
(0.0f64, -1.0f64, Some(0.0f64)),
(NAN, NAN, None),
(NAN, 1.0f64, None),
(1.0f64, NAN, None)
];

for &(a, b, result) in data_integer.iter() {
assert!(partial_max(a, b) == result);
}

for &(a, b, result) in data_float.iter() {
assert!(partial_max(a, b) == result);
}
}

#[test]
fn test_user_defined_eq() {
// Our type.
Expand Down

5 comments on commit 29c2d3d

@bors
Copy link
Contributor

@bors bors commented on 29c2d3d Sep 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 29c2d3d Sep 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging hoeppnertill/rust/master = 29c2d3d into auto

@bors
Copy link
Contributor

@bors bors commented on 29c2d3d Sep 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hoeppnertill/rust/master = 29c2d3d merged ok, testing candidate = 3be6a2f

@bors
Copy link
Contributor

@bors bors commented on 29c2d3d Sep 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 29c2d3d Sep 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 3be6a2f

Please sign in to comment.