Made Map.contains_key, contains_key_ref, and get pure.
This commit is contained in:
parent
5005be67cc
commit
ec8bfdd63c
@ -48,7 +48,7 @@ impl<T> Data<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ pub mod chained {
|
||||
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 @@ pub mod chained {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
|
||||
}
|
||||
}
|
||||
|
||||
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 pure fn get<T: Copy>(self: SmallIntMap<T>, key: uint) -> 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 @@ impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
|
||||
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 @@ impl<T: Copy> cat<T> : Map<int, T> {
|
||||
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"; }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user