Skip to content

Commit 45c9f6a

Browse files
committed
add find method to the core::container::Map trait
1 parent ee0a8c6 commit 45c9f6a

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

src/libcore/container.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#[forbid(deprecated_mode)];
1414
#[forbid(deprecated_pattern)];
1515

16+
use option::Option;
17+
1618
pub trait Container {
1719
/// Return the number of elements in the container
1820
pure fn len(&self) -> uint;
@@ -39,6 +41,9 @@ pub trait Map<K, V>: Mutable {
3941
/// Visit all values
4042
pure fn each_value(&self, f: fn(&V) -> bool);
4143

44+
/// Return the value corresponding to the key in the map
45+
pure fn find(&self, key: &K) -> Option<&self/V>;
46+
4247
/// Insert a key-value pair into the map. An existing value for a
4348
/// key is replaced by the new value. Return true if the key did
4449
/// not already exist in the map.

src/libcore/hashmap.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@ pub mod linear {
300300
self.each(|_k, v| blk(v))
301301
}
302302

303+
pure fn find(&self, k: &K) -> Option<&self/V> {
304+
match self.bucket_for_key(self.buckets, k) {
305+
FoundEntry(idx) => {
306+
match self.buckets[idx] {
307+
Some(ref bkt) => {
308+
// FIXME(#3148)---should be inferred
309+
let bkt: &self/Bucket<K,V> = bkt;
310+
Some(&bkt.value)
311+
}
312+
None => {
313+
fail ~"LinearMap::find: internal logic error"
314+
}
315+
}
316+
}
317+
TableFull | FoundHole(_) => {
318+
None
319+
}
320+
}
321+
}
322+
303323
fn insert(&mut self, k: K, v: V) -> bool {
304324
if self.size >= self.resize_at {
305325
// n.b.: We could also do this after searching, so
@@ -369,26 +389,6 @@ pub mod linear {
369389
}
370390
}
371391

372-
pure fn find(&self, k: &K) -> Option<&self/V> {
373-
match self.bucket_for_key(self.buckets, k) {
374-
FoundEntry(idx) => {
375-
match self.buckets[idx] {
376-
Some(ref bkt) => {
377-
// FIXME(#3148)---should be inferred
378-
let bkt: &self/Bucket<K,V> = bkt;
379-
Some(&bkt.value)
380-
}
381-
None => {
382-
fail ~"LinearMap::find: internal logic error"
383-
}
384-
}
385-
}
386-
TableFull | FoundHole(_) => {
387-
None
388-
}
389-
}
390-
}
391-
392392
pure fn get(&self, k: &K) -> &self/V {
393393
match self.find(k) {
394394
Some(v) => v,

src/libstd/treemap.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ impl <K: Ord, V> TreeMap<K, V>: Map<K, V> {
9898
/// Visit all values in order
9999
pure fn each_value(&self, f: fn(&V) -> bool) { self.each(|_, v| f(v)) }
100100

101+
/// Return the value corresponding to the key in the map
102+
pure fn find(&self, key: &K) -> Option<&self/V> {
103+
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
104+
loop {
105+
match *current {
106+
Some(ref r) => {
107+
let r: &self/~TreeNode<K, V> = r; // FIXME: #3148
108+
if *key < r.key {
109+
current = &r.left;
110+
} else if r.key < *key {
111+
current = &r.right;
112+
} else {
113+
return Some(&r.value);
114+
}
115+
}
116+
None => return None
117+
}
118+
}
119+
}
120+
101121
/// Insert a key-value pair into the map. An existing value for a
102122
/// key is replaced by the new value. Return true if the key did
103123
/// not already exist in the map.
@@ -136,26 +156,6 @@ impl <K: Ord, V> TreeMap<K, V> {
136156
self.each_reverse(|_, v| f(v))
137157
}
138158

139-
/// Return the value corresponding to the key in the map
140-
pure fn find(&self, key: &K) -> Option<&self/V> {
141-
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
142-
loop {
143-
match *current {
144-
Some(ref r) => {
145-
let r: &self/~TreeNode<K, V> = r; // FIXME: #3148
146-
if *key < r.key {
147-
current = &r.left;
148-
} else if r.key < *key {
149-
current = &r.right;
150-
} else {
151-
return Some(&r.value);
152-
}
153-
}
154-
None => return None
155-
}
156-
}
157-
}
158-
159159
/// Get a lazy iterator over the key-value pairs in the map.
160160
/// Requires that it be frozen (immutable).
161161
pure fn iter(&self) -> TreeMapIterator/&self<K, V> {

0 commit comments

Comments
 (0)