use std::fmt;
use std::hash::Hash;
use std::iter::FromIterator;
use super::map::SsoHashMap;
#[derive(Clone)]
pub struct SsoHashSet<T> {
map: SsoHashMap<T, ()>,
}
#[inline(always)]
fn entry_to_key<K, V>((k, _v): (K, V)) -> K {
k
}
impl<T> SsoHashSet<T> {
#[inline]
pub fn new() -> Self {
Self { map: SsoHashMap::new() }
}
#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self { map: SsoHashMap::with_capacity(cap) }
}
#[inline]
pub fn clear(&mut self) {
self.map.clear()
}
#[inline]
pub fn capacity(&self) -> usize {
self.map.capacity()
}
#[inline]
pub fn len(&self) -> usize {
self.map.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
#[inline]
pub fn iter(&'a self) -> impl Iterator<Item = &'a T> {
self.into_iter()
}
#[inline]
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
self.map.drain().map(entry_to_key)
}
}
impl<T: Eq + Hash> SsoHashSet<T> {
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.map.reserve(additional)
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.map.shrink_to_fit()
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.map.retain(|k, _v| f(k))
}
#[inline]
pub fn take(&mut self, value: &T) -> Option<T> {
self.map.remove_entry(value).map(entry_to_key)
}
#[inline]
pub fn get(&self, value: &T) -> Option<&T> {
self.map.get_key_value(value).map(entry_to_key)
}
#[inline]
pub fn insert(&mut self, elem: T) -> bool {
self.map.insert(elem, ()).is_none()
}
#[inline]
pub fn remove(&mut self, value: &T) -> bool {
self.map.remove(value).is_some()
}
#[inline]
pub fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}
}
impl<T: Eq + Hash> FromIterator<T> for SsoHashSet<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SsoHashSet<T> {
let mut set: SsoHashSet<T> = Default::default();
set.extend(iter);
set
}
}
impl<T> Default for SsoHashSet<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T: Eq + Hash> Extend<T> for SsoHashSet<T> {
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
for val in iter.into_iter() {
self.insert(val);
}
}
#[inline]
fn extend_one(&mut self, item: T) {
self.insert(item);
}
#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.map.extend_reserve(additional)
}
}
impl<'a, T> Extend<&'a T> for SsoHashSet<T>
where
T: 'a + Eq + Hash + Copy,
{
#[inline]
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
#[inline]
fn extend_one(&mut self, &item: &'a T) {
self.insert(item);
}
#[inline]
fn extend_reserve(&mut self, additional: usize) {
Extend::<T>::extend_reserve(self, additional)
}
}
impl<T> IntoIterator for SsoHashSet<T> {
type IntoIter = std::iter::Map<<SsoHashMap<T, ()> as IntoIterator>::IntoIter, fn((T, ())) -> T>;
type Item = <Self::IntoIter as Iterator>::Item;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.map.into_iter().map(entry_to_key)
}
}
impl<'a, T> IntoIterator for &'a SsoHashSet<T> {
type IntoIter = std::iter::Map<
<&'a SsoHashMap<T, ()> as IntoIterator>::IntoIter,
fn((&'a T, &'a ())) -> &'a T,
>;
type Item = <Self::IntoIter as Iterator>::Item;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.map.iter().map(entry_to_key)
}
}
impl<T> fmt::Debug for SsoHashSet<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}