Skip to content

Commit 6717250

Browse files
killerswanbrson
authored andcommitted
---
yaml --- r: 37709 b: refs/heads/try c: a343e43 h: refs/heads/master i: 37707: 330f6b7 v: v3
1 parent 6dd2e5d commit 6717250

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 09bb07bed9166105ea961a42b5fff7739ae0d2e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
5-
refs/heads/try: 455d73cb861bb0aca989fc8aca19c025ebf6f4ed
5+
refs/heads/try: a343e435d59e21188f6fc5918324c751a7eff6a9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278

branches/try/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)

branches/try/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
}

branches/try/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)