use crate::avl::{Iter, Tree};
use std::{
borrow::Borrow,
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
default::Default,
fmt::{self, Debug, Formatter},
hash::{Hash, Hasher},
iter::FromIterator,
ops::Bound,
};
#[derive(Clone)]
pub struct Set<K: Ord + Clone>(Tree<K, ()>);
impl<K> Hash for Set<K>
where
K: Hash + Ord + Clone,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
impl<K> Default for Set<K>
where
K: Ord + Clone,
{
fn default() -> Set<K> {
Set::new()
}
}
impl<K> PartialEq for Set<K>
where
K: Ord + Clone,
{
fn eq(&self, other: &Set<K>) -> bool {
self.0 == other.0
}
}
impl<K> Eq for Set<K> where K: Eq + Ord + Clone {}
impl<K> PartialOrd for Set<K>
where
K: Ord + Clone,
{
fn partial_cmp(&self, other: &Set<K>) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<K> Ord for Set<K>
where
K: Ord + Clone,
{
fn cmp(&self, other: &Set<K>) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<K> Debug for Set<K>
where
K: Debug + Ord + Clone,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_set().entries(self.into_iter()).finish()
}
}
impl<K> FromIterator<K> for Set<K>
where
K: Ord + Clone,
{
fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self {
Set::new().insert_many(iter)
}
}
pub struct SetIter<'a, Q: Ord, K: 'a + Clone + Ord + Borrow<Q>>(Iter<'a, Q, K, ()>);
impl<'a, Q, K> Iterator for SetIter<'a, Q, K>
where
Q: Ord,
K: 'a + Clone + Ord + Borrow<Q>,
{
type Item = &'a K;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, ())| k)
}
}
impl<'a, Q, K> DoubleEndedIterator for SetIter<'a, Q, K>
where
Q: Ord,
K: 'a + Clone + Ord + Borrow<Q>,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|(k, ())| k)
}
}
impl<'a, K> IntoIterator for &'a Set<K>
where
K: 'a + Borrow<K> + Ord + Clone,
{
type Item = &'a K;
type IntoIter = SetIter<'a, K, K>;
fn into_iter(self) -> Self::IntoIter {
SetIter(self.0.into_iter())
}
}
impl<K> Set<K>
where
K: Ord + Clone,
{
pub fn new() -> Self {
Set(Tree::new())
}
pub fn insert_many<E: IntoIterator<Item = K>>(&self, elts: E) -> Self {
let root = self.0.insert_many(elts.into_iter().map(|k| (k, ())));
Set(root)
}
pub fn remove_many<Q, E>(&self, elts: E) -> Self
where
Q: Ord,
K: Borrow<Q>,
E: IntoIterator<Item = Q>,
{
let root = self
.0
.update_many(elts.into_iter().map(|k| (k, ())), &mut |_, _, _| None);
Set(root)
}
pub fn update_many<Q, E, F>(&self, elts: E, mut f: F) -> Self
where
Q: Ord,
K: Borrow<Q>,
E: IntoIterator<Item = Q>,
F: FnMut(Q, Option<&K>) -> Option<K>,
{
let root = self
.0
.update_many(elts.into_iter().map(|k| (k, ())), &mut |q, (), cur| {
let cur = cur.map(|(k, ())| k);
f(q, cur).map(|k| (k, ()))
});
Set(root)
}
pub fn insert(&self, k: K) -> (Self, bool) {
if self.contains(&k) {
(self.clone(), true)
} else {
(Set(self.0.insert(k, ()).0), false)
}
}
pub fn contains<'a, Q>(&'a self, k: &Q) -> bool
where
Q: ?Sized + Ord,
K: Borrow<Q>,
{
self.0.get(k).is_some()
}
pub fn get<'a, Q>(&'a self, k: &Q) -> Option<&K>
where
Q: ?Sized + Ord,
K: Borrow<Q>,
{
self.0.get_key(k)
}
pub fn remove<Q: Sized + Ord>(&self, k: &Q) -> (Self, bool)
where
K: Borrow<Q>,
{
let (t, prev) = self.0.remove(k);
(Set(t), prev.is_some())
}
pub fn union(&self, other: &Set<K>) -> Self {
Set(Tree::union(&self.0, &other.0, &mut |_, (), ()| Some(())))
}
pub fn intersect(&self, other: &Set<K>) -> Self {
Set(Tree::intersect(&self.0, &other.0, &mut |_, (), ()| Some(())))
}
pub fn diff(&self, other: &Set<K>) -> Self where K: Debug {
Set(Tree::diff(&self.0, &other.0, &mut |_, (), ()| None))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn range<'a, Q>(&'a self, lbound: Bound<Q>, ubound: Bound<Q>) -> SetIter<'a, Q, K>
where
Q: Ord,
K: 'a + Clone + Ord + Borrow<Q>,
{
SetIter(self.0.range(lbound, ubound))
}
}
impl<K> Set<K>
where
K: Ord + Clone + Debug,
{
#[allow(dead_code)]
pub(crate) fn invariant(&self) -> () {
self.0.invariant()
}
}