add find method to the core::container::Map trait

This commit is contained in:
Daniel Micay 2013-01-23 12:21:58 -05:00
parent ee0a8c68ab
commit 45c9f6a099
3 changed files with 45 additions and 40 deletions

View File

@ -13,6 +13,8 @@
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
use option::Option;
pub trait Container {
/// Return the number of elements in the container
pure fn len(&self) -> uint;
@ -39,6 +41,9 @@ pub trait Map<K, V>: Mutable {
/// Visit all values
pure fn each_value(&self, f: fn(&V) -> bool);
/// Return the value corresponding to the key in the map
pure fn find(&self, key: &K) -> Option<&self/V>;
/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.

View File

@ -300,6 +300,26 @@ pub mod linear {
self.each(|_k, v| blk(v))
}
pure fn find(&self, k: &K) -> Option<&self/V> {
match self.bucket_for_key(self.buckets, k) {
FoundEntry(idx) => {
match self.buckets[idx] {
Some(ref bkt) => {
// FIXME(#3148)---should be inferred
let bkt: &self/Bucket<K,V> = bkt;
Some(&bkt.value)
}
None => {
fail ~"LinearMap::find: internal logic error"
}
}
}
TableFull | FoundHole(_) => {
None
}
}
}
fn insert(&mut self, k: K, v: V) -> bool {
if self.size >= self.resize_at {
// n.b.: We could also do this after searching, so
@ -369,26 +389,6 @@ pub mod linear {
}
}
pure fn find(&self, k: &K) -> Option<&self/V> {
match self.bucket_for_key(self.buckets, k) {
FoundEntry(idx) => {
match self.buckets[idx] {
Some(ref bkt) => {
// FIXME(#3148)---should be inferred
let bkt: &self/Bucket<K,V> = bkt;
Some(&bkt.value)
}
None => {
fail ~"LinearMap::find: internal logic error"
}
}
}
TableFull | FoundHole(_) => {
None
}
}
}
pure fn get(&self, k: &K) -> &self/V {
match self.find(k) {
Some(v) => v,

View File

@ -98,6 +98,26 @@ impl <K: Ord, V> TreeMap<K, V>: Map<K, V> {
/// Visit all values in order
pure fn each_value(&self, f: fn(&V) -> bool) { self.each(|_, v| f(v)) }
/// Return the value corresponding to the key in the map
pure fn find(&self, key: &K) -> Option<&self/V> {
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
loop {
match *current {
Some(ref r) => {
let r: &self/~TreeNode<K, V> = r; // FIXME: #3148
if *key < r.key {
current = &r.left;
} else if r.key < *key {
current = &r.right;
} else {
return Some(&r.value);
}
}
None => return None
}
}
}
/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.
@ -136,26 +156,6 @@ impl <K: Ord, V> TreeMap<K, V> {
self.each_reverse(|_, v| f(v))
}
/// Return the value corresponding to the key in the map
pure fn find(&self, key: &K) -> Option<&self/V> {
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
loop {
match *current {
Some(ref r) => {
let r: &self/~TreeNode<K, V> = r; // FIXME: #3148
if *key < r.key {
current = &r.left;
} else if r.key < *key {
current = &r.right;
} else {
return Some(&r.value);
}
}
None => return None
}
}
}
/// Get a lazy iterator over the key-value pairs in the map.
/// Requires that it be frozen (immutable).
pure fn iter(&self) -> TreeMapIterator/&self<K, V> {