From 45c9f6a0999fa69bb6c60f6b2c432f84dca7be87 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 23 Jan 2013 12:21:58 -0500 Subject: [PATCH] add find method to the core::container::Map trait --- src/libcore/container.rs | 5 +++++ src/libcore/hashmap.rs | 40 ++++++++++++++++++++-------------------- src/libstd/treemap.rs | 40 ++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 062416838cc..272a2efc035 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -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: 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. diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index c3314d3c352..b8e859d7eb0 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -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 = 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 = 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, diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index fa9f8dc805e..efe36ba4a69 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -98,6 +98,26 @@ impl TreeMap: Map { /// 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> = &self.root; + loop { + match *current { + Some(ref r) => { + let r: &self/~TreeNode = 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 TreeMap { 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> = &self.root; - loop { - match *current { - Some(ref r) => { - let r: &self/~TreeNode = 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 {