@@ -167,6 +167,50 @@ impl<V> SmallIntMap<V> {
167167}
168168
169169impl < V : Clone > SmallIntMap < V > {
170+ /// Update a value in the map. If the key already exists in the map,
171+ /// modify the value with `ff` taking `oldval, newval`.
172+ /// Otherwise set the value to `newval`.
173+ /// Return `true` if the key did not already exist in the map.
174+ ///
175+ /// # Example
176+ ///
177+ /// ```
178+ /// use std::collections::SmallIntMap;
179+ ///
180+ /// let mut map = SmallIntMap::new();
181+ ///
182+ /// // Key does not exist, will do a simple insert
183+ /// assert!(map.update(1, vec![1i, 2], |old, new| old.append(new.as_slice())));
184+ /// assert_eq!(map.get(&1), &vec![1i, 2]);
185+ ///
186+ /// // Key exists, update the value
187+ /// assert!(!map.update(1, vec![3i, 4], |old, new| old.append(new.as_slice())));
188+ /// assert_eq!(map.get(&1), &vec![1i, 2, 3, 4]);
189+ /// ```
190+ pub fn update ( & mut self , key : uint , newval : V , ff : |V , V | -> V ) -> bool {
191+ self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
192+ }
193+
194+ /// Update a value in the map. If the key already exists in the map,
195+ /// modify the value with `ff` taking `key, oldval, newval`.
196+ /// Otherwise set the value to `newval`.
197+ /// Return `true` if the key did not already exist in the map.
198+ ///
199+ /// # Example
200+ ///
201+ /// ```
202+ /// use std::collections::SmallIntMap;
203+ ///
204+ /// let mut map = SmallIntMap::new();
205+ ///
206+ /// // Key does not exist, will do a simple insert
207+ /// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
208+ /// assert_eq!(map.get(&7), &10);
209+ ///
210+ /// // Key exists, update the value
211+ /// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
212+ /// assert_eq!(map.get(&7), &2);
213+ /// ```
170214 pub fn update_with_key ( & mut self ,
171215 key : uint ,
172216 val : V ,
@@ -178,10 +222,6 @@ impl<V:Clone> SmallIntMap<V> {
178222 } ;
179223 self . insert ( key, new_val)
180224 }
181-
182- pub fn update ( & mut self , key : uint , newval : V , ff : |V , V | -> V ) -> bool {
183- self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
184- }
185225}
186226
187227impl < V : fmt:: Show > fmt:: Show for SmallIntMap < V > {
0 commit comments