From bfa9c9a00f0a301a5e936ae14c6a95e98f3bf5ee Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 29 Jan 2013 16:07:11 -0500 Subject: add is_subset and is_superset to the Set trait --- src/libstd/treemap.rs | 74 +++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index ed06d6a34b5..ece120bb647 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -291,6 +291,43 @@ impl TreeSet: Set { /// Remove a value from the set. Return true if the value was /// present in the set. fn remove(&mut self, value: &T) -> bool { self.map.remove(value) } + + /// Return true if the set is a subset of another + pure fn is_subset(&self, other: &TreeSet) -> bool { + other.is_superset(self) + } + + /// Return true if the set is a superset of another + pure fn is_superset(&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 b.is_some() { + if a.is_none() { + return false + } + + let a1 = a.unwrap(); + let b1 = b.unwrap(); + + if b1 < a1 { + return false + } + + if !(a1 < b1) { + y = y.next(); + b = y.get(); + } + x = x.next(); + a = x.get(); + } + } + true + } } impl TreeSet { @@ -335,43 +372,6 @@ impl TreeSet { true } - /// Check of the set is a subset of another - pure fn is_subset(&self, other: &TreeSet) -> bool { - other.is_superset(self) - } - - /// Check of the set is a superset of another - pure fn is_superset(&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 b.is_some() { - if a.is_none() { - return false - } - - let a1 = a.unwrap(); - let b1 = b.unwrap(); - - if b1 < a1 { - return false - } - - if !(a1 < b1) { - y = y.next(); - b = y.get(); - } - x = x.next(); - a = x.get(); - } - } - true - } - /// Visit the values (in-order) representing the difference pure fn difference(&self, other: &TreeSet, f: fn(&T) -> bool) { let mut x = self.iter(); -- cgit 1.4.1-3-g733a5