diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-08-29 19:23:15 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-08-29 19:23:15 -0700 |
| commit | a1c11cab2dc56483cf9fe6d25af43b2f602d5155 (patch) | |
| tree | 7eb884301bb31d4bed8d99c26cd38109b64ac41e | |
| parent | 70d3633c0b66c247b97f721653d33264ddfc680f (diff) | |
| download | rust-a1c11cab2dc56483cf9fe6d25af43b2f602d5155.tar.gz rust-a1c11cab2dc56483cf9fe6d25af43b2f602d5155.zip | |
rustc: Make `<=`, `>=`, and `>` use traits as well
| -rw-r--r-- | src/cargo/cargo.rs | 18 | ||||
| -rw-r--r-- | src/libcore/box.rs | 3 | ||||
| -rw-r--r-- | src/libcore/cmp.rs | 15 | ||||
| -rw-r--r-- | src/libcore/float.rs | 3 | ||||
| -rw-r--r-- | src/libcore/int-template.rs | 7 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 22 | ||||
| -rw-r--r-- | src/libcore/str.rs | 43 | ||||
| -rw-r--r-- | src/libcore/tuple.rs | 34 | ||||
| -rw-r--r-- | src/libcore/uint-template.rs | 7 | ||||
| -rw-r--r-- | src/libcore/uniq.rs | 3 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 48 | ||||
| -rw-r--r-- | src/libstd/test.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/ast_util.rs | 5 | ||||
| -rw-r--r-- | src/rustc/middle/resolve.rs | 3 |
14 files changed, 192 insertions, 23 deletions
diff --git a/src/cargo/cargo.rs b/src/cargo/cargo.rs index 7e1cb4e1a18..d876a9c679f 100644 --- a/src/cargo/cargo.rs +++ b/src/cargo/cargo.rs @@ -42,6 +42,24 @@ impl package : cmp::Ord { if self.versions.lt(other.versions) { return true; } return false; } + pure fn le(&&other: package) -> bool { + if self.name.lt(other.name) { return true; } + if other.name.lt(self.name) { return false; } + if self.uuid.lt(other.uuid) { return true; } + if other.uuid.lt(self.uuid) { return false; } + if self.url.lt(other.url) { return true; } + if other.url.lt(self.url) { return false; } + if self.method.lt(other.method) { return true; } + if other.method.lt(self.method) { return false; } + if self.description.lt(other.description) { return true; } + if other.description.lt(self.description) { return false; } + if self.tags.lt(other.tags) { return true; } + if other.tags.lt(self.tags) { return false; } + if self.versions.le(other.versions) { return true; } + return false; + } + pure fn ge(&&other: package) -> bool { !other.lt(self) } + pure fn gt(&&other: package) -> bool { !other.le(self) } } type local_package = { diff --git a/src/libcore/box.rs b/src/libcore/box.rs index 7a1376898db..73676f1bc4c 100644 --- a/src/libcore/box.rs +++ b/src/libcore/box.rs @@ -19,6 +19,9 @@ impl<T:Eq> @const T : Eq { impl<T:Ord> @const T : Ord { pure fn lt(&&other: @const T) -> bool { *self < *other } + pure fn le(&&other: @const T) -> bool { *self <= *other } + pure fn ge(&&other: @const T) -> bool { *self >= *other } + pure fn gt(&&other: @const T) -> bool { *self > *other } } #[test] diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index ce84674ae83..6647963bc5e 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -9,11 +9,17 @@ #[lang="ord"] trait Ord { pure fn lt(&&other: self) -> bool; + pure fn le(&&other: self) -> bool; + pure fn ge(&&other: self) -> bool; + pure fn gt(&&other: self) -> bool; } #[cfg(test)] trait Ord { pure fn lt(&&other: self) -> bool; + pure fn le(&&other: self) -> bool; + pure fn ge(&&other: self) -> bool; + pure fn gt(&&other: self) -> bool; } #[cfg(notest)] @@ -38,3 +44,12 @@ pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool { pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool { v1.eq(v2) } + +pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool { + v1.ge(v2) +} + +pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool { + v1.gt(v2) +} + diff --git a/src/libcore/float.rs b/src/libcore/float.rs index e3f274ebe59..32fbcf209f6 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -421,6 +421,9 @@ impl float: Eq { impl float: Ord { pure fn lt(&&other: float) -> bool { self < other } + pure fn le(&&other: float) -> bool { self <= other } + pure fn ge(&&other: float) -> bool { self >= other } + pure fn gt(&&other: float) -> bool { self > other } } impl float: num::Num { diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index fb75438aa65..6b572d12626 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -68,9 +68,10 @@ pure fn abs(i: T) -> T { } impl T: Ord { - pure fn lt(&&other: T) -> bool { - return self < other; - } + pure fn lt(&&other: T) -> bool { return self < other; } + pure fn le(&&other: T) -> bool { return self <= other; } + pure fn ge(&&other: T) -> bool { return self >= other; } + pure fn gt(&&other: T) -> bool { return self > other; } } impl T: Eq { diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 78469e6095d..0c4e9a7c356 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -188,6 +188,21 @@ impl<T> *const T : Ord { let b: uint = unsafe::reinterpret_cast(other); return a < b; } + pure fn le(&&other: *const T) -> bool unsafe { + let a: uint = unsafe::reinterpret_cast(self); + let b: uint = unsafe::reinterpret_cast(other); + return a <= b; + } + pure fn ge(&&other: *const T) -> bool unsafe { + let a: uint = unsafe::reinterpret_cast(self); + let b: uint = unsafe::reinterpret_cast(other); + return a >= b; + } + pure fn gt(&&other: *const T) -> bool unsafe { + let a: uint = unsafe::reinterpret_cast(self); + let b: uint = unsafe::reinterpret_cast(other); + return a > b; + } } // Equality for region pointers @@ -199,9 +214,10 @@ impl<T:Eq> &const T : Eq { // Comparison for region pointers impl<T:Ord> &const T : Ord { - pure fn lt(&&other: &const T) -> bool { - return *self < *other; - } + pure fn lt(&&other: &const T) -> bool { *self < *other } + pure fn le(&&other: &const T) -> bool { *self <= *other } + pure fn ge(&&other: &const T) -> bool { *self >= *other } + pure fn gt(&&other: &const T) -> bool { *self > *other } } #[test] diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 654992933d3..4564bd2cc13 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -707,7 +707,30 @@ pure fn lt(a: &str, b: &str) -> bool { } /// Bytewise less than or equal -pure fn le(a: &~str, b: &~str) -> bool { *a <= *b } +pure fn le(a: &str, b: &str) -> bool { + let (a_len, b_len) = (a.len(), b.len()); + let mut end = uint::min(&a_len, &b_len); + + let mut i = 0; + while i < end { + let (c_a, c_b) = (a[i], b[i]); + if c_a < c_b { return true; } + if c_a > c_b { return false; } + i += 1; + } + + return a_len <= b_len; +} + +/// Bytewise greater than or equal +pure fn ge(a: &str, b: &str) -> bool { + !lt(b, a) +} + +/// Bytewise greater than +pure fn gt(a: &str, b: &str) -> bool { + !le(b, a) +} impl &str: Eq { #[inline(always)] @@ -733,16 +756,34 @@ impl @str: Eq { impl ~str : Ord { #[inline(always)] pure fn lt(&&other: ~str) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: ~str) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: ~str) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: ~str) -> bool { gt(self, other) } } impl &str : Ord { #[inline(always)] pure fn lt(&&other: &str) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: &str) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: &str) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: &str) -> bool { gt(self, other) } } impl @str : Ord { #[inline(always)] pure fn lt(&&other: @str) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: @str) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: @str) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: @str) -> bool { gt(self, other) } } /// String hash function diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index dcec0caa3dc..2f34e69573f 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -96,6 +96,22 @@ impl<A: Ord, B: Ord> (A, B): Ord { } } } + pure fn le(&&other: (A, B)) -> bool { + match self { + (self_a, self_b) => { + match other { + (other_a, other_b) => { + if self_a.lt(other_a) { return true; } + if other_a.lt(self_a) { return false; } + if self_b.le(other_b) { return true; } + return false; + } + } + } + } + } + pure fn ge(&&other: (A, B)) -> bool { !other.lt(self) } + pure fn gt(&&other: (A, B)) -> bool { !other.ge(self) } } impl<A: Eq, B: Eq, C: Eq> (A, B, C): Eq { @@ -133,6 +149,24 @@ impl<A: Ord, B: Ord, C: Ord> (A, B, C): Ord { } } } + pure fn le(&&other: (A, B, C)) -> bool { + match self { + (self_a, self_b, self_c) => { + match other { + (other_a, other_b, other_c) => { + if self_a.lt(other_a) { return true; } + if other_a.lt(self_a) { return false; } + if self_b.lt(other_b) { return true; } + if other_b.lt(self_b) { return false; } + if self_c.le(other_c) { return true; } + return false; + } + } + } + } + } + pure fn ge(&&other: (A, B, C)) -> bool { !other.lt(self) } + pure fn gt(&&other: (A, B, C)) -> bool { !other.ge(self) } } #[test] diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index f95848f2496..5119a5dd562 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -61,9 +61,10 @@ pure fn compl(i: T) -> T { } impl T: Ord { - pure fn lt(&&other: T) -> bool { - return self < other; - } + pure fn lt(&&other: T) -> bool { self < other } + pure fn le(&&other: T) -> bool { self <= other } + pure fn ge(&&other: T) -> bool { self >= other } + pure fn gt(&&other: T) -> bool { self > other } } impl T: Eq { diff --git a/src/libcore/uniq.rs b/src/libcore/uniq.rs index c556fdd7ddb..a5e624079b7 100644 --- a/src/libcore/uniq.rs +++ b/src/libcore/uniq.rs @@ -8,5 +8,8 @@ impl<T:Eq> ~const T : Eq { impl<T:Ord> ~const T : Ord { pure fn lt(&&other: ~const T) -> bool { *self < *other } + pure fn le(&&other: ~const T) -> bool { *self <= *other } + pure fn ge(&&other: ~const T) -> bool { *self >= *other } + pure fn gt(&&other: ~const T) -> bool { *self > *other } } diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index ae08aad8138..dcdea639084 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1443,25 +1443,55 @@ pure fn lt<T: Ord>(a: &[T], b: &[T]) -> bool { return a_len < b_len; } +pure fn le<T: Ord>(a: &[T], b: &[T]) -> bool { + let (a_len, b_len) = (a.len(), b.len()); + let mut end = uint::min(&a_len, &b_len); + + let mut i = 0; + while i < end { + let (c_a, c_b) = (&a[i], &b[i]); + if *c_a < *c_b { return true; } + if *c_a > *c_b { return false; } + i += 1; + } + + return a_len <= b_len; +} + +pure fn ge<T: Ord>(a: &[T], b: &[T]) -> bool { !lt(b, a) } +pure fn gt<T: Ord>(a: &[T], b: &[T]) -> bool { !le(b, a) } + impl<T: Ord> &[T]: Ord { #[inline(always)] - pure fn lt(&&other: &[T]) -> bool { - lt(self, other) - } + pure fn lt(&&other: &[T]) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: &[T]) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: &[T]) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: &[T]) -> bool { gt(self, other) } } impl<T: Ord> ~[T]: Ord { #[inline(always)] - pure fn lt(&&other: ~[T]) -> bool { - lt(self, other) - } + pure fn lt(&&other: ~[T]) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: ~[T]) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: ~[T]) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: ~[T]) -> bool { gt(self, other) } } impl<T: Ord> @[T]: Ord { #[inline(always)] - pure fn lt(&&other: @[T]) -> bool { - lt(self, other) - } + pure fn lt(&&other: @[T]) -> bool { lt(self, other) } + #[inline(always)] + pure fn le(&&other: @[T]) -> bool { le(self, other) } + #[inline(always)] + pure fn ge(&&other: @[T]) -> bool { ge(self, other) } + #[inline(always)] + pure fn gt(&&other: @[T]) -> bool { gt(self, other) } } #[cfg(notest)] diff --git a/src/libstd/test.rs b/src/libstd/test.rs index 2c055e497dd..2c67ae8b262 100644 --- a/src/libstd/test.rs +++ b/src/libstd/test.rs @@ -217,7 +217,7 @@ fn print_failures(st: console_test_state) { st.out.write_line(~"\nfailures:"); let failures = copy st.failures; let failures = vec::map(failures, |test| test.name); - let failures = sort::merge_sort(str::le, failures); + let failures = sort::merge_sort(|x, y| str::le(*x, *y), failures); for vec::each(failures) |name| { st.out.write_line(fmt!(" %s", name)); } @@ -371,7 +371,7 @@ fn filter_tests(opts: test_opts, // Sort the tests alphabetically filtered = { pure fn lteq(t1: &test_desc, t2: &test_desc) -> bool { - str::le(&t1.name, &t2.name) + str::le(t1.name, t2.name) } sort::merge_sort(lteq, filtered) }; diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index e77a3b3068c..8e9ca7bd098 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -106,8 +106,11 @@ pure fn binop_to_method_name(op: binop) -> Option<~str> { shl => return Some(~"shl"), shr => return Some(~"shr"), lt => return Some(~"lt"), + le => return Some(~"le"), + ge => return Some(~"ge"), + gt => return Some(~"gt"), eq => return Some(~"eq"), - and | or | le | ne | ge | gt => return None + and | or | ne => return None } } diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 48e1f1d3790..ba3155bb07d 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -4341,7 +4341,8 @@ struct Resolver { self.add_fixed_trait_for_expr(expr.id, self.lang_items.shr_trait); } - expr_binary(lt, _, _) => { + expr_binary(lt, _, _) | expr_binary(le, _, _) | + expr_binary(ge, _, _) | expr_binary(gt, _, _) => { self.add_fixed_trait_for_expr(expr.id, self.lang_items.ord_trait); } |
