Skip to content

Commit a343e43

Browse files
killerswanbrson
authored andcommitted
Add an insert_with_key function to the Map trait
1 parent 455d73c commit a343e43

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/libstd/map.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ pub trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
2727
*
2828
* Returns true if the key did not already exist in the map
2929
*/
30-
fn insert(v: K, v: V) -> bool;
30+
fn insert(key: K, value: V) -> bool;
31+
32+
/**
33+
* Add a value to the map.
34+
*
35+
* If the map contains a value for the key, use the function
36+
* to set a new value.
37+
*/
38+
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, value: V) -> bool;
3139

3240
/// Returns true if the map contains a value for the specified key
3341
pure fn contains_key(key: K) -> bool;
@@ -264,6 +272,14 @@ pub mod chained {
264272
}
265273
}
266274

275+
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, val: V) -> bool {
276+
// this can be optimized but first lets see if it compiles...
277+
match self.find(key) {
278+
None => return self.insert(key, val),
279+
Some(copy orig) => return self.insert(key, ff(key, orig, val))
280+
}
281+
}
282+
267283
pure fn get(k: K) -> V {
268284
let opt_v = self.find(k);
269285
if opt_v.is_none() {
@@ -447,6 +463,13 @@ impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
447463
}
448464
}
449465

466+
fn insert_with_key(ff: fn(K, V, V) -> V, key: K, val: V) -> bool {
467+
match self.find(key) {
468+
None => return self.insert(key, val),
469+
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
470+
}
471+
}
472+
450473
fn remove(key: K) -> bool {
451474
do self.borrow_mut |p| {
452475
p.remove(&key)

src/libstd/smallintmap.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
103103
pure fn find(key: uint) -> Option<V> { find(self, key) }
104104
fn rehash() { fail }
105105

106+
fn insert_with_key(ff: fn(uint, V, V) -> V, key: uint, val: V) -> bool {
107+
match self.find(key) {
108+
None => return self.insert(key, val),
109+
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
110+
}
111+
}
112+
106113
pure fn each(it: fn(key: uint, value: V) -> bool) {
107114
self.each_ref(|k, v| it(*k, *v))
108115
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ impl<T: Copy> cat<T> : Map<int, T> {
6161
else { None }
6262
}
6363

64+
fn insert_with_key(ff: fn(+k: int, +v0: T, +v1: T) -> T, +key: int, +val: T) -> bool {
65+
match self.find(key) {
66+
None => return self.insert(key, val),
67+
Some(copy orig) => return self.insert(key, ff(key, orig, val))
68+
}
69+
}
70+
71+
6472
fn remove(+k:int) -> bool {
6573
match self.find(k) {
6674
Some(x) => {

0 commit comments

Comments
 (0)