expose find_mut in the Map trait

This commit is contained in:
Daniel Micay 2013-03-24 20:40:17 -04:00
parent f0f4a00e88
commit 38f39ac540
6 changed files with 38 additions and 36 deletions

View File

@ -42,7 +42,7 @@ pub trait Map<K, V>: Mutable {
fn find(&self, key: &K) -> Option<&'self V>; fn find(&self, key: &K) -> Option<&'self V>;
/// Return a mutable reference to the value corresponding to the key /// 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 /// 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 /// key is replaced by the new value. Return true if the key did

View File

@ -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 /// 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 /// key is replaced by the new value. Return true if the key did
/// not already exist in the map. /// not already exist in the map.
@ -419,17 +430,6 @@ pub mod linear {
old_value 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 /// Return the value corresponding to the key in the map, or insert
/// and return the value if it doesn't exist. /// and return the value if it doesn't exist.
fn find_or_insert(&mut self, k: K, v: V) -> &'self V { fn find_or_insert(&mut self, k: K, v: V) -> &'self V {

View File

@ -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 /// 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 /// key is replaced by the new value. Return true if the key did
/// not already exist in the map. /// not already exist in the map.
@ -153,12 +159,6 @@ pub impl<T> TrieMap<T> {
fn each_value_reverse(&self, f: &fn(&T) -> bool) { fn each_value_reverse(&self, f: &fn(&T) -> bool) {
self.each_reverse(|&(_, v)| f(v)) 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 { pub struct TrieSet {

View File

@ -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 /// 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 /// key is replaced by the new value. Return true if the key did
/// not already exist in the map. /// not already exist in the map.
@ -140,18 +152,6 @@ pub impl<V> SmallIntMap<V> {
fn get(&self, key: &uint) -> &'self V { fn get(&self, key: &uint) -> &'self V {
self.find(key).expect("key not present") 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> { pub impl<V:Copy> SmallIntMap<V> {

View File

@ -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 /// 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 /// key is replaced by the new value. Return true if the key did
/// not already exist in the map. /// 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> { fn iter(&self) -> TreeMapIterator<'self, K, V> {
TreeMapIterator{stack: ~[], node: &self.root} 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 /// Lazy forward iterator over a map

View File

@ -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 { fn remove(&mut self, k: &int) -> bool {
if self.find(k).is_some() { if self.find(k).is_some() {
self.meows -= *k; true self.meows -= *k; true