use std::{collections::{HashSet, hash_map::RandomState}, hash::BuildHasher, cmp::*};
use core::hash::Hash;
#[derive(Debug)]
pub struct WrapperHashSet<'a, T, S = RandomState> {
pub hs: HashSet<T, S>,
parent: Option<&'a WrapperHashSet<'a, T>>
}
impl<'a, T: Clone + Eq + std::hash::Hash> WrapperHashSet<'a, T> {
#[inline]
#[must_use]
pub fn wrap(existing: HashSet<T>) -> WrapperHashSet<'a, T> {
WrapperHashSet { hs: existing, parent: None }
}
#[must_use]
pub fn new_subset<F: Fn(&T) -> bool>(&'a self, f: F) -> Self {
let mut res: HashSet<T> = HashSet::with_capacity(self.hs.capacity());
for elem in &self.hs {
if f(elem) {
res.insert(elem.clone());
}
}
WrapperHashSet { hs: res, parent: Some(self) }
}
#[inline]
#[must_use]
pub fn has_emerged(&self) -> bool {
self.parent.is_some()
}
#[inline]
#[must_use]
pub fn get_superset(&self) -> Option<&Self> {
self.parent
}
}
impl<'a, T: std::fmt::Debug> std::fmt::Display for WrapperHashSet<'a, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}, {:?}", self.hs, self.parent)
}
}
impl<'a, T: Eq + Hash, S: BuildHasher> PartialEq<WrapperHashSet<'a, T, S>> for WrapperHashSet<'a, T, S> {
fn eq(&self, other: &WrapperHashSet<T, S>) -> bool {
if self.hs.len() != other.hs.len() {
return false;
}
let b = self.hs.iter().all(|key| other.hs.contains(key));
b && self.parent == other.parent
}
}