Skip to content

Commit 72d6288

Browse files
committed
---
yaml --- r: 65522 b: refs/heads/master c: 75f1b7f h: refs/heads/master v: v3
1 parent 82276b6 commit 72d6288

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
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 7bc950c43c820b0a0cfeede7dcf2d719625dbd90
2+
refs/heads/master: 75f1b7f96fa4e91244a96ba92f615f3213d97519
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/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)