mod iter;
mod serde;
use alloc::collections::BTreeSet;
use core::cmp::Ordering;
use core::fmt;
use core::ops::Bound;
use crate::value::Value;
#[allow(unused_imports)] pub use iter::{IntoIter, Iter};
#[derive(Default, Clone, Eq, PartialEq)]
pub struct Set {
inner: BTreeSet<Value>,
}
impl Set {
#[inline]
pub const fn new() -> Self {
Self {
inner: BTreeSet::new(),
}
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn contains(&self, value: &Value) -> bool {
self.inner.contains(value)
}
#[inline]
pub fn get(&self, value: &Value) -> Option<&Value> {
self.inner.get(value)
}
#[inline]
pub fn first(&self) -> Option<&Value> {
self.iter_sorted().next()
}
#[inline]
pub fn last(&self) -> Option<&Value> {
self.iter_sorted().next_back()
}
#[inline]
pub fn iter(&self) -> Iter<'_> {
Iter {
inner: self.inner.iter(),
}
}
#[inline]
pub fn iter_sorted(&self) -> Iter<'_> {
Iter {
inner: self.inner.iter(),
}
}
#[inline]
pub fn insert(&mut self, value: Value) -> bool {
self.inner.insert(value)
}
#[inline]
pub fn remove(&mut self, value: &Value) -> bool {
self.inner.remove(value)
}
#[inline]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&Value) -> bool,
{
self.inner.retain(f);
}
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
#[inline]
pub fn append(&mut self, other: &mut Set) {
self.inner.append(&mut other.inner);
}
pub fn intersection(&self, other: &Set) -> Set {
Set {
inner: self.inner.intersection(&other.inner).cloned().collect(),
}
}
pub fn union(&self, other: &Set) -> Set {
Set {
inner: self.inner.union(&other.inner).cloned().collect(),
}
}
pub fn difference(&self, other: &Set) -> Set {
Set {
inner: self.inner.difference(&other.inner).cloned().collect(),
}
}
#[inline]
pub fn is_subset(&self, other: &Set) -> bool {
self.inner.is_subset(&other.inner)
}
#[inline]
pub fn into_value(self) -> Value {
Value::Set(crate::Rc::new(self))
}
#[inline]
pub const fn cursor(&self) -> SetCursor {
SetCursor {
inner: SetCursorInner::BTree(None),
}
}
pub fn next<'a>(&'a self, cursor: &mut SetCursor) -> Option<&'a Value> {
let SetCursorInner::BTree(ref mut last) = cursor.inner;
let next = last.as_ref().map_or_else(
|| self.inner.iter().next(),
|prev| {
self.inner
.range((Bound::Excluded(prev), Bound::Unbounded))
.next()
},
);
let v = next?;
*last = Some(v.clone());
Some(v)
}
}
#[derive(Debug, Clone)]
pub struct SetCursor {
inner: SetCursorInner,
}
#[derive(Debug, Clone)]
enum SetCursorInner {
BTree(Option<Value>),
}
impl Ord for Set {
fn cmp(&self, other: &Self) -> Ordering {
self.iter_sorted().cmp(other.iter_sorted())
}
}
impl PartialOrd for Set {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Debug for Set {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter_sorted()).finish()
}
}
impl Extend<Value> for Set {
fn extend<I: IntoIterator<Item = Value>>(&mut self, iter: I) {
self.inner.extend(iter);
}
}
impl FromIterator<Value> for Set {
fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
Self {
inner: BTreeSet::from_iter(iter),
}
}
}
impl From<BTreeSet<Value>> for Set {
#[inline]
fn from(set: BTreeSet<Value>) -> Self {
Self { inner: set }
}
}
impl From<Set> for Value {
#[inline]
fn from(s: Set) -> Self {
s.into_value()
}
}