Skip to content

Commit 38f39ac

Browse files
committed
expose find_mut in the Map trait
1 parent f0f4a00 commit 38f39ac

File tree

6 files changed

+38
-36
lines changed

6 files changed

+38
-36
lines changed

src/libcore/container.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub trait Map<K, V>: Mutable {
4242
fn find(&self, key: &K) -> Option<&'self V>;
4343

4444
/// Return a mutable reference to the value corresponding to the key
45-
//fn find_mut(&mut self, key: &K) -> Option<&'self mut V>;
45+
fn find_mut(&mut self, key: &K) -> Option<&'self mut V>;
4646

4747
/// Insert a key-value pair into the map. An existing value for a
4848
/// key is replaced by the new value. Return true if the key did

src/libcore/hashmap.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,17 @@ pub mod linear {
355355
}
356356
}
357357
358+
/// Return a mutable reference to the value corresponding to the key
359+
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
360+
let idx = match self.bucket_for_key(k) {
361+
FoundEntry(idx) => idx,
362+
TableFull | FoundHole(_) => return None
363+
};
364+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
365+
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
366+
}
367+
}
368+
358369
/// Insert a key-value pair into the map. An existing value for a
359370
/// key is replaced by the new value. Return true if the key did
360371
/// not already exist in the map.
@@ -419,17 +430,6 @@ pub mod linear {
419430
old_value
420431
}
421432
422-
/// Return a mutable reference to the value corresponding to the key
423-
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
424-
let idx = match self.bucket_for_key(k) {
425-
FoundEntry(idx) => idx,
426-
TableFull | FoundHole(_) => return None
427-
};
428-
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
429-
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
430-
}
431-
}
432-
433433
/// Return the value corresponding to the key in the map, or insert
434434
/// and return the value if it doesn't exist.
435435
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {

src/libcore/trie.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ impl<T> Map<uint, T> for TrieMap<T> {
111111
}
112112
}
113113

114+
/// Return a mutable reference to the value corresponding to the key
115+
#[inline(always)]
116+
fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> {
117+
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
118+
}
119+
114120
/// Insert a key-value pair into the map. An existing value for a
115121
/// key is replaced by the new value. Return true if the key did
116122
/// not already exist in the map.
@@ -153,12 +159,6 @@ pub impl<T> TrieMap<T> {
153159
fn each_value_reverse(&self, f: &fn(&T) -> bool) {
154160
self.each_reverse(|&(_, v)| f(v))
155161
}
156-
157-
/// Return a mutable reference to the value corresponding to the key
158-
#[inline(always)]
159-
fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> {
160-
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
161-
}
162162
}
163163

164164
pub struct TrieSet {

src/libstd/smallintmap.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
108108
}
109109
}
110110

111+
/// Return a mutable reference to the value corresponding to the key
112+
fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> {
113+
if *key < self.v.len() {
114+
match self.v[*key] {
115+
Some(ref mut value) => Some(value),
116+
None => None
117+
}
118+
} else {
119+
None
120+
}
121+
}
122+
111123
/// Insert a key-value pair into the map. An existing value for a
112124
/// key is replaced by the new value. Return true if the key did
113125
/// not already exist in the map.
@@ -140,18 +152,6 @@ pub impl<V> SmallIntMap<V> {
140152
fn get(&self, key: &uint) -> &'self V {
141153
self.find(key).expect("key not present")
142154
}
143-
144-
/// Return a mutable reference to the value corresponding to the key
145-
fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> {
146-
if *key < self.v.len() {
147-
match self.v[*key] {
148-
Some(ref mut value) => Some(value),
149-
None => None
150-
}
151-
} else {
152-
None
153-
}
154-
}
155155
}
156156

157157
pub impl<V:Copy> SmallIntMap<V> {

src/libstd/treemap.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
152152
}
153153
}
154154

155+
/// Return a mutable reference to the value corresponding to the key
156+
#[inline(always)]
157+
fn find_mut(&mut self, key: &K) -> Option<&'self mut V> {
158+
find_mut(&mut self.root, key)
159+
}
160+
155161
/// Insert a key-value pair into the map. An existing value for a
156162
/// key is replaced by the new value. Return true if the key did
157163
/// not already exist in the map.
@@ -189,12 +195,6 @@ pub impl<K: TotalOrd, V> TreeMap<K, V> {
189195
fn iter(&self) -> TreeMapIterator<'self, K, V> {
190196
TreeMapIterator{stack: ~[], node: &self.root}
191197
}
192-
193-
/// Return a mutable reference to the value corresponding to the key
194-
#[inline(always)]
195-
fn find_mut(&mut self, key: &K) -> Option<&'self mut V> {
196-
find_mut(&mut self.root, key)
197-
}
198198
}
199199

200200
/// Lazy forward iterator over a map

src/test/run-pass/class-impl-very-parameterized-trait.rs

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ impl<T> Map<int, T> for cat<T> {
9898
}
9999
}
100100
101+
fn find_mut(&mut self, k: &int) -> Option<&'self mut T> { fail!() }
102+
101103
fn remove(&mut self, k: &int) -> bool {
102104
if self.find(k).is_some() {
103105
self.meows -= *k; true

0 commit comments

Comments
 (0)