Skip to content

Commit

Permalink
Add clear methods to maps and sets.
Browse files Browse the repository at this point in the history
Closes #46.
  • Loading branch information
bodil committed Sep 16, 2018
1 parent 29583fa commit 104dce9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,31 @@ where
result
}

/// Discard all elements from the map.
///
/// This leaves you with an empty map, and all elements that
/// were previously inside it are dropped.
///
/// Time: O(n)
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::HashMap;
/// # fn main() {
/// let mut map = hashmap![1=>1, 2=>2, 3=>3];
/// map.clear();
/// assert!(map.is_empty());
/// # }
/// ```
pub fn clear(&mut self) {
if !self.is_empty() {
self.root = Default::default();
self.size = 0;
}
}

/// Get the [`Entry`][Entry] for a key in the map for in-place manipulation.
///
/// Time: O(log n)
Expand Down
25 changes: 25 additions & 0 deletions src/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@ where
result.map(|v| v.0)
}

/// Discard all elements from the set.
///
/// This leaves you with an empty set, and all elements that
/// were previously inside it are dropped.
///
/// Time: O(n)
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::HashSet;
/// # fn main() {
/// let mut set = hashset![1, 2, 3];
/// set.clear();
/// assert!(set.is_empty());
/// # }
/// ```
pub fn clear(&mut self) {
if !self.is_empty() {
self.root = Default::default();
self.size = 0;
}
}

/// Construct a new set from the current set with the given value
/// added.
///
Expand Down
25 changes: 25 additions & 0 deletions src/ord/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,31 @@ where
removed_value
}

/// Discard all elements from the map.
///
/// This leaves you with an empty map, and all elements that
/// were previously inside it are dropped.
///
/// Time: O(n)
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::OrdMap;
/// # fn main() {
/// let mut map = ordmap![1=>1, 2=>2, 3=>3];
/// map.clear();
/// assert!(map.is_empty());
/// # }
/// ```
pub fn clear(&mut self) {
if !self.is_empty() {
self.root = Default::default();
self.size = 0;
}
}

/// Construct a new map by inserting a key/value mapping into a
/// map.
///
Expand Down
25 changes: 25 additions & 0 deletions src/ord/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,31 @@ where
self.remove(&key)
}

/// Discard all elements from the set.
///
/// This leaves you with an empty set, and all elements that
/// were previously inside it are dropped.
///
/// Time: O(n)
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::OrdSet;
/// # fn main() {
/// let mut set = ordset![1, 2, 3];
/// set.clear();
/// assert!(set.is_empty());
/// # }
/// ```
pub fn clear(&mut self) {
if !self.is_empty() {
self.root = Default::default();
self.size = 0;
}
}

/// Construct a new set from the current set with the given value
/// added.
///
Expand Down

0 comments on commit 104dce9

Please sign in to comment.