use std::borrow::Borrow;
use std::collections::HashSet;
use std::hash::Hash;
use std::ops::Sub;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnorderedHashSet<Key: Hash + Eq>(HashSet<Key>);
impl<Key: Hash + Eq> UnorderedHashSet<Key> {
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)
}
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> Default for UnorderedHashSet<Key> {
fn default() -> Self {
Self(Default::default())
}
}
impl<Key: Hash + Eq> FromIterator<Key> for UnorderedHashSet<Key> {
fn from_iter<T: IntoIterator<Item = Key>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl<'a, Key: Hash + Eq + Clone> Sub<&'a UnorderedHashSet<Key>> for &'a UnorderedHashSet<Key> {
type Output = UnorderedHashSet<Key>;
fn sub(self, rhs: Self) -> Self::Output {
UnorderedHashSet::<Key>(&self.0 - &rhs.0)
}
}