diff options
| author | bors <bors@rust-lang.org> | 2014-07-24 22:51:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-07-24 22:51:10 +0000 |
| commit | 7f2e63ec3f6f03ac9273a9f166a4ce8deff48097 (patch) | |
| tree | 2bf6ee5186ca92dce413db41d2c1e38179159674 | |
| parent | d3f66bd5b4c7066b1405c46a2fcdd9646761bc1e (diff) | |
| parent | 222b780e7ac3fa1937948cb159c2e88422adb715 (diff) | |
| download | rust-7f2e63ec3f6f03ac9273a9f166a4ce8deff48097.tar.gz rust-7f2e63ec3f6f03ac9273a9f166a4ce8deff48097.zip | |
auto merge of #15945 : treeman/rust/doc-smallint-update, r=alexcrichton
Forgot two methods, but @alexcrichton was a bit too quick to accept #15943, so I made a new PR.
| -rw-r--r-- | src/libcollections/smallintmap.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index 3bc2dbe5cbb..994a6d6c5f3 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -279,6 +279,50 @@ impl<V> SmallIntMap<V> { } impl<V:Clone> SmallIntMap<V> { + /// Update a value in the map. If the key already exists in the map, + /// modify the value with `ff` taking `oldval, newval`. + /// Otherwise set the value to `newval`. + /// Return `true` if the key did not already exist in the map. + /// + /// # Example + /// + /// ``` + /// use std::collections::SmallIntMap; + /// + /// let mut map = SmallIntMap::new(); + /// + /// // Key does not exist, will do a simple insert + /// assert!(map.update(1, vec![1i, 2], |old, new| old.append(new.as_slice()))); + /// assert_eq!(map.get(&1), &vec![1i, 2]); + /// + /// // Key exists, update the value + /// assert!(!map.update(1, vec![3i, 4], |old, new| old.append(new.as_slice()))); + /// assert_eq!(map.get(&1), &vec![1i, 2, 3, 4]); + /// ``` + pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool { + self.update_with_key(key, newval, |_k, v, v1| ff(v,v1)) + } + + /// Update a value in the map. If the key already exists in the map, + /// modify the value with `ff` taking `key, oldval, newval`. + /// Otherwise set the value to `newval`. + /// Return `true` if the key did not already exist in the map. + /// + /// # Example + /// + /// ``` + /// use std::collections::SmallIntMap; + /// + /// let mut map = SmallIntMap::new(); + /// + /// // Key does not exist, will do a simple insert + /// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key)); + /// assert_eq!(map.get(&7), &10); + /// + /// // Key exists, update the value + /// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key)); + /// assert_eq!(map.get(&7), &2); + /// ``` pub fn update_with_key(&mut self, key: uint, val: V, @@ -290,10 +334,6 @@ impl<V:Clone> SmallIntMap<V> { }; self.insert(key, new_val) } - - pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool { - self.update_with_key(key, newval, |_k, v, v1| ff(v,v1)) - } } impl<V: fmt::Show> fmt::Show for SmallIntMap<V> { |
