Skip to content

Commit 6b08683

Browse files
committed
add intersection and union to the Set trait
1 parent 99eb4dd commit 6b08683

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

src/libcore/container.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ pub trait Set<T>: Mutable {
8181

8282
/// Visit the values representing the symmetric difference
8383
pure fn symmetric_difference(&self, other: &self, f: fn(&T) -> bool);
84+
85+
/// Visit the values representing the intersection
86+
pure fn intersection(&self, other: &self, f: fn(&T) -> bool);
87+
88+
/// Visit the values representing the union
89+
pure fn union(&self, other: &self, f: fn(&T) -> bool);
8490
}

src/libcore/hashmap.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ pub mod linear {
474474
pure fn difference(&self, other: &LinearSet<T>, f: fn(&T) -> bool) {
475475
for self.each |v| {
476476
if !other.contains(v) {
477-
if !f(v) { return; }
477+
if !f(v) { return }
478478
}
479479
}
480480
}
@@ -485,6 +485,28 @@ pub mod linear {
485485
self.difference(other, f);
486486
other.difference(self, f);
487487
}
488+
489+
/// Visit the values representing the intersection
490+
pure fn intersection(&self, other: &LinearSet<T>, f: fn(&T) -> bool) {
491+
for self.each |v| {
492+
if other.contains(v) {
493+
if !f(v) { return }
494+
}
495+
}
496+
}
497+
498+
/// Visit the values representing the union
499+
pure fn union(&self, other: &LinearSet<T>, f: fn(&T) -> bool) {
500+
for self.each |v| {
501+
if !f(v) { return }
502+
}
503+
504+
for other.each |v| {
505+
if !self.contains(v) {
506+
if !f(v) { return }
507+
}
508+
}
509+
}
488510
}
489511

490512
pub impl <T: Hash IterBytes Eq> LinearSet<T> {
@@ -698,6 +720,36 @@ mod test_set {
698720
assert b.is_superset(&a);
699721
}
700722

723+
#[test]
724+
fn test_intersection() {
725+
let mut a = linear::LinearSet::new();
726+
let mut b = linear::LinearSet::new();
727+
728+
assert a.insert(11);
729+
assert a.insert(1);
730+
assert a.insert(3);
731+
assert a.insert(77);
732+
assert a.insert(103);
733+
assert a.insert(5);
734+
assert a.insert(-5);
735+
736+
assert b.insert(2);
737+
assert b.insert(11);
738+
assert b.insert(77);
739+
assert b.insert(-9);
740+
assert b.insert(-42);
741+
assert b.insert(5);
742+
assert b.insert(3);
743+
744+
let mut i = 0;
745+
let expected = [3, 5, 11, 77];
746+
for a.intersection(&b) |x| {
747+
assert vec::contains(expected, x);
748+
i += 1
749+
}
750+
assert i == expected.len();
751+
}
752+
701753
#[test]
702754
fn test_difference() {
703755
let mut a = linear::LinearSet::new();
@@ -746,4 +798,34 @@ mod test_set {
746798
}
747799
assert i == expected.len();
748800
}
801+
802+
#[test]
803+
fn test_union() {
804+
let mut a = linear::LinearSet::new();
805+
let mut b = linear::LinearSet::new();
806+
807+
assert a.insert(1);
808+
assert a.insert(3);
809+
assert a.insert(5);
810+
assert a.insert(9);
811+
assert a.insert(11);
812+
assert a.insert(16);
813+
assert a.insert(19);
814+
assert a.insert(24);
815+
816+
assert b.insert(-2);
817+
assert b.insert(1);
818+
assert b.insert(5);
819+
assert b.insert(9);
820+
assert b.insert(13);
821+
assert b.insert(19);
822+
823+
let mut i = 0;
824+
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
825+
for a.union(&b) |x| {
826+
assert vec::contains(expected, x);
827+
i += 1
828+
}
829+
assert i == expected.len();
830+
}
749831
}

src/libstd/treemap.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,22 +432,6 @@ impl <T: Ord> TreeSet<T>: Set<T> {
432432
}
433433
}
434434
}
435-
}
436-
437-
impl <T: Ord> TreeSet<T> {
438-
/// Create an empty TreeSet
439-
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
440-
441-
/// Visit all values in reverse order
442-
pure fn each_reverse(&self, f: fn(&T) -> bool) {
443-
self.map.each_key_reverse(f)
444-
}
445-
446-
/// Get a lazy iterator over the values in the set.
447-
/// Requires that it be frozen (immutable).
448-
pure fn iter(&self) -> TreeSetIterator/&self<T> {
449-
TreeSetIterator{iter: self.map.iter()}
450-
}
451435

452436
/// Visit the values (in-order) representing the intersection
453437
pure fn intersection(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
@@ -516,6 +500,22 @@ impl <T: Ord> TreeSet<T> {
516500
}
517501
}
518502

503+
impl <T: Ord> TreeSet<T> {
504+
/// Create an empty TreeSet
505+
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
506+
507+
/// Visit all values in reverse order
508+
pure fn each_reverse(&self, f: fn(&T) -> bool) {
509+
self.map.each_key_reverse(f)
510+
}
511+
512+
/// Get a lazy iterator over the values in the set.
513+
/// Requires that it be frozen (immutable).
514+
pure fn iter(&self) -> TreeSetIterator/&self<T> {
515+
TreeSetIterator{iter: self.map.iter()}
516+
}
517+
}
518+
519519
/// Lazy forward iterator over a set
520520
pub struct TreeSetIterator<T> {
521521
priv iter: TreeMapIterator<T, ()>

0 commit comments

Comments
 (0)