Add an insert_with_key function to the Map trait
This commit is contained in:
committed by
Brian Anderson
parent
455d73cb86
commit
a343e435d5
@@ -27,7 +27,15 @@ pub trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
|
|||||||
*
|
*
|
||||||
* Returns true if the key did not already exist in the map
|
* Returns true if the key did not already exist in the map
|
||||||
*/
|
*/
|
||||||
fn insert(v: K, v: V) -> bool;
|
fn insert(key: K, value: V) -> bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a value to the map.
|
||||||
|
*
|
||||||
|
* If the map contains a value for the key, use the function
|
||||||
|
* to set a new value.
|
||||||
|
*/
|
||||||
|
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, value: V) -> bool;
|
||||||
|
|
||||||
/// Returns true if the map contains a value for the specified key
|
/// Returns true if the map contains a value for the specified key
|
||||||
pure fn contains_key(key: K) -> bool;
|
pure fn contains_key(key: K) -> bool;
|
||||||
@@ -264,6 +272,14 @@ pub mod chained {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, val: V) -> bool {
|
||||||
|
// this can be optimized but first lets see if it compiles...
|
||||||
|
match self.find(key) {
|
||||||
|
None => return self.insert(key, val),
|
||||||
|
Some(copy orig) => return self.insert(key, ff(key, orig, val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pure fn get(k: K) -> V {
|
pure fn get(k: K) -> V {
|
||||||
let opt_v = self.find(k);
|
let opt_v = self.find(k);
|
||||||
if opt_v.is_none() {
|
if opt_v.is_none() {
|
||||||
@@ -447,6 +463,13 @@ impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, val: V) -> bool {
|
||||||
|
match self.find(key) {
|
||||||
|
None => return self.insert(key, val),
|
||||||
|
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn remove(key: K) -> bool {
|
fn remove(key: K) -> bool {
|
||||||
do self.borrow_mut |p| {
|
do self.borrow_mut |p| {
|
||||||
p.remove(&key)
|
p.remove(&key)
|
||||||
|
|||||||
@@ -103,6 +103,13 @@ impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
|
|||||||
pure fn find(key: uint) -> Option<V> { find(self, key) }
|
pure fn find(key: uint) -> Option<V> { find(self, key) }
|
||||||
fn rehash() { fail }
|
fn rehash() { fail }
|
||||||
|
|
||||||
|
fn insert_with_key(ff: fn(uint, V, V) -> V, key: uint, val: V) -> bool {
|
||||||
|
match self.find(key) {
|
||||||
|
None => return self.insert(key, val),
|
||||||
|
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
||||||
self.each_ref(|k, v| it(*k, *v))
|
self.each_ref(|k, v| it(*k, *v))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,14 @@ impl<T: Copy> cat<T> : Map<int, T> {
|
|||||||
else { None }
|
else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn insert_with_key(ff: fn(+k: int, +v0: T, +v1: T) -> T, +key: int, +val: T) -> bool {
|
||||||
|
match self.find(key) {
|
||||||
|
None => return self.insert(key, val),
|
||||||
|
Some(copy orig) => return self.insert(key, ff(key, orig, val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn remove(+k:int) -> bool {
|
fn remove(+k:int) -> bool {
|
||||||
match self.find(k) {
|
match self.find(k) {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user