1
Fork 0

core: Trait-ify various overloaded operators

This commit is contained in:
Patrick Walton 2012-07-27 14:51:19 -07:00
parent b6aadf56c8
commit 01e2471cb7
7 changed files with 55 additions and 5 deletions

View file

@ -310,3 +310,10 @@ impl extensions<A:copy> for dvec<A> {
do self.swap |v| { vec::reachi(v, f); v } do self.swap |v| { vec::reachi(v, f); v }
} }
} }
impl extensions<A:copy> of ops::index<uint,A> for dvec<A> {
pure fn index(&&idx: uint) -> A {
self.get_elt(idx)
}
}

View file

@ -1897,7 +1897,6 @@ trait unique_str {
fn trim() -> self; fn trim() -> self;
fn trim_left() -> self; fn trim_left() -> self;
fn trim_right() -> self; fn trim_right() -> self;
pure fn +(rhs: &str) -> self;
} }
/// Extension methods for strings /// Extension methods for strings
@ -1919,6 +1918,13 @@ impl extensions of unique_str for ~str {
} }
} }
impl extensions of ops::add<&str,~str> for ~str {
#[inline(always)]
pure fn add(rhs: &str) -> ~str {
append(self, rhs)
}
}
trait str_slice { trait str_slice {
fn all(it: fn(char) -> bool) -> bool; fn all(it: fn(char) -> bool) -> bool;
fn any(it: fn(char) -> bool) -> bool; fn any(it: fn(char) -> bool) -> bool;

View file

@ -1299,6 +1299,13 @@ impl extensions<T: copy> of vec_concat<T> for ~[T] {
} }
} }
impl extensions<T: copy> of ops::add<&[const T],~[T]> for ~[T] {
#[inline(always)]
pure fn add(rhs: &[const T]) -> ~[T] {
append(self, rhs)
}
}
impl extensions<T: copy> of vec_concat<T> for ~[mut T] { impl extensions<T: copy> of vec_concat<T> for ~[mut T] {
#[inline(always)] #[inline(always)]
pure fn +(rhs: &[const T]) -> ~[mut T] { pure fn +(rhs: &[const T]) -> ~[mut T] {
@ -1306,6 +1313,13 @@ impl extensions<T: copy> of vec_concat<T> for ~[mut T] {
} }
} }
impl extensions<T: copy> of ops::add<&[const T],~[mut T]> for ~[mut T] {
#[inline(always)]
pure fn add(rhs: &[const T]) -> ~[mut T] {
append_mut(self, rhs)
}
}
trait const_vector { trait const_vector {
pure fn is_empty() -> bool; pure fn is_empty() -> bool;
pure fn is_not_empty() -> bool; pure fn is_not_empty() -> bool;

View file

@ -239,7 +239,7 @@ trait methods {
fn union(rhs: bitv) -> bool; fn union(rhs: bitv) -> bool;
fn intersect(rhs: bitv) -> bool; fn intersect(rhs: bitv) -> bool;
fn assign(rhs: bitv) -> bool; fn assign(rhs: bitv) -> bool;
fn get(i: uint) -> bool; pure fn get(i: uint) -> bool;
fn [](i: uint) -> bool; fn [](i: uint) -> bool;
fn eq(rhs: bitv) -> bool; fn eq(rhs: bitv) -> bool;
fn clear(); fn clear();
@ -261,7 +261,7 @@ impl of methods for bitv {
fn union(rhs: bitv) -> bool { union(self, rhs) } fn union(rhs: bitv) -> bool { union(self, rhs) }
fn intersect(rhs: bitv) -> bool { intersect(self, rhs) } fn intersect(rhs: bitv) -> bool { intersect(self, rhs) }
fn assign(rhs: bitv) -> bool { assign(self, rhs) } fn assign(rhs: bitv) -> bool { assign(self, rhs) }
fn get(i: uint) -> bool { get(self, i) } pure fn get(i: uint) -> bool { get(self, i) }
fn [](i: uint) -> bool { self.get(i) } fn [](i: uint) -> bool { self.get(i) }
fn eq(rhs: bitv) -> bool { equal(self, rhs) } fn eq(rhs: bitv) -> bool { equal(self, rhs) }
fn clear() { clear(self) } fn clear() { clear(self) }
@ -285,6 +285,12 @@ impl of methods for bitv {
} }
} }
impl extensions of ops::index<uint,bool> for bitv {
pure fn index(&&i: uint) -> bool {
self.get(i)
}
}
impl of to_str::to_str for bitv { impl of to_str::to_str for bitv {
fn to_str() -> ~str { to_str(self) } fn to_str() -> ~str { to_str(self) }
} }

View file

@ -314,6 +314,14 @@ mod chained {
} }
} }
impl hashmap<K, V: copy> of ops::index<K, V> for t<K, V> {
pure fn index(k: K) -> V {
unchecked {
self.get(k)
}
}
}
fn chains<K,V>(nchains: uint) -> ~[mut chain<K,V>] { fn chains<K,V>(nchains: uint) -> ~[mut chain<K,V>] {
ret vec::to_mut(vec::from_elem(nchains, absent)); ret vec::to_mut(vec::from_elem(nchains, absent));

View file

@ -35,7 +35,7 @@ fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
* Get the value for the specified key. If the key does not exist * Get the value for the specified key. If the key does not exist
* in the map then returns none * in the map then returns none
*/ */
fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> { pure fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
if key < self.v.len() { ret self.v.get_elt(key); } if key < self.v.len() { ret self.v.get_elt(key); }
ret none::<T>; ret none::<T>;
} }
@ -47,7 +47,7 @@ fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
* *
* If the key does not exist in the map * If the key does not exist in the map
*/ */
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T { pure fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
alt find(self, key) { alt find(self, key) {
none { #error("smallintmap::get(): key not present"); fail; } none { #error("smallintmap::get(): key not present"); fail; }
some(v) { ret v; } some(v) { ret v; }
@ -114,6 +114,14 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
} }
} }
impl extensions<V: copy> of ops::index<uint, V> for smallintmap<V> {
pure fn index(&&key: uint) -> V {
unchecked {
get(self, key)
}
}
}
/// Cast the given smallintmap to a map::map /// Cast the given smallintmap to a map::map
fn as_map<V: copy>(s: smallintmap<V>) -> map::map<uint, V> { fn as_map<V: copy>(s: smallintmap<V>) -> map::map<uint, V> {
s as map::map::<uint, V> s as map::map::<uint, V>

View file

@ -16,6 +16,7 @@ import syntax::util::interner;
import util::ppaux::ty_to_str; import util::ppaux::ty_to_str;
import syntax::codemap::span; import syntax::codemap::span;
import dvec::{dvec, extensions}; import dvec::{dvec, extensions};
import vec::extensions;
import std::map::hashmap; import std::map::hashmap;
import option::is_some; import option::is_some;