]> git.lizzy.rs Git - rust.git/commitdiff
Add a default impl for Set::is_superset
authorSteven Fackler <sfackler@gmail.com>
Mon, 14 Apr 2014 03:22:58 +0000 (20:22 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 16 Apr 2014 02:45:00 +0000 (19:45 -0700)
I also deleted a bunch of documentation that was copy/pasted from the
trait definition.

src/libcollections/hashmap.rs
src/libcollections/treemap.rs
src/libstd/container.rs

index a2413d78e5fe04d7936f2efbc0e69d1a139b4dc2..46b93242685e4380c3006e08c7a2bff7eda6ecee 100644 (file)
@@ -1424,43 +1424,28 @@ fn eq(&self, other: &HashSet<T, H>) -> bool {
 }
 
 impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Container for HashSet<T, H> {
-    /// Return the number of elements in the set
     fn len(&self) -> uint { self.map.len() }
 }
 
 impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Mutable for HashSet<T, H> {
-    /// Clear the set, removing all values.
     fn clear(&mut self) { self.map.clear() }
 }
 
 impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Set<T> for HashSet<T, H> {
-    /// 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<T, H>) -> bool {
         self.iter().all(|v| !other.contains(v))
     }
 
-    /// Return true if the set is a subset of another
     fn is_subset(&self, other: &HashSet<T, H>) -> bool {
         self.iter().all(|v| other.contains(v))
     }
-
-    /// Return true if the set is a superset of another
-    fn is_superset(&self, other: &HashSet<T, H>) -> bool {
-        other.is_subset(self)
-    }
 }
 
 impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
-    /// 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) }
 }
 
index d964e73f69620654ebe3fd4533aa467747d31225..b14c5fd9f29599d51b4f6e1d75be67153aab81ff 100644 (file)
@@ -573,74 +573,54 @@ fn gt(&self, other: &TreeSet<T>) -> bool { self.map > other.map }
 }
 
 impl<T: TotalOrd> Container for TreeSet<T> {
-    /// 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<T: TotalOrd> Mutable for TreeSet<T> {
-    /// Clear the set, removing all values.
     #[inline]
     fn clear(&mut self) { self.map.clear() }
 }
 
 impl<T: TotalOrd> Set<T> for TreeSet<T> {
-    /// 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<T>) -> bool {
         self.intersection(other).next().is_none()
     }
 
-    /// Return true if the set is a subset of another
-    #[inline]
     fn is_subset(&self, other: &TreeSet<T>) -> bool {
-        other.is_superset(self)
-    }
-
-    /// Return true if the set is a superset of another
-    fn is_superset(&self, other: &TreeSet<T>) -> 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<T: TotalOrd> MutableSet<T> for TreeSet<T> {
-    /// 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) }
 }
index 326b9fa3d33216b0c701ebf7cf3c19953a1693e3..e8ee3792dcf2cdf423398ec3606003b594d9b192 100644 (file)
@@ -88,7 +88,9 @@ pub trait Set<T>: 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
 }