Skip to content

Commit fe1dd88

Browse files
committed
---
yaml --- r: 60921 b: refs/heads/auto c: 75f1b7f h: refs/heads/master i: 60919: 3e14d76 v: v3
1 parent ea0ab5a commit fe1dd88

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 7bc950c43c820b0a0cfeede7dcf2d719625dbd90
17+
refs/heads/auto: 75f1b7f96fa4e91244a96ba92f615f3213d97519
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libstd/hashmap.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,14 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
467467
self.mangle(k, (), |k,_a| f(k), |_k,_v,_a| ())
468468
}
469469

470+
/// Insert a key-value pair into the map if the key is not already present.
471+
/// Otherwise, modify the existing value for the key.
472+
/// Returns the new or modified value for the key.
473+
pub fn insert_or_update_with<'a>(&'a mut self, k: K, v: V,
474+
f: &fn(&K, &mut V)) -> &'a mut V {
475+
self.mangle(k, v, |_k,a| a, |k,v,_a| f(k,v))
476+
}
477+
470478
/// Calls a function on each element of a hash map, destroying the hash
471479
/// map in the process.
472480
pub fn consume(&mut self, f: &fn(K, V)) {
@@ -758,6 +766,13 @@ mod test_map {
758766
assert_eq!(*m.find_or_insert_with(1, |_| 3), 2);
759767
}
760768

769+
#[test]
770+
fn test_insert_or_update_with() {
771+
let mut m = HashMap::new::<int, int>();
772+
assert_eq!(*m.insert_or_update_with(1, 2, |_,x| *x+=1), 2);
773+
assert_eq!(*m.insert_or_update_with(1, 2, |_,x| *x+=1), 3);
774+
}
775+
761776
#[test]
762777
fn test_consume() {
763778
let mut m = HashMap::new();

0 commit comments

Comments
 (0)