From c7325bdd8e29d57e7bc971b86accfb352c4262bc Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 13 Apr 2014 20:22:58 -0700 Subject: [PATCH] Add a default impl for Set::is_superset I also deleted a bunch of documentation that was copy/pasted from the trait definition. --- src/libcollections/hashmap.rs | 15 --------------- src/libcollections/treemap.rs | 36 ++++++++--------------------------- src/libstd/container.rs | 4 +++- 3 files changed, 11 insertions(+), 44 deletions(-) diff --git a/src/libcollections/hashmap.rs b/src/libcollections/hashmap.rs index a2413d78e5fe..46b93242685e 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -1424,43 +1424,28 @@ impl, S, H: Hasher> Eq for HashSet { } impl, S, H: Hasher> Container for HashSet { - /// Return the number of elements in the set fn len(&self) -> uint { self.map.len() } } impl, S, H: Hasher> Mutable for HashSet { - /// Clear the set, removing all values. fn clear(&mut self) { self.map.clear() } } impl, S, H: Hasher> Set for HashSet { - /// Return true if the set contains a value fn contains(&self, value: &T) -> bool { self.map.search(value).is_some() } - /// Return true if the set has no elements in common with `other`. - /// This is equivalent to checking for an empty intersection. fn is_disjoint(&self, other: &HashSet) -> bool { self.iter().all(|v| !other.contains(v)) } - /// Return true if the set is a subset of another fn is_subset(&self, other: &HashSet) -> bool { self.iter().all(|v| other.contains(v)) } - - /// Return true if the set is a superset of another - fn is_superset(&self, other: &HashSet) -> bool { - other.is_subset(self) - } } impl, S, H: Hasher> MutableSet for HashSet { - /// Add a value to the set. Return true if the value was not already - /// present in the set. fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) } - /// 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) } } diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index d964e73f6962..b14c5fd9f295 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -573,74 +573,54 @@ impl Ord for TreeSet { } impl Container for TreeSet { - /// Return the number of elements in the set #[inline] fn len(&self) -> uint { self.map.len() } - - /// Return true if the set contains no elements - #[inline] - fn is_empty(&self) -> bool { self.map.is_empty() } } impl Mutable for TreeSet { - /// Clear the set, removing all values. #[inline] fn clear(&mut self) { self.map.clear() } } impl Set for TreeSet { - /// Return true if the set contains a value #[inline] fn contains(&self, value: &T) -> bool { self.map.contains_key(value) } - /// Return true if the set has no elements in common with `other`. - /// This is equivalent to checking for an empty intersection. fn is_disjoint(&self, other: &TreeSet) -> bool { self.intersection(other).next().is_none() } - /// Return true if the set is a subset of another - #[inline] fn is_subset(&self, other: &TreeSet) -> bool { - other.is_superset(self) - } - - /// Return true if the set is a superset of another - fn is_superset(&self, other: &TreeSet) -> bool { let mut x = self.iter(); let mut y = other.iter(); let mut a = x.next(); let mut b = y.next(); - while b.is_some() { - if a.is_none() { - return false + while a.is_some() { + if b.is_none() { + return false; } let a1 = a.unwrap(); let b1 = b.unwrap(); - match a1.cmp(b1) { - Less => (), - Greater => return false, - Equal => b = y.next(), + match b1.cmp(a1) { + Less => (), + Greater => return false, + Equal => a = x.next(), } - a = x.next(); + b = y.next(); } true } } impl MutableSet for TreeSet { - /// Add a value to the set. Return true if the value was not already - /// present in the set. #[inline] fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) } - /// Remove a value from the set. Return true if the value was - /// present in the set. #[inline] fn remove(&mut self, value: &T) -> bool { self.map.remove(value) } } diff --git a/src/libstd/container.rs b/src/libstd/container.rs index 326b9fa3d332..e8ee3792dcf2 100644 --- a/src/libstd/container.rs +++ b/src/libstd/container.rs @@ -88,7 +88,9 @@ pub trait Set: Container { fn is_subset(&self, other: &Self) -> bool; /// Return true if the set is a superset of another - fn is_superset(&self, other: &Self) -> bool; + fn is_superset(&self, other: &Self) -> bool { + other.is_subset(self) + } // FIXME #8154: Add difference, sym. difference, intersection and union iterators }