use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections;
use std::fmt::{Debug, Error, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::{FromIterator, IntoIterator, Sum};
use std::ops::{Add, Deref, Mul};
use hashset::HashSet;
use nodes::btree::{
BTreeValue, ConsumingIter as ConsumingNodeIter, DiffItem as NodeDiffItem,
DiffIter as NodeDiffIter, Insert, Iter as NodeIter, Node, Remove,
};
pub type DiffItem<'a, A> = NodeDiffItem<'a, A>;
#[macro_export]
macro_rules! ordset {
() => { $crate::ordset::OrdSet::new() };
( $($x:expr),* ) => {{
let mut l = $crate::ordset::OrdSet::new();
$(
l.insert($x);
)*
l
}};
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Value<A>(A);
impl<A> Deref for Value<A> {
type Target = A;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<A: Ord + Clone> BTreeValue for Value<A> {
type Key = A;
fn ptr_eq(&self, _other: &Self) -> bool {
false
}
fn search_key<BK>(slice: &[Self], key: &BK) -> Result<usize, usize>
where
BK: Ord + ?Sized,
Self::Key: Borrow<BK>,
{
slice.binary_search_by(|value| Self::Key::borrow(value).cmp(key))
}
fn search_value(slice: &[Self], key: &Self) -> Result<usize, usize> {
slice.binary_search_by(|value| value.cmp(key))
}
fn cmp_keys(&self, other: &Self) -> Ordering {
self.cmp(other)
}
}
pub struct OrdSet<A> {
root: Node<Value<A>>,
}
impl<A> OrdSet<A> {
pub fn new() -> Self {
OrdSet { root: Node::new() }
}
pub fn singleton(a: A) -> Self {
OrdSet {
root: Node::singleton(Value(a)),
}
}
pub fn is_empty(&self) -> bool {
self.root.len() == 0
}
pub fn len(&self) -> usize {
self.root.len()
}
pub fn get_min(&self) -> Option<&A> {
self.root.min().map(Deref::deref)
}
pub fn get_max(&self) -> Option<&A> {
self.root.max().map(Deref::deref)
}
}
impl<A: Ord + Clone> OrdSet<A> {
pub fn iter(&self) -> Iter<A> {
Iter {
it: NodeIter::new(&self.root),
}
}
pub fn diff<'a>(&'a self, other: &'a Self) -> DiffIter<A> {
DiffIter {
it: NodeDiffIter::new(&self.root, &other.root),
}
}
pub fn contains<BA>(&self, a: &BA) -> bool
where
BA: Ord + ?Sized,
A: Borrow<BA>,
{
self.root.lookup(a).is_some()
}
#[inline]
pub fn insert(&mut self, a: A) -> Option<A> {
match self.root.insert(Value(a)) {
Insert::Replaced(Value(old_value)) => return Some(old_value),
Insert::Added => (),
Insert::Update(root) => self.root = root,
Insert::Split(left, median, right) => self.root = Node::from_split(left, median, right),
}
None
}
#[inline]
pub fn remove<BA>(&mut self, a: &BA) -> Option<A>
where
BA: Ord + ?Sized,
A: Borrow<BA>,
{
match self.root.remove(a) {
Remove::Update(value, root) => {
self.root = root;
Some(value.0)
}
Remove::Removed(value) => Some(value.0),
Remove::NoChange => None,
}
}
pub fn remove_min(&mut self) -> Option<A> {
let key = match self.get_min() {
None => return None,
Some(v) => v,
}.clone();
self.remove(&key)
}
pub fn remove_max(&mut self) -> Option<A> {
let key = match self.get_max() {
None => return None,
Some(v) => v,
}.clone();
self.remove(&key)
}
pub fn update(&self, a: A) -> Self {
let mut out = self.clone();
out.insert(a);
out
}
pub fn without<BA>(&self, a: &BA) -> Self
where
BA: Ord + ?Sized,
A: Borrow<BA>,
{
let mut out = self.clone();
out.remove(a);
out
}
pub fn without_min(&self) -> (Option<A>, Self) {
match self.get_min() {
Some(v) => (Some(v.clone()), self.without(&v)),
None => (None, self.clone()),
}
}
pub fn without_max(&self) -> (Option<A>, Self) {
match self.get_max() {
Some(v) => (Some(v.clone()), self.without(&v)),
None => (None, self.clone()),
}
}
pub fn union(mut self, other: Self) -> Self {
for value in other {
self.insert(value);
}
self
}
pub fn unions<I>(i: I) -> Self
where
I: IntoIterator<Item = Self>,
{
i.into_iter().fold(Self::default(), |a, b| a.union(b))
}
pub fn difference(mut self, other: Self) -> Self {
for value in other {
if self.remove(&value).is_none() {
self.insert(value);
}
}
self
}
pub fn intersection(self, other: Self) -> Self {
let mut out = Self::default();
for value in other {
if self.contains(&value) {
out.insert(value);
}
}
out
}
pub fn is_subset<RS>(&self, other: RS) -> bool
where
RS: Borrow<Self>,
{
let o = other.borrow();
self.iter().all(|a| o.contains(&a))
}
pub fn is_proper_subset<RS>(&self, other: RS) -> bool
where
RS: Borrow<Self>,
{
self.len() != other.borrow().len() && self.is_subset(other)
}
pub fn split<BA>(self, split: &BA) -> (Self, Self)
where
BA: Ord + ?Sized,
A: Borrow<BA>,
{
let (left, _, right) = self.split_member(split);
(left, right)
}
pub fn split_member<BA>(self, split: &BA) -> (Self, bool, Self)
where
BA: Ord + ?Sized,
A: Borrow<BA>,
{
let mut left = Self::default();
let mut right = Self::default();
let mut present = false;
for value in self {
match value.borrow().cmp(split) {
Ordering::Less => {
left.insert(value);
}
Ordering::Equal => {
present = true;
}
Ordering::Greater => {
right.insert(value);
}
}
}
(left, present, right)
}
pub fn take(&self, n: usize) -> Self {
self.iter().take(n).cloned().collect()
}
pub fn skip(&self, n: usize) -> Self {
self.iter().skip(n).cloned().collect()
}
}
impl<A> Clone for OrdSet<A> {
fn clone(&self) -> Self {
OrdSet {
root: self.root.clone(),
}
}
}
impl<A: Ord + Clone> PartialEq for OrdSet<A> {
fn eq(&self, other: &Self) -> bool {
self.root.ptr_eq(&other.root)
|| (self.len() == other.len() && self.diff(other).next().is_none())
}
}
impl<A: Ord + Eq + Clone> Eq for OrdSet<A> {}
impl<A: Ord + Clone> PartialOrd for OrdSet<A> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.iter().partial_cmp(other.iter())
}
}
impl<A: Ord + Clone> Ord for OrdSet<A> {
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl<A: Ord + Clone + Hash> Hash for OrdSet<A> {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
for i in self.iter() {
i.hash(state);
}
}
}
impl<A> Default for OrdSet<A> {
fn default() -> Self {
OrdSet::new()
}
}
impl<A: Ord + Clone> Add for OrdSet<A> {
type Output = OrdSet<A>;
fn add(self, other: Self) -> Self::Output {
self.union(other)
}
}
impl<'a, A: Ord + Clone> Add for &'a OrdSet<A> {
type Output = OrdSet<A>;
fn add(self, other: Self) -> Self::Output {
self.clone().union(other.clone())
}
}
impl<A: Ord + Clone> Mul for OrdSet<A> {
type Output = OrdSet<A>;
fn mul(self, other: Self) -> Self::Output {
self.intersection(other)
}
}
impl<'a, A: Ord + Clone> Mul for &'a OrdSet<A> {
type Output = OrdSet<A>;
fn mul(self, other: Self) -> Self::Output {
self.clone().intersection(other.clone())
}
}
impl<A: Ord + Clone> Sum for OrdSet<A> {
fn sum<I>(it: I) -> Self
where
I: Iterator<Item = Self>,
{
it.fold(Self::new(), |a, b| a + b)
}
}
impl<A, R> Extend<R> for OrdSet<A>
where
A: Ord + Clone + From<R>,
{
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = R>,
{
for value in iter {
self.insert(From::from(value));
}
}
}
impl<A: Ord + Clone + Debug> Debug for OrdSet<A> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
f.debug_set().entries(self.iter()).finish()
}
}
pub struct Iter<'a, A>
where
A: 'a,
{
it: NodeIter<'a, Value<A>>,
}
impl<'a, A> Iterator for Iter<'a, A>
where
A: 'a + Ord + Clone,
{
type Item = &'a A;
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(Deref::deref)
}
}
pub struct ConsumingIter<A> {
it: ConsumingNodeIter<Value<A>>,
}
impl<A> Iterator for ConsumingIter<A>
where
A: Ord + Clone,
{
type Item = A;
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(|v| v.0)
}
}
pub struct DiffIter<'a, A: 'a> {
it: NodeDiffIter<'a, Value<A>>,
}
impl<'a, A> Iterator for DiffIter<'a, A>
where
A: 'a + Ord + Clone + PartialEq,
{
type Item = DiffItem<'a, A>;
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(|item| match item {
NodeDiffItem::Add(v) => NodeDiffItem::Add(v.deref()),
NodeDiffItem::Update { old, new } => NodeDiffItem::Update {
old: old.deref(),
new: new.deref(),
},
NodeDiffItem::Remove(v) => NodeDiffItem::Remove(v.deref()),
})
}
}
impl<A, R> FromIterator<R> for OrdSet<A>
where
A: Ord + Clone + From<R>,
{
fn from_iter<T>(i: T) -> Self
where
T: IntoIterator<Item = R>,
{
let mut out = Self::new();
for item in i {
out.insert(From::from(item));
}
out
}
}
impl<'a, A> IntoIterator for &'a OrdSet<A>
where
A: 'a + Ord + Clone,
{
type Item = &'a A;
type IntoIter = Iter<'a, A>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<A> IntoIterator for OrdSet<A>
where
A: Ord + Clone,
{
type Item = A;
type IntoIter = ConsumingIter<A>;
fn into_iter(self) -> Self::IntoIter {
ConsumingIter {
it: ConsumingNodeIter::new(&self.root),
}
}
}
impl<'s, 'a, A, OA> From<&'s OrdSet<&'a A>> for OrdSet<OA>
where
A: ToOwned<Owned = OA> + Ord + ?Sized,
OA: Borrow<A> + Ord + Clone,
{
fn from(set: &OrdSet<&A>) -> Self {
set.iter().map(|a| (*a).to_owned()).collect()
}
}
impl<'a, A> From<&'a [A]> for OrdSet<A>
where
A: Ord + Clone,
{
fn from(slice: &'a [A]) -> Self {
slice.iter().cloned().collect()
}
}
impl<A: Ord + Clone> From<Vec<A>> for OrdSet<A> {
fn from(vec: Vec<A>) -> Self {
vec.into_iter().collect()
}
}
impl<'a, A: Ord + Clone> From<&'a Vec<A>> for OrdSet<A> {
fn from(vec: &Vec<A>) -> Self {
vec.into_iter().cloned().collect()
}
}
impl<A: Eq + Hash + Ord + Clone> From<collections::HashSet<A>> for OrdSet<A> {
fn from(hash_set: collections::HashSet<A>) -> Self {
hash_set.into_iter().collect()
}
}
impl<'a, A: Eq + Hash + Ord + Clone> From<&'a collections::HashSet<A>> for OrdSet<A> {
fn from(hash_set: &collections::HashSet<A>) -> Self {
hash_set.into_iter().cloned().collect()
}
}
impl<A: Ord + Clone> From<collections::BTreeSet<A>> for OrdSet<A> {
fn from(btree_set: collections::BTreeSet<A>) -> Self {
btree_set.into_iter().collect()
}
}
impl<'a, A: Ord + Clone> From<&'a collections::BTreeSet<A>> for OrdSet<A> {
fn from(btree_set: &collections::BTreeSet<A>) -> Self {
btree_set.into_iter().cloned().collect()
}
}
impl<A: Hash + Eq + Ord + Clone, S: BuildHasher> From<HashSet<A, S>> for OrdSet<A> {
fn from(hashset: HashSet<A, S>) -> Self {
hashset.into_iter().collect()
}
}
impl<'a, A: Hash + Eq + Ord + Clone, S: BuildHasher> From<&'a HashSet<A, S>> for OrdSet<A> {
fn from(hashset: &HashSet<A, S>) -> Self {
hashset.into_iter().cloned().collect()
}
}
#[cfg(all(feature = "arc", any(test, feature = "quickcheck")))]
use quickcheck::{Arbitrary, Gen};
#[cfg(all(feature = "arc", any(test, feature = "quickcheck")))]
impl<A: Ord + Clone + Arbitrary + Sync> Arbitrary for OrdSet<A> {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
OrdSet::from_iter(Vec::<A>::arbitrary(g))
}
}
#[cfg(any(test, feature = "proptest"))]
pub mod proptest {
use super::*;
use proptest::strategy::{BoxedStrategy, Strategy, ValueTree};
use std::ops::Range;
pub fn ord_set<A: Strategy + 'static>(
element: A,
size: Range<usize>,
) -> BoxedStrategy<OrdSet<<A::Tree as ValueTree>::Value>>
where
<A::Tree as ValueTree>::Value: Ord + Clone,
{
::proptest::collection::vec(element, size.clone())
.prop_map(OrdSet::from)
.prop_filter("OrdSet minimum size".to_owned(), move |s| {
s.len() >= size.start
})
.boxed()
}
}
#[cfg(test)]
mod test {
use super::proptest::*;
use super::*;
#[test]
fn match_strings_with_string_slices() {
let mut set: OrdSet<String> = From::from(&ordset!["foo", "bar"]);
set = set.without("bar");
assert!(!set.contains("bar"));
set.remove("foo");
assert!(!set.contains("foo"));
}
proptest! {
#[test]
fn proptest_a_set(ref s in ord_set(".*", 10..100)) {
assert!(s.len() < 100);
assert!(s.len() >= 10);
}
}
}