use crate::map::BumpMap;
use core::fmt::{self, Debug};
use core::hash::{BuildHasher, Hash};
use core::iter::Iterator;
use equivalent::Equivalent;
#[cfg(feature = "std")]
use std::hash::RandomState;
#[cfg(feature = "std")]
pub struct BumpSet<T, S = RandomState> {
map: BumpMap<T, (), S>,
}
#[cfg(not(feature = "std"))]
pub struct BumpSet<T, S> {
map: BumpMap<T, (), S>,
}
#[cfg(feature = "std")]
impl<T> BumpSet<T, RandomState> {
#[inline]
#[must_use]
pub fn new() -> Self {
Default::default()
}
#[inline]
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, Default::default())
}
}
impl<T, S> BumpSet<T, S> {
pub const fn with_hasher(hasher: S) -> Self {
Self {
map: BumpMap::with_hasher(hasher),
}
}
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
Self {
map: BumpMap::with_capacity_and_hasher(capacity, hash_builder),
}
}
#[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()
}
pub fn clear(&mut self) {
self.map.clear();
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.map.iter().map(|(k, _)| k)
}
}
impl<T: Eq, S> BumpSet<T, S> {
pub fn drain(&mut self) -> impl Iterator<Item = T> {
self.map.drain().map(|(k, _)| k)
}
}
impl<T, S> BumpSet<T, S>
where
S: BuildHasher,
{
#[inline]
pub fn hasher(&self) -> &S {
self.map.hasher()
}
pub fn contains<Q>(&self, value: &Q) -> bool
where
Q: ?Sized + Hash + Equivalent<T>,
{
self.map.contains_key(value)
}
pub fn get<Q>(&self, value: &Q) -> Option<&T>
where
Q: ?Sized + Hash + Equivalent<T>,
{
self.map.get_key_value(value).map(|(k, _)| k)
}
}
impl<T, S> BumpSet<T, S>
where
T: Hash + Eq,
S: BuildHasher,
{
pub fn insert(&self, value: T) -> bool {
self.map.try_insert(value, ()).is_ok()
}
}
impl<K, S> core::clone::Clone for BumpSet<K, S>
where
K: Clone,
S: Clone,
{
fn clone(&self) -> Self {
Self {
map: self.map.clone(),
}
}
}
impl<K, S> Debug for BumpSet<K, S>
where
K: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<K, S> Default for BumpSet<K, S>
where
S: Default,
{
#[inline]
fn default() -> Self {
Self::with_hasher(Default::default())
}
}
impl<K, S> Eq for BumpSet<K, S>
where
K: Eq + Hash,
S: BuildHasher,
{
}
impl<K, S> PartialEq for BumpSet<K, S>
where
K: Eq + Hash,
S: BuildHasher,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
self.iter().all(|k| other.contains(k))
}
}
#[cfg(feature = "std")]
impl<T, const N: usize> From<[T; N]> for BumpSet<T, RandomState>
where
T: Eq + Hash,
{
fn from(arr: [T; N]) -> Self {
let mut set = Self::with_capacity(N);
set.extend(arr);
set
}
}
impl<T, S> FromIterator<T> for BumpSet<T, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut set = BumpSet::with_hasher(Default::default());
set.extend(iter);
set
}
}
impl<T, S> Extend<T> for BumpSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for item in iter {
self.insert(item);
}
}
}
impl<'a, T, S> Extend<&'a T> for BumpSet<T, S>
where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().copied());
}
}