use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::iter::FusedIterator;
use super::SmallSet;
use crate::small_map::SmallMap;
impl<T, const N: usize, S: Default> SmallSet<T, N, S> {
#[inline]
pub fn new() -> Self {
SmallSet(SmallMap::new())
}
}
impl<T, const N: usize, S> SmallSet<T, N, S> {
#[inline]
pub fn with_hasher(hasher: S) -> Self {
SmallSet(SmallMap::with_hasher(hasher))
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self
where
S: Default,
{
SmallSet(SmallMap::with_capacity(capacity))
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn is_inline(&self) -> bool {
self.0.is_inline()
}
#[inline]
pub fn hasher(&self) -> &S {
self.0.hasher()
}
}
impl<T, const N: usize, S> SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
pub fn insert(&mut self, value: T) -> bool {
self.0.insert(value, ()).is_none()
}
#[inline]
pub fn remove<Q>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.remove(value).is_some()
}
#[inline]
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.remove_entry(value).map(|(k, _)| k)
}
#[inline]
pub fn contains<Q>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.contains_key(value)
}
#[inline]
pub fn get<Q>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.get_key_value(value).map(|(k, _)| k)
}
#[inline]
pub fn clear(&mut self) {
self.0.clear();
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.0.retain(|k, _| f(k));
}
}
impl<T, const N: usize, S> SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
pub fn is_disjoint(&self, other: &Self) -> bool {
if self.len() <= other.len() {
self.iter().all(|v| !other.contains(v))
} else {
other.iter().all(|v| !self.contains(v))
}
}
pub fn is_subset(&self, other: &Self) -> bool {
if self.len() > other.len() {
return false;
}
self.iter().all(|v| other.contains(v))
}
#[inline]
pub fn is_superset(&self, other: &Self) -> bool {
other.is_subset(self)
}
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T, N, S> {
let (smaller, larger) =
if self.len() <= other.len() { (self, other) } else { (other, self) };
Intersection { iter: smaller.iter(), other: larger }
}
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, N, S> {
Union {
iter: self.iter(),
other_iter: other.iter(),
current: self,
}
}
pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T, N, S> {
Difference { iter: self.iter(), other }
}
pub fn symmetric_difference<'a>(
&'a self,
other: &'a Self,
) -> SymmetricDifference<'a, T, N, S> {
SymmetricDifference {
iter_a: self.difference(other),
iter_b: other.difference(self),
}
}
}
impl<T, const N: usize, S> SmallSet<T, N, S> {
#[inline]
pub fn capacity(&self) -> usize {
self.0.capacity()
}
#[inline]
pub fn shrink_to_fit(&mut self)
where
T: Eq + Hash,
S: BuildHasher + Default,
{
self.0.shrink_to_fit();
}
}
impl<T, const N: usize, S> SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.0.reserve(additional);
}
}
impl<T, const N: usize, S> SmallSet<T, N, S> {
#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter(self.0.iter())
}
}
pub struct Iter<'a, T>(crate::small_map::Iter<'a, T, ()>);
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<T> ExactSizeIterator for Iter<'_, T> {
#[inline]
fn len(&self) -> usize {
self.0.len()
}
}
impl<T> FusedIterator for Iter<'_, T> {}
pub struct IntoIter<T, const N: usize>(crate::small_map::IntoIter<T, (), N>);
impl<T, const N: usize> Iterator for IntoIter<T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
#[inline]
fn len(&self) -> usize {
self.0.len()
}
}
impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
pub struct Intersection<'a, T, const N: usize, S> {
iter: Iter<'a, T>,
other: &'a SmallSet<T, N, S>,
}
impl<'a, T, const N: usize, S> Iterator for Intersection<'a, T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.by_ref().find(|v| self.other.contains(v))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.iter.size_hint().1)
}
}
impl<T: Eq + Hash, const N: usize, S: BuildHasher> FusedIterator
for Intersection<'_, T, N, S>
{
}
pub struct Union<'a, T, const N: usize, S> {
iter: Iter<'a, T>,
other_iter: Iter<'a, T>,
current: &'a SmallSet<T, N, S>,
}
impl<'a, T, const N: usize, S> Iterator for Union<'a, T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.or_else(|| self.other_iter.by_ref().find(|v| !self.current.contains(v)))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (lo, hi) = self.iter.size_hint();
let (_, other_hi) = self.other_iter.size_hint();
(lo, hi.and_then(|a| other_hi.map(|b| a + b)))
}
}
impl<T: Eq + Hash, const N: usize, S: BuildHasher> FusedIterator for Union<'_, T, N, S> {}
pub struct Difference<'a, T, const N: usize, S> {
iter: Iter<'a, T>,
other: &'a SmallSet<T, N, S>,
}
impl<'a, T, const N: usize, S> Iterator for Difference<'a, T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.by_ref().find(|v| !self.other.contains(v))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.iter.size_hint().1)
}
}
impl<T: Eq + Hash, const N: usize, S: BuildHasher> FusedIterator
for Difference<'_, T, N, S>
{
}
pub struct SymmetricDifference<'a, T, const N: usize, S> {
iter_a: Difference<'a, T, N, S>,
iter_b: Difference<'a, T, N, S>,
}
impl<'a, T, const N: usize, S> Iterator for SymmetricDifference<'a, T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter_a.next().or_else(|| self.iter_b.next())
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (a_lo, a_hi) = self.iter_a.size_hint();
let (b_lo, b_hi) = self.iter_b.size_hint();
(a_lo + b_lo, a_hi.and_then(|a| b_hi.map(|b| a + b)))
}
}
impl<T: Eq + Hash, const N: usize, S: BuildHasher> FusedIterator
for SymmetricDifference<'_, T, N, S>
{
}
impl<T, const N: usize, S> IntoIterator for SmallSet<T, N, S> {
type Item = T;
type IntoIter = IntoIter<T, N>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
}
impl<'a, T, const N: usize, S> IntoIterator for &'a SmallSet<T, N, S> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: Clone, const N: usize, S: Clone> Clone for SmallSet<T, N, S> {
#[inline]
fn clone(&self) -> Self {
SmallSet(self.0.clone())
}
}
impl<T: fmt::Debug, const N: usize, S> fmt::Debug for SmallSet<T, N, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<T, const N: usize, S> PartialEq for SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
self.iter().all(|v| other.contains(v))
}
}
impl<T, const N: usize, S> Eq for SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher,
{
}
impl<T, const N: usize, S: Default> Default for SmallSet<T, N, S> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T, const N: usize, S> Extend<T> for SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for v in iter {
self.insert(v);
}
}
}
impl<'a, T, const N: usize, S> Extend<&'a T> for SmallSet<T, N, S>
where
T: Eq + Hash + Copy,
S: BuildHasher + Default,
{
#[inline]
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
for &v in iter {
self.insert(v);
}
}
}
impl<T, const N: usize, S> FromIterator<T> for SmallSet<T, N, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut set = Self::new();
set.extend(iter);
set
}
}
impl<T, const N: usize, S> core::ops::BitAnd<&SmallSet<T, N, S>> for &SmallSet<T, N, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = SmallSet<T, N, S>;
fn bitand(self, other: &SmallSet<T, N, S>) -> SmallSet<T, N, S> {
self.intersection(other).cloned().collect()
}
}
impl<T, const N: usize, S> core::ops::BitOr<&SmallSet<T, N, S>> for &SmallSet<T, N, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = SmallSet<T, N, S>;
fn bitor(self, other: &SmallSet<T, N, S>) -> SmallSet<T, N, S> {
self.union(other).cloned().collect()
}
}
impl<T, const N: usize, S> core::ops::Sub<&SmallSet<T, N, S>> for &SmallSet<T, N, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = SmallSet<T, N, S>;
fn sub(self, other: &SmallSet<T, N, S>) -> SmallSet<T, N, S> {
self.difference(other).cloned().collect()
}
}
impl<T, const N: usize, S> core::ops::BitXor<&SmallSet<T, N, S>> for &SmallSet<T, N, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
{
type Output = SmallSet<T, N, S>;
fn bitxor(self, other: &SmallSet<T, N, S>) -> SmallSet<T, N, S> {
self.symmetric_difference(other).cloned().collect()
}
}