Skip to content

Commit 42cafce

Browse files
committed
add is_disjoint to the Set trait
1 parent bfa9c9a commit 42cafce

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

src/libcore/container.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ pub trait Set<T>: Mutable {
6666
/// present in the set.
6767
fn remove(&mut self, value: &T) -> bool;
6868

69+
/// Return true if the set has no elements in common with `other`.
70+
/// This is equivalent to checking for an empty intersection.
71+
pure fn is_disjoint(&self, other: &self) -> bool;
72+
6973
/// Return true if the set is a subset of another
7074
pure fn is_subset(&self, other: &self) -> bool;
7175

src/libcore/hashmap.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
#[forbid(deprecated_mode)];
1515
#[forbid(deprecated_pattern)];
1616

17+
use container::{Container, Mutable, Map, Set};
1718
use cmp::Eq;
1819
use hash::Hash;
1920
use to_bytes::IterBytes;
2021

2122
/// Open addressing with linear probing.
2223
pub mod linear {
24+
use super::*;
2325
use iter::BaseIter;
24-
use container::{Container, Mutable, Map, Set};
25-
use cmp::Eq;
26-
use cmp;
2726
use hash::Hash;
2827
use iter;
2928
use kinds::Copy;
@@ -455,6 +454,12 @@ pub mod linear {
455454
/// present in the set.
456455
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
457456

457+
/// Return true if the set has no elements in common with `other`.
458+
/// This is equivalent to checking for an empty intersection.
459+
pure fn is_disjoint(&self, other: &LinearSet<T>) -> bool {
460+
iter::all(self, |v| !other.contains(v))
461+
}
462+
458463
/// Return true if the set is a subset of another
459464
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
460465
iter::all(self, |v| other.contains(v))
@@ -626,6 +631,28 @@ mod test_map {
626631
mod test_set {
627632
use super::*;
628633

634+
#[test]
635+
fn test_disjoint() {
636+
let mut xs = linear::LinearSet::new();
637+
let mut ys = linear::LinearSet::new();
638+
assert xs.is_disjoint(&ys);
639+
assert ys.is_disjoint(&xs);
640+
assert xs.insert(5);
641+
assert ys.insert(11);
642+
assert xs.is_disjoint(&ys);
643+
assert ys.is_disjoint(&xs);
644+
assert xs.insert(7);
645+
assert xs.insert(19);
646+
assert xs.insert(4);
647+
assert ys.insert(2);
648+
assert ys.insert(-11);
649+
assert xs.is_disjoint(&ys);
650+
assert ys.is_disjoint(&xs);
651+
assert ys.insert(7);
652+
assert !xs.is_disjoint(&ys);
653+
assert !ys.is_disjoint(&xs);
654+
}
655+
629656
#[test]
630657
fn test_subset_and_superset() {
631658
let mut a = linear::LinearSet::new();

src/libstd/treemap.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,33 @@ impl <T: Ord> TreeSet<T>: Set<T> {
292292
/// present in the set.
293293
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
294294

295+
/// Return true if the set has no elements in common with `other`.
296+
/// This is equivalent to checking for an empty intersection.
297+
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
298+
let mut x = self.iter();
299+
let mut y = other.iter();
300+
unsafe { // purity workaround
301+
x = x.next();
302+
y = y.next();
303+
let mut a = x.get();
304+
let mut b = y.get();
305+
while a.is_some() && b.is_some() {
306+
let a1 = a.unwrap();
307+
let b1 = b.unwrap();
308+
if a1 < b1 {
309+
x = x.next();
310+
a = x.get();
311+
} else if b1 < a1 {
312+
y = y.next();
313+
b = y.get();
314+
} else {
315+
return false;
316+
}
317+
}
318+
}
319+
true
320+
}
321+
295322
/// Return true if the set is a subset of another
296323
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
297324
other.is_superset(self)
@@ -345,33 +372,6 @@ impl <T: Ord> TreeSet<T> {
345372
TreeSetIterator{iter: self.map.iter()}
346373
}
347374

348-
/// Return true if the set has no elements in common with `other`.
349-
/// This is equivalent to checking for an empty intersection.
350-
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
351-
let mut x = self.iter();
352-
let mut y = other.iter();
353-
unsafe { // purity workaround
354-
x = x.next();
355-
y = y.next();
356-
let mut a = x.get();
357-
let mut b = y.get();
358-
while a.is_some() && b.is_some() {
359-
let a1 = a.unwrap();
360-
let b1 = b.unwrap();
361-
if a1 < b1 {
362-
x = x.next();
363-
a = x.get();
364-
} else if b1 < a1 {
365-
y = y.next();
366-
b = y.get();
367-
} else {
368-
return false;
369-
}
370-
}
371-
}
372-
true
373-
}
374-
375375
/// Visit the values (in-order) representing the difference
376376
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
377377
let mut x = self.iter();

0 commit comments

Comments
 (0)