提交 42cafcee 编写于 作者: D Daniel Micay

add is_disjoint to the Set trait

上级 bfa9c9a0
......@@ -66,6 +66,10 @@ pub trait Set<T>: Mutable {
/// present in the set.
fn remove(&mut self, value: &T) -> bool;
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
pure fn is_disjoint(&self, other: &self) -> bool;
/// Return true if the set is a subset of another
pure fn is_subset(&self, other: &self) -> bool;
......
......@@ -14,16 +14,15 @@
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
use container::{Container, Mutable, Map, Set};
use cmp::Eq;
use hash::Hash;
use to_bytes::IterBytes;
/// Open addressing with linear probing.
pub mod linear {
use super::*;
use iter::BaseIter;
use container::{Container, Mutable, Map, Set};
use cmp::Eq;
use cmp;
use hash::Hash;
use iter;
use kinds::Copy;
......@@ -455,6 +454,12 @@ fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
/// present in the set.
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
pure fn is_disjoint(&self, other: &LinearSet<T>) -> bool {
iter::all(self, |v| !other.contains(v))
}
/// Return true if the set is a subset of another
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
iter::all(self, |v| other.contains(v))
......@@ -626,6 +631,28 @@ pub fn test_expand() {
mod test_set {
use super::*;
#[test]
fn test_disjoint() {
let mut xs = linear::LinearSet::new();
let mut ys = linear::LinearSet::new();
assert xs.is_disjoint(&ys);
assert ys.is_disjoint(&xs);
assert xs.insert(5);
assert ys.insert(11);
assert xs.is_disjoint(&ys);
assert ys.is_disjoint(&xs);
assert xs.insert(7);
assert xs.insert(19);
assert xs.insert(4);
assert ys.insert(2);
assert ys.insert(-11);
assert xs.is_disjoint(&ys);
assert ys.is_disjoint(&xs);
assert ys.insert(7);
assert !xs.is_disjoint(&ys);
assert !ys.is_disjoint(&xs);
}
#[test]
fn test_subset_and_superset() {
let mut a = linear::LinearSet::new();
......
......@@ -292,6 +292,33 @@ fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
/// present in the set.
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
x = x.next();
y = y.next();
let mut a = x.get();
let mut b = y.get();
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
if a1 < b1 {
x = x.next();
a = x.get();
} else if b1 < a1 {
y = y.next();
b = y.get();
} else {
return false;
}
}
}
true
}
/// Return true if the set is a subset of another
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
other.is_superset(self)
......@@ -345,33 +372,6 @@ impl <T: Ord> TreeSet<T> {
TreeSetIterator{iter: self.map.iter()}
}
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
x = x.next();
y = y.next();
let mut a = x.get();
let mut b = y.get();
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
if a1 < b1 {
x = x.next();
a = x.get();
} else if b1 < a1 {
y = y.next();
b = y.get();
} else {
return false;
}
}
}
true
}
/// Visit the values (in-order) representing the difference
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
let mut x = self.iter();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册