Skip to content

Commit 37f8046

Browse files
committed
Add relative_complement to sets/maps
1 parent 01f2726 commit 37f8046

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/hash/map.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,32 @@ where
11311131
out.union(self)
11321132
}
11331133

1134+
/// Construct the relative complement between two maps by discarding keys
1135+
/// which occur in `other`.
1136+
///
1137+
/// Time: O(m log n) where m is the size of the other map
1138+
///
1139+
/// # Examples
1140+
///
1141+
/// ```
1142+
/// # #[macro_use] extern crate im;
1143+
/// # use im::ordmap::OrdMap;
1144+
/// # fn main() {
1145+
/// let map1 = ordmap!{1 => 1, 3 => 4};
1146+
/// let map2 = ordmap!{2 => 2, 3 => 5};
1147+
/// let expected = ordmap!{1 => 1, 2 => 2};
1148+
/// assert_eq!(expected, map1.relative_complement(map2));
1149+
/// # }
1150+
/// ```
1151+
#[inline]
1152+
#[must_use]
1153+
pub fn relative_complement(mut self, other: Self) -> Self {
1154+
for (key, _) in other {
1155+
let _ = self.remove(&key);
1156+
}
1157+
self
1158+
}
1159+
11341160
/// Construct the intersection of two maps, keeping the values
11351161
/// from the current map.
11361162
///

src/hash/set.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,31 @@ where
537537
self
538538
}
539539

540+
/// Construct the relative complement between two sets, that is the set
541+
/// of values in `self` that do not occur in `other`.
542+
///
543+
/// Time: O(m log n) where m is the size of the other set
544+
///
545+
/// # Examples
546+
///
547+
/// ```
548+
/// # #[macro_use] extern crate im;
549+
/// # use im::ordset::OrdSet;
550+
/// # fn main() {
551+
/// let set1 = ordset!{1, 2};
552+
/// let set2 = ordset!{2, 3};
553+
/// let expected = ordset!{1};
554+
/// assert_eq!(expected, set1.relative_complement(set2));
555+
/// # }
556+
/// ```
557+
#[must_use]
558+
pub fn relative_complement(mut self, other: Self) -> Self {
559+
for value in other {
560+
let _ = self.remove(&value);
561+
}
562+
self
563+
}
564+
540565
/// Construct the intersection of two sets.
541566
///
542567
/// Time: O(n log n)

src/ord/map.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,32 @@ where
10951095
out.union(self)
10961096
}
10971097

1098+
/// Construct the relative complement between two maps by discarding keys
1099+
/// which occur in `other`.
1100+
///
1101+
/// Time: O(m log n) where m is the size of the other map
1102+
///
1103+
/// # Examples
1104+
///
1105+
/// ```
1106+
/// # #[macro_use] extern crate im;
1107+
/// # use im::ordmap::OrdMap;
1108+
/// # fn main() {
1109+
/// let map1 = ordmap!{1 => 1, 3 => 4};
1110+
/// let map2 = ordmap!{2 => 2, 3 => 5};
1111+
/// let expected = ordmap!{1 => 1, 2 => 2};
1112+
/// assert_eq!(expected, map1.relative_complement(map2));
1113+
/// # }
1114+
/// ```
1115+
#[inline]
1116+
#[must_use]
1117+
pub fn relative_complement(mut self, other: Self) -> Self {
1118+
for (key, _) in other {
1119+
let _ = self.remove(&key);
1120+
}
1121+
self
1122+
}
1123+
10981124
/// Construct the intersection of two maps, keeping the values
10991125
/// from the current map.
11001126
///

src/ord/set.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,31 @@ where
637637
self
638638
}
639639

640+
/// Construct the relative complement between two sets, that is the set
641+
/// of values in `self` that do not occur in `other`.
642+
///
643+
/// Time: O(m log n) where m is the size of the other set
644+
///
645+
/// # Examples
646+
///
647+
/// ```
648+
/// # #[macro_use] extern crate im;
649+
/// # use im::ordset::OrdSet;
650+
/// # fn main() {
651+
/// let set1 = ordset!{1, 2};
652+
/// let set2 = ordset!{2, 3};
653+
/// let expected = ordset!{1};
654+
/// assert_eq!(expected, set1.relative_complement(set2));
655+
/// # }
656+
/// ```
657+
#[must_use]
658+
pub fn relative_complement(mut self, other: Self) -> Self {
659+
for value in other {
660+
let _ = self.remove(&value);
661+
}
662+
self
663+
}
664+
640665
/// Construct the intersection of two sets.
641666
///
642667
/// Time: O(n log n)

0 commit comments

Comments
 (0)