mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-11 23:33:00 +00:00
add find method to the core::container::Map trait
This commit is contained in:
parent
ee0a8c68ab
commit
45c9f6a099
@ -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.
|
||||
|
@ -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,
|
||||
|
@ -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> {
|
||||
|
Loading…
Reference in New Issue
Block a user