libcore: combine cmp::Ordering instances in lexical order.
This commit is contained in:
parent
3698ea7e54
commit
85b82c763b
@ -116,6 +116,19 @@ fn cmp(&self, other: &$t) -> Ordering {
|
||||
totalord_impl!(int)
|
||||
totalord_impl!(uint)
|
||||
|
||||
/**
|
||||
Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
|
||||
lexical ordering on a type `(int, int)`.
|
||||
*/
|
||||
// used in deriving code in libsyntax
|
||||
#[inline(always)]
|
||||
pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
|
||||
match o1 {
|
||||
Equal => o2,
|
||||
_ => o1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trait for values that can be compared for a sort-order.
|
||||
*
|
||||
@ -184,6 +197,8 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::lexical_ordering;
|
||||
|
||||
#[test]
|
||||
fn test_int_totalord() {
|
||||
assert_eq!(5.cmp(&10), Less);
|
||||
@ -204,4 +219,16 @@ fn test_ordering_order() {
|
||||
assert!(Less < Equal);
|
||||
assert_eq!(Greater.cmp(&Less), Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lexical_ordering() {
|
||||
fn t(o1: Ordering, o2: Ordering, e: Ordering) {
|
||||
assert_eq!(lexical_ordering(o1, o2), e);
|
||||
}
|
||||
for [Less, Equal, Greater].each |&o| {
|
||||
t(Less, o, Less);
|
||||
t(Equal, o, o);
|
||||
t(Greater, o, Greater);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user