Rename insert_with functions to update, update_with_key
This commit is contained in:
@@ -35,16 +35,16 @@ pub trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
|
|||||||
* If the map contains a value for the key, use the function
|
* If the map contains a value for the key, use the function
|
||||||
* to set a new value.
|
* to set a new value.
|
||||||
*/
|
*/
|
||||||
fn insert_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool;
|
fn update_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a value to the map.
|
* Add a value to the map.
|
||||||
*
|
*
|
||||||
* If the map contains a value for the key, use the function
|
* If the map contains a value for the key, use the function to
|
||||||
* to set a new value. (Like insert_with_key, but with a function
|
* set a new value. (Like `insert_or_update_with_key`, but with a
|
||||||
* of only values.)
|
* function of only values.)
|
||||||
*/
|
*/
|
||||||
fn insert_with(key: K, newval: V, ff: fn(V, V) -> V) -> bool;
|
fn update(key: K, newval: V, ff: fn(V, V) -> 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;
|
||||||
@@ -281,7 +281,7 @@ pub mod chained {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool {
|
fn update_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool {
|
||||||
/*
|
/*
|
||||||
match self.find(key) {
|
match self.find(key) {
|
||||||
None => return self.insert(key, val),
|
None => return self.insert(key, val),
|
||||||
@@ -330,8 +330,8 @@ pub mod chained {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_with(key: K, newval: V, ff: fn(V, V) -> V) -> bool {
|
fn update(key: K, newval: V, ff: fn(V, V) -> V) -> bool {
|
||||||
return self.insert_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
return self.update_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn get(k: K) -> V {
|
pure fn get(k: K) -> V {
|
||||||
@@ -517,15 +517,15 @@ impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool {
|
fn update_with_key(key: K, newval: V, ff: fn(K, V, V) -> V) -> bool {
|
||||||
match self.find(key) {
|
match self.find(key) {
|
||||||
None => return self.insert(key, newval),
|
None => return self.insert(key, newval),
|
||||||
Some(copy orig) => return self.insert(key, ff(key, orig, newval))
|
Some(copy orig) => return self.insert(key, ff(key, orig, newval))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_with(key: K, newval: V, ff: fn(V, V) -> V) -> bool {
|
fn update(key: K, newval: V, ff: fn(V, V) -> V) -> bool {
|
||||||
return self.insert_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
return self.update_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(key: K) -> bool {
|
fn remove(key: K) -> bool {
|
||||||
@@ -833,7 +833,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_insert_with_key() {
|
fn test_update_with_key() {
|
||||||
let map = map::HashMap::<~str, uint>();
|
let map = map::HashMap::<~str, uint>();
|
||||||
|
|
||||||
// given a new key, initialize it with this new count, given
|
// given a new key, initialize it with this new count, given
|
||||||
@@ -848,11 +848,11 @@ mod tests {
|
|||||||
|
|
||||||
// count the number of several types of animal,
|
// count the number of several types of animal,
|
||||||
// adding in groups as we go
|
// adding in groups as we go
|
||||||
map.insert_with(~"cat", 1, addMoreToCount_simple);
|
map.update(~"cat", 1, addMoreToCount_simple);
|
||||||
map.insert_with_key(~"mongoose", 1, addMoreToCount);
|
map.update_with_key(~"mongoose", 1, addMoreToCount);
|
||||||
map.insert_with(~"cat", 7, addMoreToCount_simple);
|
map.update(~"cat", 7, addMoreToCount_simple);
|
||||||
map.insert_with_key(~"ferret", 3, addMoreToCount);
|
map.update_with_key(~"ferret", 3, addMoreToCount);
|
||||||
map.insert_with_key(~"cat", 2, addMoreToCount);
|
map.update_with_key(~"cat", 2, addMoreToCount);
|
||||||
|
|
||||||
// check the total counts
|
// check the total counts
|
||||||
assert 10 == option::get(map.find(~"cat"));
|
assert 10 == option::get(map.find(~"cat"));
|
||||||
|
|||||||
@@ -103,15 +103,15 @@ 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(key: uint, val: V, ff: fn(uint, V, V) -> V) -> bool {
|
fn update_with_key(key: uint, val: V, ff: fn(uint, V, V) -> V) -> bool {
|
||||||
match self.find(key) {
|
match self.find(key) {
|
||||||
None => return self.insert(key, val),
|
None => return self.insert(key, val),
|
||||||
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
|
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_with(key: uint, newval: V, ff: fn(V, V) -> V) -> bool {
|
fn update(key: uint, newval: V, ff: fn(V, V) -> V) -> bool {
|
||||||
return self.insert_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
return self.update_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
||||||
@@ -172,11 +172,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// count integers
|
// count integers
|
||||||
map.insert_with(3, 1, addMoreToCount_simple);
|
map.update(3, 1, addMoreToCount_simple);
|
||||||
map.insert_with_key(9, 1, addMoreToCount);
|
map.update_with_key(9, 1, addMoreToCount);
|
||||||
map.insert_with(3, 7, addMoreToCount_simple);
|
map.update(3, 7, addMoreToCount_simple);
|
||||||
map.insert_with_key(5, 3, addMoreToCount);
|
map.update_with_key(5, 3, addMoreToCount);
|
||||||
map.insert_with_key(3, 2, addMoreToCount);
|
map.update_with_key(3, 2, addMoreToCount);
|
||||||
|
|
||||||
// check the total counts
|
// check the total counts
|
||||||
assert 10 == option::get(map.find(3));
|
assert 10 == option::get(map.find(3));
|
||||||
|
|||||||
Reference in New Issue
Block a user