use crate::iter;
use crate::tree_core;
use crate::vec_like;
use std::borrow::Borrow;
use std::cmp;
use std::iter::Peekable;
use std::ops;
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SplaySet<T> {
tree: tree_core::Tree<T, ()>,
}
impl<T> SplaySet<T>
where
T: Ord,
{
pub fn new() -> Self {
SplaySet {
tree: tree_core::Tree::new(),
}
}
pub fn clear(&mut self) {
self.tree = tree_core::Tree::new();
}
pub fn contains<Q>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.contains_key(value)
}
pub fn contains_immut<Q>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.contains_key_immut(value)
}
pub fn get<Q>(&mut self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
if self.tree.get(value).is_some() {
Some(&self.tree.root_ref().key)
} else {
None
}
}
pub fn get_immut<Q>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.get_immut(value).map(|x| x.0)
}
pub fn find_lower_bound<Q>(&mut self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.find_lower_bound(value)
}
pub fn find_lower_bound_immut<Q>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.find_lower_bound_immut(value)
}
pub fn find_upper_bound<Q>(&mut self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.find_upper_bound(value)
}
pub fn find_upper_bound_immut<Q>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.find_upper_bound_immut(value)
}
pub fn smallest(&mut self) -> Option<&T> {
self.tree.get_lftmost().map(|(v, _)| v)
}
pub fn smallest_immut(&self) -> Option<&T> {
self.tree.get_lftmost_immut().map(|(v, _)| v)
}
pub fn take_smallest(&mut self) -> Option<T> {
self.tree.take_lftmost().map(|(v, _)| v)
}
pub fn largest(&mut self) -> Option<&T> {
self.tree.get_rgtmost().map(|(v, _)| v)
}
pub fn largest_immut(&self) -> Option<&T> {
self.tree.get_rgtmost_immut().map(|(v, _)| v)
}
pub fn take_largest(&mut self) -> Option<T> {
self.tree.take_rgtmost().map(|(v, _)| v)
}
pub fn insert(&mut self, value: T) -> bool {
self.tree.insert(value, ()).is_none()
}
pub fn replace(&mut self, value: T) -> Option<T> {
let old = self.take(&value);
self.insert(value);
old
}
pub fn remove<Q>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.tree.remove(value).is_some()
}
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
if self.contains(value) {
self.tree.pop_root().map(|(e, _)| e)
} else {
None
}
}
pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T> {
Difference(self.iter().peekable(), other.iter().peekable())
}
pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T> {
SymmetricDifference(self.iter().peekable(), other.iter().peekable())
}
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> {
Intersection(self.iter().peekable(), other.iter().peekable())
}
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T> {
Union(self.iter().peekable(), other.iter().peekable())
}
pub fn is_disjoint(&self, other: &Self) -> bool {
self.intersection(other).count() == 0
}
pub fn is_subset(&self, other: &Self) -> bool {
self.difference(other).count() == 0
}
pub fn is_superset(&self, other: &Self) -> bool {
other.is_subset(self)
}
pub fn as_vec_like_mut(&mut self) -> VecLikeMut<'_, T> {
VecLikeMut::new(&mut self.tree)
}
}
impl<T> SplaySet<T> {
pub fn len(&self) -> usize {
self.tree.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> Iter<'_, T> {
Iter::new(self)
}
pub fn as_vec_like(&self) -> VecLike<'_, T> {
VecLike::new(&self.tree)
}
}
impl<T> Default for SplaySet<T>
where
T: Ord,
{
fn default() -> Self {
SplaySet::new()
}
}
impl<T> std::iter::FromIterator<T> for SplaySet<T>
where
T: Ord,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let mut set = SplaySet::new();
for x in iter {
set.insert(x);
}
set
}
}
impl<T> IntoIterator for SplaySet<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.tree.into_iter())
}
}
impl<'a, T> IntoIterator for &'a SplaySet<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
Iter::new(self)
}
}
impl<T> Extend<T> for SplaySet<T>
where
T: Ord,
{
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
for x in iter {
self.insert(x);
}
}
}
impl<'a, T> Extend<&'a T> for SplaySet<T>
where
T: Copy + 'a + Ord,
{
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = &'a T>,
{
for x in iter {
self.insert(*x);
}
}
}
impl<T> ops::Sub<&SplaySet<T>> for &SplaySet<T>
where
T: Ord + Clone,
{
type Output = SplaySet<T>;
fn sub(self, rhs: &SplaySet<T>) -> SplaySet<T> {
self.difference(rhs).cloned().collect()
}
}
impl<T> ops::BitXor<&SplaySet<T>> for &SplaySet<T>
where
T: Ord + Clone,
{
type Output = SplaySet<T>;
fn bitxor(self, rhs: &SplaySet<T>) -> SplaySet<T> {
self.symmetric_difference(rhs).cloned().collect()
}
}
impl<T> ops::BitAnd<&SplaySet<T>> for &SplaySet<T>
where
T: Ord + Clone,
{
type Output = SplaySet<T>;
fn bitand(self, rhs: &SplaySet<T>) -> SplaySet<T> {
self.intersection(rhs).cloned().collect()
}
}
impl<T> ops::BitOr<&SplaySet<T>> for &SplaySet<T>
where
T: Ord + Clone,
{
type Output = SplaySet<T>;
fn bitor(self, rhs: &SplaySet<T>) -> SplaySet<T> {
self.union(rhs).cloned().collect()
}
}
pub struct Iter<'a, T: 'a>(iter::Iter<'a, T, ()>);
impl<'a, T: 'a> Iter<'a, T> {
fn new(set: &'a SplaySet<T>) -> Self {
Iter(set.tree.iter())
}
}
impl<'a, T: 'a> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(e, _)| e)
}
}
pub struct IntoIter<T>(iter::IntoIter<T, ()>);
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(e, _)| e)
}
}
fn item_cmp<T>(a: Option<&T>, b: Option<&T>) -> Option<cmp::Ordering>
where
T: Ord,
{
match (a, b) {
(None, None) => None,
(Some(_), None) => Some(cmp::Ordering::Less),
(None, Some(_)) => Some(cmp::Ordering::Greater),
(Some(a), Some(b)) => Some(a.cmp(b)),
}
}
pub struct Difference<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Difference<'a, T>
where
T: Ord,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
loop {
match item_cmp(self.0.peek(), self.1.peek()) {
None => return None,
Some(cmp::Ordering::Less) => return self.0.next(),
Some(cmp::Ordering::Greater) => {
self.1.next();
}
Some(cmp::Ordering::Equal) => {
self.0.next();
self.1.next();
}
}
}
}
}
pub struct SymmetricDifference<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for SymmetricDifference<'a, T>
where
T: Ord,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
loop {
match item_cmp(self.0.peek(), self.1.peek()) {
None => return None,
Some(cmp::Ordering::Less) => return self.0.next(),
Some(cmp::Ordering::Greater) => return self.1.next(),
Some(cmp::Ordering::Equal) => {
self.0.next();
self.1.next();
}
}
}
}
}
pub struct Intersection<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Intersection<'a, T>
where
T: Ord,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
loop {
match item_cmp(self.0.peek(), self.1.peek()) {
None => return None,
Some(cmp::Ordering::Less) => {
self.0.next();
}
Some(cmp::Ordering::Greater) => {
self.1.next();
}
Some(cmp::Ordering::Equal) => {
self.0.next();
return self.1.next();
}
}
}
}
}
pub struct Union<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Union<'a, T>
where
T: Ord,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
match item_cmp(self.0.peek(), self.1.peek()) {
None => None,
Some(cmp::Ordering::Less) => self.0.next(),
Some(cmp::Ordering::Greater) => self.1.next(),
Some(cmp::Ordering::Equal) => {
self.0.next();
self.1.next()
}
}
}
}
#[derive(Debug, Clone)]
pub struct VecLike<'a, T: 'a> {
inner: vec_like::VecLike<'a, T, ()>,
}
impl<'a, T: 'a> VecLike<'a, T> {
fn new(tree: &'a tree_core::Tree<T, ()>) -> Self {
VecLike {
inner: vec_like::VecLike::new(tree),
}
}
pub fn get(&self, index: usize) -> Option<&'a T> {
self.inner.get(index).map(|(v, _)| v)
}
pub fn first(&self) -> Option<&'a T> {
self.inner.first().map(|(v, _)| v)
}
pub fn last(&self) -> Option<&'a T> {
self.inner.last().map(|(v, _)| v)
}
pub fn iter(&self) -> VecLikeIter<'a, T> {
VecLikeIter(self.inner.iter())
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Debug)]
pub struct VecLikeMut<'a, T: 'a> {
inner: vec_like::VecLikeMut<'a, T, ()>,
}
impl<'a, T: 'a> VecLikeMut<'a, T>
where
T: Ord,
{
pub fn push(&mut self, value: T) -> bool {
self.inner.push(value, ())
}
pub fn pop(&mut self) -> Option<T> {
self.inner.pop().map(|(v, _)| v)
}
pub fn find_index<Q>(&mut self, value: &Q) -> Option<usize>
where
T: Borrow<Q>,
Q: ?Sized + Ord,
{
self.inner.find_index(value)
}
}
impl<'a, T: 'a> VecLikeMut<'a, T> {
fn new(tree: &'a mut tree_core::Tree<T, ()>) -> Self {
VecLikeMut {
inner: vec_like::VecLikeMut::new(tree),
}
}
pub fn get(&self, index: usize) -> Option<&T> {
self.inner.get(index).map(|(v, _)| v)
}
pub fn first(&self) -> Option<&T> {
self.inner.first().map(|(v, _)| v)
}
pub fn last(&self) -> Option<&T> {
self.inner.last().map(|(v, _)| v)
}
pub fn iter(&self) -> VecLikeIter<'_, T> {
VecLikeIter(self.inner.iter())
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub struct VecLikeIter<'a, T: 'a>(vec_like::Iter<'a, T, ()>);
impl<'a, T: 'a> Iterator for VecLikeIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(v, _)| v)
}
}