diff options
| author | Steven Fackler <sfackler@gmail.com> | 2014-04-13 20:22:58 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-04-15 19:45:00 -0700 |
| commit | c7325bdd8e29d57e7bc971b86accfb352c4262bc (patch) | |
| tree | 320b3c23ef713d787533b24f71a3d617bd8882ae | |
| parent | 54ec04f1c12c7fb4dbe5f01fdeb73d1079fef53d (diff) | |
Add a default impl for Set::is_superset
I also deleted a bunch of documentation that was copy/pasted from the trait definition.
| -rw-r--r-- | src/libcollections/hashmap.rs | 15 | ||||
| -rw-r--r-- | src/libcollections/treemap.rs | 36 | ||||
| -rw-r--r-- | 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 a2413d78e5f..46b93242685 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -1424,43 +1424,28 @@ impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> { } 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) } } diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index d964e73f696..b14c5fd9f29 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -573,74 +573,54 @@ impl<T: Ord + TotalOrd> Ord for TreeSet<T> { } 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) } } diff --git a/src/libstd/container.rs b/src/libstd/container.rs index 326b9fa3d33..e8ee3792dcf 100644 --- a/src/libstd/container.rs +++ b/src/libstd/container.rs @@ -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 } |
