cairo_lang_utils/
unordered_hash_set.rsuse core::borrow::Borrow;
use core::hash::{BuildHasher, Hash};
use core::ops::Sub;
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
#[cfg(feature = "std")]
use std::collections::HashSet;
#[cfg(not(feature = "std"))]
use hashbrown::HashSet;
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub struct UnorderedHashSet<Key, BH = RandomState>(HashSet<Key, BH>);
#[cfg(not(feature = "std"))]
#[derive(Clone, Debug)]
pub struct UnorderedHashSet<Key, BH = hashbrown::hash_map::DefaultHashBuilder>(HashSet<Key, BH>);
impl<K, BH: Default> Default for UnorderedHashSet<K, BH> {
#[cfg(feature = "std")]
fn default() -> Self {
Self(Default::default())
}
#[cfg(not(feature = "std"))]
fn default() -> Self {
Self(HashSet::with_hasher(Default::default()))
}
}
impl<K, BH> PartialEq for UnorderedHashSet<K, BH>
where
K: Eq + Hash,
BH: BuildHasher,
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<K, BH> Eq for UnorderedHashSet<K, BH>
where
K: Eq + Hash,
BH: BuildHasher,
{
}
impl<Key, BH> UnorderedHashSet<Key, BH> {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn clear(&mut self) {
self.0.clear()
}
}
impl<Key: Hash + Eq, BH: BuildHasher> UnorderedHashSet<Key, BH> {
pub fn insert(&mut self, key: Key) -> bool {
self.0.insert(key)
}
pub fn remove<Q: ?Sized + Hash + Eq>(&mut self, value: &Q) -> bool
where
Key: Borrow<Q>,
{
self.0.remove(value)
}
pub fn extend<I: IntoIterator<Item = Key>>(&mut self, iter: I) {
self.0.extend(iter)
}
pub fn extend_unordered(&mut self, other: Self) {
self.0.extend(other.0)
}
pub fn contains<Q: ?Sized + Hash + Eq>(&self, value: &Q) -> bool
where
Key: Borrow<Q>,
{
self.0.contains(value)
}
}
impl<Key: Hash + Eq, BH: BuildHasher + Default> FromIterator<Key> for UnorderedHashSet<Key, BH> {
fn from_iter<T: IntoIterator<Item = Key>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl<'a, Key, BH> Sub<&'a UnorderedHashSet<Key, BH>> for &'a UnorderedHashSet<Key, BH>
where
&'a HashSet<Key, BH>: Sub<Output = HashSet<Key, BH>>,
{
type Output = UnorderedHashSet<Key, BH>;
fn sub(self, rhs: Self) -> Self::Output {
UnorderedHashSet::<Key, BH>(&self.0 - &rhs.0)
}
}