usecrate::RandomState; use std::collections::{hash_set, HashSet}; use std::fmt::{self, Debug}; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::{BitAnd, BitOr, BitXor, Deref, DerefMut, Sub};
#[cfg(feature = "serde")] use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
/// A [`HashSet`](std::collections::HashSet) using [`RandomState`](crate::RandomState) to hash the items. /// (Requires the `std` feature to be enabled.) #[derive(Clone)] pubstruct AHashSet<T, S = RandomState>(HashSet<T, S>);
impl<T> AHashSet<T, RandomState> { /// This crates a hashset using [RandomState::new]. /// See the documentation in [RandomSource] for notes about key strength. pubfn new() -> Self {
AHashSet(HashSet::with_hasher(RandomState::new()))
}
/// This crates a hashset with the specified capacity using [RandomState::new]. /// See the documentation in [RandomSource] for notes about key strength. pubfn with_capacity(capacity: usize) -> Self {
AHashSet(HashSet::with_capacity_and_hasher(capacity, RandomState::new()))
}
}
impl<T, S> Eq for AHashSet<T, S> where
T: Eq + Hash,
S: BuildHasher,
{
}
impl<T, S> BitOr<&AHashSet<T, S>> for &AHashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{ type Output = AHashSet<T, S>;
/// Returns the union of `self` and `rhs` as a new `AHashSet<T, S>`. /// /// # Examples /// /// ``` /// use ahash::AHashSet; /// /// let a: AHashSet<_> = vec![1, 2, 3].into_iter().collect(); /// let b: AHashSet<_> = vec![3, 4, 5].into_iter().collect(); /// /// let set = &a | &b; /// /// let mut i = 0; /// let expected = [1, 2, 3, 4, 5]; /// for x in &set { /// assert!(expected.contains(x)); /// i += 1; /// } /// assert_eq!(i, expected.len()); /// ``` fn bitor(self, rhs: &AHashSet<T, S>) -> AHashSet<T, S> {
AHashSet(self.0.bitor(&rhs.0))
}
}
impl<T, S> BitAnd<&AHashSet<T, S>> for &AHashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{ type Output = AHashSet<T, S>;
/// Returns the intersection of `self` and `rhs` as a new `AHashSet<T, S>`. /// /// # Examples /// /// ``` /// use ahash::AHashSet; /// /// let a: AHashSet<_> = vec![1, 2, 3].into_iter().collect(); /// let b: AHashSet<_> = vec![2, 3, 4].into_iter().collect(); /// /// let set = &a & &b; /// /// let mut i = 0; /// let expected = [2, 3]; /// for x in &set { /// assert!(expected.contains(x)); /// i += 1; /// } /// assert_eq!(i, expected.len()); /// ``` fn bitand(self, rhs: &AHashSet<T, S>) -> AHashSet<T, S> {
AHashSet(self.0.bitand(&rhs.0))
}
}
impl<T, S> BitXor<&AHashSet<T, S>> for &AHashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{ type Output = AHashSet<T, S>;
/// Returns the symmetric difference of `self` and `rhs` as a new `AHashSet<T, S>`. /// /// # Examples /// /// ``` /// use ahash::AHashSet; /// /// let a: AHashSet<_> = vec![1, 2, 3].into_iter().collect(); /// let b: AHashSet<_> = vec![3, 4, 5].into_iter().collect(); /// /// let set = &a ^ &b; /// /// let mut i = 0; /// let expected = [1, 2, 4, 5]; /// for x in &set { /// assert!(expected.contains(x)); /// i += 1; /// } /// assert_eq!(i, expected.len()); /// ``` fn bitxor(self, rhs: &AHashSet<T, S>) -> AHashSet<T, S> {
AHashSet(self.0.bitxor(&rhs.0))
}
}
impl<T, S> Sub<&AHashSet<T, S>> for &AHashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{ type Output = AHashSet<T, S>;
/// Returns the difference of `self` and `rhs` as a new `AHashSet<T, S>`. /// /// # Examples /// /// ``` /// use ahash::AHashSet; /// /// let a: AHashSet<_> = vec![1, 2, 3].into_iter().collect(); /// let b: AHashSet<_> = vec![3, 4, 5].into_iter().collect(); /// /// let set = &a - &b; /// /// let mut i = 0; /// let expected = [1, 2]; /// for x in &set { /// assert!(expected.contains(x)); /// i += 1; /// } /// assert_eq!(i, expected.len()); /// ``` fn sub(self, rhs: &AHashSet<T, S>) -> AHashSet<T, S> {
AHashSet(self.0.sub(&rhs.0))
}
}
impl<T> FromIterator<T> for AHashSet<T, RandomState> where
T: Eq + Hash,
{ /// This crates a hashset from the provided iterator using [RandomState::new]. /// See the documentation in [RandomSource] for notes about key strength. #[inline] fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> AHashSet<T> { letmut inner = HashSet::with_hasher(RandomState::new());
inner.extend(iter);
AHashSet(inner)
}
}
impl<'a, T, S> IntoIterator for &'a AHashSet<T, S> { type Item = &'a T; type IntoIter = hash_set::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter {
(&self.0).iter()
}
}
impl<T, S> IntoIterator for AHashSet<T, S> { type Item = T; type IntoIter = hash_set::IntoIter<T>; fn into_iter(self) -> Self::IntoIter { self.0.into_iter()
}
}
/// NOTE: For safety this trait impl is only available available if either of the flags `runtime-rng` (on by default) or /// `compile-time-rng` are enabled. This is to prevent weakly keyed maps from being accidentally created. Instead one of /// constructors for [RandomState] must be used. #[cfg(any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng"))] impl<T> Default for AHashSet<T, RandomState> { /// Creates an empty `AHashSet<T, S>` with the `Default` value for the hasher. #[inline] fn default() -> AHashSet<T, RandomState> {
AHashSet(HashSet::default())
}
}
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.