use core::hash::{BuildHasher, Hash};
pub trait Set<T>: Clone + Default + Extend<T> {
type Iter<'a>: Iterator<Item = &'a T>
where
T: 'a,
Self: 'a;
fn insert(&mut self, value: T) -> Result<bool, T>;
fn remove(&mut self, value: &T) -> bool;
fn first(&self) -> Option<&T>;
fn contains(&mut self, value: &T) -> bool;
fn is_subset(&self, other: &Self) -> bool;
fn len(&self) -> usize;
fn clear(&mut self);
#[must_use]
fn is_empty(&self) -> bool;
fn sub(&self, other: &Self) -> Self;
#[must_use]
fn iter(&self) -> Self::Iter<'_>;
}
#[cfg(feature = "alloc")]
impl<T> Set<T> for alloc::collections::BTreeSet<T>
where
T: Clone + Ord,
{
type Iter<'a> = alloc::collections::btree_set::Iter<'a, T> where T: 'a, Self: 'a;
fn insert(&mut self, value: T) -> Result<bool, T> {
Ok(alloc::collections::BTreeSet::insert(self, value))
}
fn remove(&mut self, value: &T) -> bool {
alloc::collections::BTreeSet::remove(self, value)
}
fn first(&self) -> Option<&T> {
alloc::collections::BTreeSet::first(self)
}
fn contains(&mut self, value: &T) -> bool {
alloc::collections::BTreeSet::contains(self, value)
}
fn is_subset(&self, other: &Self) -> bool {
alloc::collections::BTreeSet::is_subset(self, other)
}
fn len(&self) -> usize {
alloc::collections::BTreeSet::len(self)
}
fn clear(&mut self) {
alloc::collections::BTreeSet::clear(self)
}
fn is_empty(&self) -> bool {
alloc::collections::BTreeSet::is_empty(self)
}
fn sub(&self, other: &Self) -> Self {
self - other
}
fn iter(&self) -> Self::Iter<'_> {
alloc::collections::BTreeSet::iter(self)
}
}
impl<T, S, const N: usize> Set<T> for heapless::IndexSet<T, S, N>
where
T: Clone + Eq + Hash,
S: BuildHasher + Clone + Default,
{
type Iter<'a> = heapless::IndexSetIter<'a, T>
where
T: 'a,
Self: 'a;
fn insert(&mut self, value: T) -> Result<bool, T> {
heapless::IndexSet::insert(self, value)
}
fn remove(&mut self, value: &T) -> bool {
heapless::IndexSet::remove(self, value)
}
fn first(&self) -> Option<&T> {
heapless::IndexSet::first(self)
}
fn contains(&mut self, value: &T) -> bool {
heapless::IndexSet::contains(self, value)
}
fn is_subset(&self, other: &Self) -> bool {
heapless::IndexSet::is_subset(self, other)
}
fn len(&self) -> usize {
heapless::IndexSet::len(self)
}
fn clear(&mut self) {
heapless::IndexSet::clear(self)
}
fn is_empty(&self) -> bool {
heapless::IndexSet::is_empty(self)
}
fn sub(&self, other: &Self) -> Self {
let mut result = heapless::IndexSet::default();
result.extend(self.difference(other).cloned());
result
}
fn iter(&self) -> Self::Iter<'_> {
heapless::IndexSet::iter(self)
}
}