diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-03-24 20:40:17 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-03-24 21:40:16 -0400 |
| commit | 38f39ac540d2a8b42c650e3aae9eaa715d47c554 (patch) | |
| tree | 4c21510053d0949fdccd721272416da9b4f5adda | |
| parent | f0f4a00e88fc374b2b3096789a11bf429d42c3a9 (diff) | |
| download | rust-38f39ac540d2a8b42c650e3aae9eaa715d47c554.tar.gz rust-38f39ac540d2a8b42c650e3aae9eaa715d47c554.zip | |
expose find_mut in the Map trait
| -rw-r--r-- | src/libcore/container.rs | 2 | ||||
| -rw-r--r-- | src/libcore/hashmap.rs | 22 | ||||
| -rw-r--r-- | src/libcore/trie.rs | 12 | ||||
| -rw-r--r-- | src/libstd/smallintmap.rs | 24 | ||||
| -rw-r--r-- | src/libstd/treemap.rs | 12 | ||||
| -rw-r--r-- | src/test/run-pass/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<K, V>: 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<T> Map<uint, T> for TrieMap<T> { } } + /// 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<T> TrieMap<T> { 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<V> Map<uint, V> for SmallIntMap<V> { } } + /// 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<V> SmallIntMap<V> { 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<V:Copy> SmallIntMap<V> { 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<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> { } } + /// 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<K: TotalOrd, V> TreeMap<K, V> { 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<T> Map<int, T> for cat<T> { } } + 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 |
