Skip to content

Commit

Permalink
Add relative_complement to sets/maps
Browse files Browse the repository at this point in the history
  • Loading branch information
AljoschaMeyer committed May 10, 2019
1 parent 01f2726 commit 37f8046
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/hash/map.rs
Expand Up @@ -1131,6 +1131,32 @@ where
out.union(self)
}

/// Construct the relative complement between two maps by discarding keys
/// which occur in `other`.
///
/// Time: O(m log n) where m is the size of the other map
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::ordmap::OrdMap;
/// # fn main() {
/// let map1 = ordmap!{1 => 1, 3 => 4};
/// let map2 = ordmap!{2 => 2, 3 => 5};
/// let expected = ordmap!{1 => 1, 2 => 2};
/// assert_eq!(expected, map1.relative_complement(map2));
/// # }
/// ```
#[inline]
#[must_use]
pub fn relative_complement(mut self, other: Self) -> Self {
for (key, _) in other {
let _ = self.remove(&key);
}
self
}

/// Construct the intersection of two maps, keeping the values
/// from the current map.
///
Expand Down
25 changes: 25 additions & 0 deletions src/hash/set.rs
Expand Up @@ -537,6 +537,31 @@ where
self
}

/// Construct the relative complement between two sets, that is the set
/// of values in `self` that do not occur in `other`.
///
/// Time: O(m log n) where m is the size of the other set
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::ordset::OrdSet;
/// # fn main() {
/// let set1 = ordset!{1, 2};
/// let set2 = ordset!{2, 3};
/// let expected = ordset!{1};
/// assert_eq!(expected, set1.relative_complement(set2));
/// # }
/// ```
#[must_use]
pub fn relative_complement(mut self, other: Self) -> Self {
for value in other {
let _ = self.remove(&value);
}
self
}

/// Construct the intersection of two sets.
///
/// Time: O(n log n)
Expand Down
26 changes: 26 additions & 0 deletions src/ord/map.rs
Expand Up @@ -1095,6 +1095,32 @@ where
out.union(self)
}

/// Construct the relative complement between two maps by discarding keys
/// which occur in `other`.
///
/// Time: O(m log n) where m is the size of the other map
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::ordmap::OrdMap;
/// # fn main() {
/// let map1 = ordmap!{1 => 1, 3 => 4};
/// let map2 = ordmap!{2 => 2, 3 => 5};
/// let expected = ordmap!{1 => 1, 2 => 2};
/// assert_eq!(expected, map1.relative_complement(map2));
/// # }
/// ```
#[inline]
#[must_use]
pub fn relative_complement(mut self, other: Self) -> Self {
for (key, _) in other {
let _ = self.remove(&key);
}
self
}

/// Construct the intersection of two maps, keeping the values
/// from the current map.
///
Expand Down
25 changes: 25 additions & 0 deletions src/ord/set.rs
Expand Up @@ -637,6 +637,31 @@ where
self
}

/// Construct the relative complement between two sets, that is the set
/// of values in `self` that do not occur in `other`.
///
/// Time: O(m log n) where m is the size of the other set
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::ordset::OrdSet;
/// # fn main() {
/// let set1 = ordset!{1, 2};
/// let set2 = ordset!{2, 3};
/// let expected = ordset!{1};
/// assert_eq!(expected, set1.relative_complement(set2));
/// # }
/// ```
#[must_use]
pub fn relative_complement(mut self, other: Self) -> Self {
for value in other {
let _ = self.remove(&value);
}
self
}

/// Construct the intersection of two sets.
///
/// Time: O(n log n)
Expand Down

0 comments on commit 37f8046

Please sign in to comment.