@@ -279,6 +279,50 @@ impl<V> SmallIntMap<V> {
279279}
280280
281281impl < V : Clone > SmallIntMap < V > {
282+ /// Update a value in the map. If the key already exists in the map,
283+ /// modify the value with `ff` taking `oldval, newval`.
284+ /// Otherwise set the value to `newval`.
285+ /// Return `true` if the key did not already exist in the map.
286+ ///
287+ /// # Example
288+ ///
289+ /// ```
290+ /// use std::collections::SmallIntMap;
291+ ///
292+ /// let mut map = SmallIntMap::new();
293+ ///
294+ /// // Key does not exist, will do a simple insert
295+ /// assert!(map.update(1, vec![1i, 2], |old, new| old.append(new.as_slice())));
296+ /// assert_eq!(map.get(&1), &vec![1i, 2]);
297+ ///
298+ /// // Key exists, update the value
299+ /// assert!(!map.update(1, vec![3i, 4], |old, new| old.append(new.as_slice())));
300+ /// assert_eq!(map.get(&1), &vec![1i, 2, 3, 4]);
301+ /// ```
302+ pub fn update ( & mut self , key : uint , newval : V , ff : |V , V | -> V ) -> bool {
303+ self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
304+ }
305+
306+ /// Update a value in the map. If the key already exists in the map,
307+ /// modify the value with `ff` taking `key, oldval, newval`.
308+ /// Otherwise set the value to `newval`.
309+ /// Return `true` if the key did not already exist in the map.
310+ ///
311+ /// # Example
312+ ///
313+ /// ```
314+ /// use std::collections::SmallIntMap;
315+ ///
316+ /// let mut map = SmallIntMap::new();
317+ ///
318+ /// // Key does not exist, will do a simple insert
319+ /// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
320+ /// assert_eq!(map.get(&7), &10);
321+ ///
322+ /// // Key exists, update the value
323+ /// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
324+ /// assert_eq!(map.get(&7), &2);
325+ /// ```
282326 pub fn update_with_key ( & mut self ,
283327 key : uint ,
284328 val : V ,
@@ -290,10 +334,6 @@ impl<V:Clone> SmallIntMap<V> {
290334 } ;
291335 self . insert ( key, new_val)
292336 }
293-
294- pub fn update ( & mut self , key : uint , newval : V , ff : |V , V | -> V ) -> bool {
295- self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
296- }
297337}
298338
299339impl < V : fmt:: Show > fmt:: Show for SmallIntMap < V > {
0 commit comments