/// A default set of values. /// /// Provides an API compatible with both [`HashSet`] and [`BTreeSet`]. /// /// [`HashSet`]: hashbrown::HashSet /// [`BTreeSet`]: alloc::collections::BTreeSet #[derive(Debug, Clone)] pubstruct Set<T> { /// The underlying hash-set or btree-set data structure used.
inner: detail::SetImpl<T>,
}
impl<T> Set<T> { /// Clears the [`Set`], removing all elements. #[inline] pubfn clear(&mutself) { self.inner.clear()
}
/// Retains only the elements specified by the predicate. /// /// In other words, remove all elements `e` for which `f(&e)` returns `false`. /// The elements are visited in unsorted (and unspecified) order. #[inline] pubfn retain<F>(&mutself, f: F) where
T: Ord,
F: FnMut(&T) -> bool,
{ self.inner.retain(f)
}
/// Returns the number of elements in the [`Set`]. #[inline] pubfn len(&self) -> usize { self.inner.len()
}
/// Returns `true` if the [`Set`] contains no elements. #[inline] pubfn is_empty(&self) -> bool { self.inner.is_empty()
}
/// Returns an iterator that yields the items in the [`Set`]. #[inline] pubfn iter(&self) -> Iter<'_, T> {
Iter {
inner: self.inner.iter(),
}
}
}
impl<T> Set<T> where
T: Eq + Hash + Ord,
{ /// Reserves capacity for at least `additional` more elements to be inserted in the [`Set`]. #[inline] pubfn reserve(&mutself, additional: usize) { #[cfg(not(feature = "no-hash-maps"))] self.inner.reserve(additional); #[cfg(feature = "no-hash-maps")] let _ = additional;
}
/// Returns true if the [`Set`] contains an element equal to the `value`. #[inline] pubfn contains<Q>(&self, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.contains(value)
}
/// Returns a reference to the element in the [`Set`], if any, that is equal to the `value`. #[inline] pubfn get<Q>(&self, value: &Q) -> Option<&T> where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.get(value)
}
/// Adds `value` to the [`Set`]. /// /// Returns whether the value was newly inserted: /// /// - Returns `true` if the set did not previously contain an equal value. /// - Returns `false` otherwise and the entry is not updated. #[inline] pubfn insert(&mutself, value: T) -> bool { self.inner.insert(value)
}
/// If the set contains an element equal to the value, removes it from the [`Set`] and drops it. /// /// Returns `true` if such an element was present, otherwise `false`. #[inline] pubfn remove<Q>(&mutself, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.remove(value)
}
/// Removes and returns the element in the [`Set`], if any, that is equal to /// the value. /// /// The value may be any borrowed form of the set's element type, /// but the ordering on the borrowed form *must* match the /// ordering on the element type. #[inline] pubfn take<Q>(&mutself, value: &Q) -> Option<T> where
T: Borrow<Q>,
Q: ?Sized + Hash + Ord,
{ self.inner.take(value)
}
/// Adds a value to the [`Set`], replacing the existing value, if any, that is equal to the given /// one. Returns the replaced value. #[inline] pubfn replace(&mutself, value: T) -> Option<T> { self.inner.replace(value)
}
/// Returns `true` if `self` has no elements in common with `other`. /// This is equivalent to checking for an empty intersection. #[inline] pubfn is_disjoint(&self, other: &Self) -> bool { self.inner.is_disjoint(&other.inner)
}
/// Returns `true` if the [`Set`] is a subset of another, /// i.e., `other` contains at least all the values in `self`. #[inline] pubfn is_subset(&self, other: &Self) -> bool { self.inner.is_subset(&other.inner)
}
/// Returns `true` if the [`Set`] is a superset of another, /// i.e., `self` contains at least all the values in `other`. #[inline] pubfn is_superset(&self, other: &Self) -> bool { self.inner.is_superset(&other.inner)
}
/// Visits the values representing the difference, /// i.e., the values that are in `self` but not in `other`. #[inline] pubfn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T> {
Difference {
inner: self.inner.difference(&other.inner),
}
}
/// Visits the values representing the symmetric difference, /// i.e., the values that are in `self` or in `other` but not in both. #[inline] pubfn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T> {
SymmetricDifference {
inner: self.inner.symmetric_difference(&other.inner),
}
}
/// Visits the values representing the intersection, /// i.e., the values that are both in `self` and `other`. /// /// When an equal element is present in `self` and `other` /// then the resulting `Intersection` may yield references to /// one or the other. This can be relevant if `T` contains fields which /// are not compared by its `Eq` implementation, and may hold different /// value between the two equal copies of `T` in the two sets. #[inline] pubfn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> {
Intersection {
inner: self.inner.intersection(&other.inner),
}
}
/// Visits the values representing the union, /// i.e., all the values in `self` or `other`, without duplicates. #[inline] pubfn union<'a>(&'a self, other: &'a Self) -> Union<'a, T> {
Union {
inner: self.inner.union(&other.inner),
}
}
}
impl<T> FusedIterator for IntoIter<T> where detail::IntoIterImpl<T>: FusedIterator {}
/// A lazy iterator producing elements in the difference of [`Set`]s. /// /// This `struct` is created by the [`difference`] method on [`Set`]. /// See its documentation for more. /// /// [`difference`]: Set::difference pubstruct Difference<'a, T: 'a> {
inner: detail::DifferenceImpl<'a, T>,
}
impl<'a, T> FusedIterator for Difference<'a, T> where
T: Hash + Eq + Ord,
detail::DifferenceImpl<'a, T>: FusedIterator,
{
}
/// A lazy iterator producing elements in the intersection of [`Set`]s. /// /// This `struct` is created by the [`intersection`] method on [`Set`]. /// See its documentation for more. /// /// [`intersection`]: Set::intersection pubstruct Intersection<'a, T: 'a> {
inner: detail::IntersectionImpl<'a, T>,
}
impl<'a, T> FusedIterator for Intersection<'a, T> where
T: Hash + Eq + Ord,
detail::IntersectionImpl<'a, T>: FusedIterator,
{
}
/// A lazy iterator producing elements in the symmetric difference of [`Set`]s. /// /// This `struct` is created by the [`symmetric_difference`] method on /// [`Set`]. See its documentation for more. /// /// [`symmetric_difference`]: Set::symmetric_difference pubstruct SymmetricDifference<'a, T: 'a> {
inner: detail::SymmetricDifferenceImpl<'a, T>,
}
impl<'a, T> FusedIterator for SymmetricDifference<'a, T> where
T: Hash + Eq + Ord,
detail::SymmetricDifferenceImpl<'a, T>: FusedIterator,
{
}
/// A lazy iterator producing elements in the union of [`Set`]s. /// /// This `struct` is created by the [`union`] method on /// [`Set`]. See its documentation for more. /// /// [`union`]: Set::union pubstruct Union<'a, T: 'a> {
inner: detail::UnionImpl<'a, T>,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.