提交 68c852ad 编写于 作者: J Jesse Jones 提交者: Brian Anderson

Made Map.contains_key, contains_key_ref, and get pure.

上级 1a1e99c2
......@@ -48,7 +48,7 @@ fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
}
}
fn borrow_const<R>(op: &fn(t: &const T) -> R) -> R {
pure fn borrow_const<R>(op: &fn(t: &const T) -> R) -> R {
op(&const self.value)
}
......
......@@ -30,17 +30,17 @@ pub trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
fn insert(v: K, v: V) -> bool;
/// Returns true if the map contains a value for the specified key
fn contains_key(key: K) -> bool;
pure fn contains_key(key: K) -> bool;
/// Returns true if the map contains a value for the specified
/// key, taking the key by reference.
fn contains_key_ref(key: &K) -> bool;
pure fn contains_key_ref(key: &K) -> bool;
/**
* Get the value for the specified key. Fails if the key does not exist in
* the map.
*/
fn get(key: K) -> V;
pure fn get(key: K) -> V;
/**
* Get the value for the specified key. If the key does not exist in
......@@ -200,11 +200,11 @@ fn rehash() {
impl<K:Eq IterBytes Hash Copy, V: Copy> T<K, V>: Map<K, V> {
pure fn size() -> uint { self.count }
fn contains_key(k: K) -> bool {
pure fn contains_key(k: K) -> bool {
self.contains_key_ref(&k)
}
fn contains_key_ref(k: &K) -> bool {
pure fn contains_key_ref(k: &K) -> bool {
let hash = k.hash_keyed(0,0) as uint;
match self.search_tbl(k, hash) {
NotFound => false,
......@@ -264,7 +264,7 @@ fn insert(k: K, v: V) -> bool {
}
}
fn get(k: K) -> V {
pure fn get(k: K) -> V {
let opt_v = self.find(k);
if opt_v.is_none() {
fail fmt!("Key not found in table: %?", k);
......@@ -421,19 +421,19 @@ fn insert(key: K, value: V) -> bool {
}
}
fn contains_key(key: K) -> bool {
pure fn contains_key(key: K) -> bool {
do self.borrow_const |p| {
p.contains_key(&key)
}
}
fn contains_key_ref(key: &K) -> bool {
pure fn contains_key_ref(key: &K) -> bool {
do self.borrow_const |p| {
p.contains_key(key)
}
}
fn get(key: K) -> V {
pure fn get(key: K) -> V {
do self.borrow_const |p| {
p.get(&key)
}
......
......@@ -60,7 +60,7 @@ pub fn insert<T: Copy>(self: SmallIntMap<T>, key: uint, val: T) {
}
/// Returns true if the map contains a value for the specified key
pub fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
pub pure fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
return !find(self, key).is_none();
}
......@@ -93,13 +93,13 @@ fn remove(key: uint) -> bool {
fn clear() {
self.v.set(~[]);
}
fn contains_key(key: uint) -> bool {
pure fn contains_key(key: uint) -> bool {
contains_key(self, key)
}
fn contains_key_ref(key: &uint) -> bool {
pure fn contains_key_ref(key: &uint) -> bool {
contains_key(self, *key)
}
fn get(key: uint) -> V { get(self, key) }
pure fn get(key: uint) -> V { get(self, key) }
pure fn find(key: uint) -> Option<V> { find(self, key) }
fn rehash() { fail }
......
......@@ -47,10 +47,10 @@ fn insert(+k: int, +_v: T) -> bool {
self.meows += k;
true
}
fn contains_key(+k: int) -> bool { k <= self.meows }
fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) }
pure fn contains_key(+k: int) -> bool { k <= self.meows }
pure fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) }
fn get(+k:int) -> T { match self.find(k) {
pure fn get(+k:int) -> T { match self.find(k) {
Some(v) => { v }
None => { fail ~"epic fail"; }
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册