diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 669a41be2b7..3c9ee50855e 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -66,6 +66,10 @@ pub trait Set: 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; diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index a9f3cd5247c..cf99c0e46b9 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -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 @@ pub mod linear { /// 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) -> 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) -> bool { iter::all(self, |v| other.contains(v)) @@ -626,6 +631,28 @@ mod test_map { 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(); diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index ece120bb647..b1c22f86c96 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -292,6 +292,33 @@ impl TreeSet: Set { /// 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) -> 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) -> bool { other.is_superset(self) @@ -345,33 +372,6 @@ impl TreeSet { 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) -> 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, f: fn(&T) -> bool) { let mut x = self.iter();