mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-05 11:33:04 +00:00
add is_disjoint to the Set trait
This commit is contained in:
parent
bfa9c9a00f
commit
42cafcee2c
@ -66,6 +66,10 @@ pub trait Set<T>: Mutable {
|
|||||||
/// present in the set.
|
/// present in the set.
|
||||||
fn remove(&mut self, value: &T) -> bool;
|
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
|
/// Return true if the set is a subset of another
|
||||||
pure fn is_subset(&self, other: &self) -> bool;
|
pure fn is_subset(&self, other: &self) -> bool;
|
||||||
|
|
||||||
|
@ -14,16 +14,15 @@
|
|||||||
#[forbid(deprecated_mode)];
|
#[forbid(deprecated_mode)];
|
||||||
#[forbid(deprecated_pattern)];
|
#[forbid(deprecated_pattern)];
|
||||||
|
|
||||||
|
use container::{Container, Mutable, Map, Set};
|
||||||
use cmp::Eq;
|
use cmp::Eq;
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use to_bytes::IterBytes;
|
use to_bytes::IterBytes;
|
||||||
|
|
||||||
/// Open addressing with linear probing.
|
/// Open addressing with linear probing.
|
||||||
pub mod linear {
|
pub mod linear {
|
||||||
|
use super::*;
|
||||||
use iter::BaseIter;
|
use iter::BaseIter;
|
||||||
use container::{Container, Mutable, Map, Set};
|
|
||||||
use cmp::Eq;
|
|
||||||
use cmp;
|
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use iter;
|
use iter;
|
||||||
use kinds::Copy;
|
use kinds::Copy;
|
||||||
@ -455,6 +454,12 @@ pub mod linear {
|
|||||||
/// present in the set.
|
/// present in the set.
|
||||||
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
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
|
/// Return true if the set is a subset of another
|
||||||
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
|
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
|
||||||
iter::all(self, |v| other.contains(v))
|
iter::all(self, |v| other.contains(v))
|
||||||
@ -626,6 +631,28 @@ mod test_map {
|
|||||||
mod test_set {
|
mod test_set {
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn test_subset_and_superset() {
|
fn test_subset_and_superset() {
|
||||||
let mut a = linear::LinearSet::new();
|
let mut a = linear::LinearSet::new();
|
||||||
|
@ -292,6 +292,33 @@ impl <T: Ord> TreeSet<T>: Set<T> {
|
|||||||
/// present in the set.
|
/// present in the set.
|
||||||
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
|
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
|
/// Return true if the set is a subset of another
|
||||||
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
|
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
|
||||||
other.is_superset(self)
|
other.is_superset(self)
|
||||||
@ -345,33 +372,6 @@ impl <T: Ord> TreeSet<T> {
|
|||||||
TreeSetIterator{iter: self.map.iter()}
|
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
|
/// Visit the values (in-order) representing the difference
|
||||||
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
|
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
|
||||||
let mut x = self.iter();
|
let mut x = self.iter();
|
||||||
|
Loading…
Reference in New Issue
Block a user