From 38f39ac540d2a8b42c650e3aae9eaa715d47c554 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 24 Mar 2013 20:40:17 -0400 Subject: [PATCH] expose find_mut in the Map trait --- src/libcore/container.rs | 2 +- src/libcore/hashmap.rs | 22 ++++++++--------- src/libcore/trie.rs | 12 +++++----- src/libstd/smallintmap.rs | 24 +++++++++---------- src/libstd/treemap.rs | 12 +++++----- .../class-impl-very-parameterized-trait.rs | 2 ++ 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 9eba2c7105c..e20821b919b 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -42,7 +42,7 @@ pub trait Map: Mutable { fn find(&self, key: &K) -> Option<&'self V>; /// Return a mutable reference to the value corresponding to the key - //fn find_mut(&mut self, key: &K) -> Option<&'self mut V>; + fn find_mut(&mut self, key: &K) -> Option<&'self mut 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 diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index b980082a88c..8c290553a45 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -355,6 +355,17 @@ pub mod linear { } } + /// Return a mutable reference to the value corresponding to the key + fn find_mut(&mut self, k: &K) -> Option<&'self mut V> { + let idx = match self.bucket_for_key(k) { + FoundEntry(idx) => idx, + TableFull | FoundHole(_) => return None + }; + unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker + Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx))) + } + } + /// 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. @@ -419,17 +430,6 @@ pub mod linear { old_value } - /// Return a mutable reference to the value corresponding to the key - fn find_mut(&mut self, k: &K) -> Option<&'self mut V> { - let idx = match self.bucket_for_key(k) { - FoundEntry(idx) => idx, - TableFull | FoundHole(_) => return None - }; - unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker - Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx))) - } - } - /// Return the value corresponding to the key in the map, or insert /// and return the value if it doesn't exist. fn find_or_insert(&mut self, k: K, v: V) -> &'self V { diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 007bafcd03d..5921ae5b3f5 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -111,6 +111,12 @@ impl Map for TrieMap { } } + /// Return a mutable reference to the value corresponding to the key + #[inline(always)] + fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> { + find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1) + } + /// 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. @@ -153,12 +159,6 @@ pub impl TrieMap { fn each_value_reverse(&self, f: &fn(&T) -> bool) { self.each_reverse(|&(_, v)| f(v)) } - - /// Return a mutable reference to the value corresponding to the key - #[inline(always)] - fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> { - find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1) - } } pub struct TrieSet { diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index fffd6c9ee4f..4ad8d38b072 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -108,6 +108,18 @@ impl Map for SmallIntMap { } } + /// Return a mutable reference to the value corresponding to the key + fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> { + if *key < self.v.len() { + match self.v[*key] { + Some(ref mut value) => Some(value), + None => None + } + } else { + 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. @@ -140,18 +152,6 @@ pub impl SmallIntMap { fn get(&self, key: &uint) -> &'self V { self.find(key).expect("key not present") } - - /// Return a mutable reference to the value corresponding to the key - fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> { - if *key < self.v.len() { - match self.v[*key] { - Some(ref mut value) => Some(value), - None => None - } - } else { - None - } - } } pub impl SmallIntMap { diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index b77037ba3ad..fccf58ddb6f 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -152,6 +152,12 @@ impl Map for TreeMap { } } + /// Return a mutable reference to the value corresponding to the key + #[inline(always)] + fn find_mut(&mut self, key: &K) -> Option<&'self mut V> { + find_mut(&mut self.root, key) + } + /// 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. @@ -189,12 +195,6 @@ pub impl TreeMap { fn iter(&self) -> TreeMapIterator<'self, K, V> { TreeMapIterator{stack: ~[], node: &self.root} } - - /// Return a mutable reference to the value corresponding to the key - #[inline(always)] - fn find_mut(&mut self, key: &K) -> Option<&'self mut V> { - find_mut(&mut self.root, key) - } } /// Lazy forward iterator over a map diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index 40dd1a46574..281d520be0f 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -98,6 +98,8 @@ impl Map for cat { } } + fn find_mut(&mut self, k: &int) -> Option<&'self mut T> { fail!() } + fn remove(&mut self, k: &int) -> bool { if self.find(k).is_some() { self.meows -= *k; true