}
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) }
}
}
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) }
}
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
}