1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 The `Ord` and `Eq` comparison traits
15 This module contains the definition of both `Ord` and `Eq` which define
16 the common interfaces for doing comparison. Both are language items
17 that the compiler uses to implement the comparison operators. Rust code
18 may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators,
19 and `Eq` to overload the `==` and `!=` operators.
23 #[allow(missing_doc)];
26 * Trait for values that can be compared for equality and inequality.
28 * This trait allows partial equality, where types can be unordered instead of strictly equal or
29 * unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
30 * evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
32 * Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
34 * Eventually, this will be implemented by default for types that implement `TotalEq`.
38 fn eq(&self, other: &Self) -> bool;
41 fn ne(&self, other: &Self) -> bool { !self.eq(other) }
44 /// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
46 fn equals(&self, other: &Self) -> bool;
49 macro_rules! totaleq_impl(
53 fn equals(&self, other: &$t) -> bool { *self == *other }
75 /// Trait for testing approximate equality
76 pub trait ApproxEq<Eps> {
77 fn approx_epsilon() -> Eps;
78 fn approx_eq(&self, other: &Self) -> bool;
79 fn approx_eq_eps(&self, other: &Self, approx_epsilon: &Eps) -> bool;
82 #[deriving(Clone, Eq)]
83 pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
85 /// Trait for types that form a total order
86 pub trait TotalOrd: TotalEq {
87 fn cmp(&self, other: &Self) -> Ordering;
90 impl TotalEq for Ordering {
92 fn equals(&self, other: &Ordering) -> bool {
96 impl TotalOrd for Ordering {
98 fn cmp(&self, other: &Ordering) -> Ordering {
99 (*self as int).cmp(&(*other as int))
103 impl Ord for Ordering {
105 fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
108 macro_rules! totalord_impl(
110 impl TotalOrd for $t {
112 fn cmp(&self, other: &$t) -> Ordering {
113 if *self < *other { Less }
114 else if *self > *other { Greater }
136 /// Compares (a1, b1) against (a2, b2), where the a values are more significant.
137 pub fn cmp2<A:TotalOrd,B:TotalOrd>(
139 a2: &A, b2: &B) -> Ordering
149 Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
150 lexical ordering on a type `(int, int)`.
153 pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
161 * Trait for values that can be compared for a sort-order.
163 * Ord only requires implementation of the `lt` method,
164 * with the others generated from default implementations.
166 * However it remains possible to implement the others separately,
167 * for compatibility with floating-point NaN semantics
168 * (cf. IEEE 754-2008 section 5.11).
172 fn lt(&self, other: &Self) -> bool;
174 fn le(&self, other: &Self) -> bool { !other.lt(self) }
176 fn gt(&self, other: &Self) -> bool { other.lt(self) }
178 fn ge(&self, other: &Self) -> bool { !self.lt(other) }
181 /// The equivalence relation. Two values may be equivalent even if they are
182 /// of different types. The most common use case for this relation is
183 /// container types; e.g. it is often desirable to be able to use `&str`
184 /// values to look up entries in a container with `~str` keys.
186 fn equiv(&self, other: &T) -> bool;
190 pub fn min<T:Ord>(v1: T, v2: T) -> T {
191 if v1 < v2 { v1 } else { v2 }
195 pub fn max<T:Ord>(v1: T, v2: T) -> T {
196 if v1 > v2 { v1 } else { v2 }
201 use super::lexical_ordering;
204 fn test_int_totalord() {
205 assert_eq!(5.cmp(&10), Less);
206 assert_eq!(10.cmp(&5), Greater);
207 assert_eq!(5.cmp(&5), Equal);
208 assert_eq!((-5).cmp(&12), Less);
209 assert_eq!(12.cmp(-5), Greater);
214 assert_eq!(cmp2(1, 2, 3, 4), Less);
215 assert_eq!(cmp2(3, 2, 3, 4), Less);
216 assert_eq!(cmp2(5, 2, 3, 4), Greater);
217 assert_eq!(cmp2(5, 5, 5, 4), Greater);
221 fn test_int_totaleq() {
222 assert!(5.equals(&5));
223 assert!(!2.equals(&17));
227 fn test_ordering_order() {
228 assert!(Less < Equal);
229 assert_eq!(Greater.cmp(&Less), Greater);
233 fn test_lexical_ordering() {
234 fn t(o1: Ordering, o2: Ordering, e: Ordering) {
235 assert_eq!(lexical_ordering(o1, o2), e);
238 let xs = [Less, Equal, Greater];
239 for &o in xs.iter() {
242 t(Greater, o, Greater);