Skip to content

Commit

Permalink
Document update and update_with_key in SmallIntMap.
Browse files Browse the repository at this point in the history
Move update above for better docs progression.
  • Loading branch information
treeman committed Jul 24, 2014
1 parent 9e2bb9d commit 222b780
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/libcollections/smallintmap.rs
Expand Up @@ -167,6 +167,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,
Expand All @@ -178,10 +222,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> {
Expand Down

5 comments on commit 222b780

@bors
Copy link
Contributor

@bors bors commented on 222b780 Jul 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 222b780 Jul 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging treeman/rust/doc-smallint-update = 222b780 into auto

@bors
Copy link
Contributor

@bors bors commented on 222b780 Jul 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

treeman/rust/doc-smallint-update = 222b780 merged ok, testing candidate = 7f2e63e

@bors
Copy link
Contributor

@bors bors commented on 222b780 Jul 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7f2e63e

Please sign in to comment.