Struct Set

Source
pub struct Set<T, C = Natural<T>>
where C: Compare<T>,
{ /* private fields */ }
Expand description

An ordered set based on a binary search tree.

The behavior of this set is undefined if an item’s ordering relative to any other item changes while the item is in the set. This is normally only possible through Cell, RefCell, or unsafe code.

Implementations§

Source§

impl<T> Set<T>
where T: Ord,

Source

pub fn new() -> Self

Creates an empty set ordered according to the natural order of its items.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

let mut it = set.iter();
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), None);
Source§

impl<T, C> Set<T, C>
where C: Compare<T>,

Source

pub fn with_cmp(cmp: C) -> Self

Creates an empty set ordered according to the given comparator.

§Examples
use compare::{Compare, natural};

let mut set = tree::Set::with_cmp(natural().rev());

set.insert(2);
set.insert(1);
set.insert(3);

let mut it = set.iter();
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), None);
Source

pub fn is_empty(&self) -> bool

Checks if the set is empty.

§Examples
let mut set = tree::Set::new();
assert!(set.is_empty());

set.insert(2);
assert!(!set.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of items in the set.

§Examples
let mut set = tree::Set::new();
assert_eq!(set.len(), 0);

set.insert(2);
assert_eq!(set.len(), 1);
Source

pub fn cmp(&self) -> &C

Returns a reference to the set’s comparator.

§Examples
use compare::{Compare, natural};

let set = tree::Set::new();
assert!(set.cmp().compares_lt(&1, &2));

let set: tree::Set<_, _> = tree::Set::with_cmp(natural().rev());
assert!(set.cmp().compares_gt(&1, &2));
Source

pub fn clear(&mut self)

Removes all items from the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.len(), 3);
assert_eq!(set.iter().next(), Some(&1));

set.clear();

assert_eq!(set.len(), 0);
assert_eq!(set.iter().next(), None);
Source

pub fn insert(&mut self, item: T) -> bool

Inserts an item into the set, returning true if the set did not already contain the item.

§Examples
let mut set = tree::Set::new();
assert!(!set.contains(&1));
assert!(set.insert(1));
assert!(set.contains(&1));
assert!(!set.insert(1));
Source

pub fn remove<Q: ?Sized>(&mut self, item: &Q) -> bool
where C: Compare<Q, T>,

Removes the given item from the set, returning true if the set contained the item.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.len(), 3);
assert!(set.contains(&1));
assert!(set.remove(&1));

assert_eq!(set.len(), 2);
assert!(!set.contains(&1));
assert!(!set.remove(&1));
Source

pub fn entry(&mut self, item: T) -> Entry<'_, T>

Returns the set’s entry corresponding to the given item.

§Examples
use tree::set::Entry;

let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

match set.entry(1) {
    Entry::Occupied(e) => {
        assert_eq!(*e.get(), 1);
        assert_eq!(e.remove(), 1);
    }
    Entry::Vacant(_) => panic!("expected an occupied entry"),
}

assert!(!set.contains(&1));

match set.entry(4) {
    Entry::Occupied(_) => panic!("expected a vacant entry"),
    Entry::Vacant(e) => e.insert(),
}

assert!(set.contains(&4));
Source

pub fn contains<Q: ?Sized>(&self, item: &Q) -> bool
where C: Compare<Q, T>,

Checks if the set contains the given item.

§Examples
let mut set = tree::Set::new();
assert!(!set.contains(&1));
set.insert(1);
assert!(set.contains(&1));
Source

pub fn max(&self) -> Option<&T>

Returns a reference to the set’s maximum item, or None if the set is empty.

§Examples
let mut set = tree::Set::new();
assert_eq!(set.max(), None);

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.max(), Some(&3));
Source

pub fn remove_max(&mut self) -> Option<T>

Removes and returns the set’s maximum item, or None if the set is empty.

§Examples
let mut set = tree::Set::new();
assert_eq!(set.remove_max(), None);

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.remove_max(), Some(3));
Source

pub fn max_entry(&mut self) -> Option<OccupiedEntry<'_, T>>

Returns the entry corresponding to the set’s maximum item.

§Examples
let mut set = tree::Set::new();
assert!(set.max_entry().is_none());

set.insert(2);
set.insert(1);
set.insert(3);

{
    let mut e = set.max_entry().unwrap();
    assert_eq!(*e.get(), 3);
    assert_eq!(e.remove(), 3);
}

assert!(!set.contains(&3));
Source

pub fn min(&self) -> Option<&T>

Returns a reference to the set’s minimum item, or None if the set is empty.

§Examples
let mut set = tree::Set::new();
assert_eq!(set.min(), None);

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.min(), Some(&1));
Source

pub fn remove_min(&mut self) -> Option<T>

Removes and returns the set’s minimum item, or None if the set is empty.

§Examples
let mut set = tree::Set::new();
assert_eq!(set.remove_min(), None);

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.remove_min(), Some(1));
Source

pub fn min_entry(&mut self) -> Option<OccupiedEntry<'_, T>>

Returns the entry corresponding to the set’s minimum item.

§Examples
let mut set = tree::Set::new();
assert!(set.min_entry().is_none());

set.insert(2);
set.insert(1);
set.insert(3);

{
    let mut e = set.min_entry().unwrap();
    assert_eq!(*e.get(), 1);
    assert_eq!(e.remove(), 1);
}

assert!(!set.contains(&1));
Source

pub fn pred<Q: ?Sized>(&self, item: &Q, inclusive: bool) -> Option<&T>
where C: Compare<Q, T>,

Returns a reference to the predecessor of the given item, or None if no such item is present in the set.

If inclusive is false, this method finds the greatest item that is strictly less than the given item. If inclusive is true, this method finds the greatest item that is less than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.pred(&0, false), None);
assert_eq!(set.pred(&1, false), None);
assert_eq!(set.pred(&2, false), Some(&1));
assert_eq!(set.pred(&3, false), Some(&2));
assert_eq!(set.pred(&4, false), Some(&3));

assert_eq!(set.pred(&0, true), None);
assert_eq!(set.pred(&1, true), Some(&1));
assert_eq!(set.pred(&2, true), Some(&2));
assert_eq!(set.pred(&3, true), Some(&3));
assert_eq!(set.pred(&4, true), Some(&3));
Source

pub fn remove_pred<Q: ?Sized>(&mut self, item: &Q, inclusive: bool) -> Option<T>
where C: Compare<Q, T>,

Removes the predecessor of the given item from the set and returns it, or None if no such item present in the set.

If inclusive is false, this method removes the greatest item that is strictly less than the given item. If inclusive is true, this method removes the greatest item that is less than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.remove_pred(&1, false), None);
assert!(set.contains(&1));

assert_eq!(set.remove_pred(&2, false), Some(1));
assert!(!set.contains(&1));

assert_eq!(set.remove_pred(&2, true), Some(2));
assert!(!set.contains(&2));
Source

pub fn pred_entry<Q: ?Sized>( &mut self, item: &Q, inclusive: bool, ) -> Option<OccupiedEntry<'_, T>>
where C: Compare<Q, T>,

Returns the entry corresponding to the predecessor of the given item.

If inclusive is false, this method returns the entry corresponding to the greatest item that is strictly less than the given item. If inclusive is true, this method returns the entry corresponding to the greatest item that is less than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert!(set.pred_entry(&1, false).is_none());

{
    let mut e = set.pred_entry(&4, true).unwrap();
    assert_eq!(*e.get(), 3);
}

{
    let e = set.pred_entry(&3, false).unwrap();
    assert_eq!(e.remove(), 2);
}

assert!(!set.contains(&2));
Source

pub fn succ<Q: ?Sized>(&self, item: &Q, inclusive: bool) -> Option<&T>
where C: Compare<Q, T>,

Returns a reference to the successor of the given item, or None if no such item is present in the set.

If inclusive is false, this method finds the smallest item that is strictly greater than the given item. If inclusive is true, this method finds the smallest item that is greater than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.succ(&0, false), Some(&1));
assert_eq!(set.succ(&1, false), Some(&2));
assert_eq!(set.succ(&2, false), Some(&3));
assert_eq!(set.succ(&3, false), None);
assert_eq!(set.succ(&4, false), None);

assert_eq!(set.succ(&0, true), Some(&1));
assert_eq!(set.succ(&1, true), Some(&1));
assert_eq!(set.succ(&2, true), Some(&2));
assert_eq!(set.succ(&3, true), Some(&3));
assert_eq!(set.succ(&4, true), None);
Source

pub fn remove_succ<Q: ?Sized>(&mut self, item: &Q, inclusive: bool) -> Option<T>
where C: Compare<Q, T>,

Removes the successor of the given item from the set and returns it, or None if no such item present in the set.

If inclusive is false, this method removes the smallest item that is strictly greater than the given item. If inclusive is true, this method removes the smallest item that is greater than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert_eq!(set.remove_succ(&3, false), None);
assert!(set.contains(&3));

assert_eq!(set.remove_succ(&2, false), Some(3));
assert!(!set.contains(&3));

assert_eq!(set.remove_succ(&2, true), Some(2));
assert!(!set.contains(&2));
Source

pub fn succ_entry<Q: ?Sized>( &mut self, item: &Q, inclusive: bool, ) -> Option<OccupiedEntry<'_, T>>
where C: Compare<Q, T>,

Returns the entry corresponding to the successor of the given item.

If inclusive is false, this method returns the entry corresponding to the smallest item that is strictly greater than the given item. If inclusive is true, this method returns the entry corresponding to the smallest item that is greater than or equal to the given item.

The given item need not itself be present in the set.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

assert!(set.succ_entry(&3, false).is_none());

{
    let mut e = set.succ_entry(&0, true).unwrap();
    assert_eq!(*e.get(), 1);
}

{
    let e = set.succ_entry(&1, false).unwrap();
    assert_eq!(e.remove(), 2);
}

assert!(!set.contains(&2));
Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the set.

The iterator yields the items in ascending order according to the set’s comparator.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

let mut it = set.iter();
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), None);

Trait Implementations§

Source§

impl<T, C> Arbitrary for Set<T, C>
where T: Arbitrary, C: 'static + Clone + Compare<T> + Default + Send,

Source§

fn arbitrary<G: Gen>(gen: &mut G) -> Self

Source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

Source§

impl<T: Clone, C> Clone for Set<T, C>
where C: Compare<T> + Clone,

Source§

fn clone(&self) -> Set<T, C>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, C> Debug for Set<T, C>
where T: Debug, C: Compare<T>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, C> Default for Set<T, C>
where C: Compare<T> + Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, C> Extend<T> for Set<T, C>
where C: Compare<T>,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, C> FromIterator<T> for Set<T, C>
where C: Compare<T> + Default,

Source§

fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T, C> Hash for Set<T, C>
where T: Hash, C: Compare<T>,

Source§

fn hash<H: Hasher>(&self, h: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T, C> IntoIterator for &'a Set<T, C>
where C: Compare<T>,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T, C> IntoIterator for Set<T, C>
where C: Compare<T>,

Source§

fn into_iter(self) -> IntoIter<T>

Returns an iterator that consumes the set.

The iterator yields the items in ascending order according to the set’s comparator.

§Examples
let mut set = tree::Set::new();

set.insert(2);
set.insert(1);
set.insert(3);

let mut it = set.into_iter();
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(2));
assert_eq!(it.next(), Some(3));
assert_eq!(it.next(), None);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<T, C> Ord for Set<T, C>
where C: Compare<T>,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T, C> PartialEq for Set<T, C>
where C: Compare<T>,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, C> PartialOrd for Set<T, C>
where C: Compare<T>,

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, C> Eq for Set<T, C>
where C: Compare<T>,

Auto Trait Implementations§

§

impl<T, C> Freeze for Set<T, C>
where C: Freeze,

§

impl<T, C> RefUnwindSafe for Set<T, C>

§

impl<T, C> Send for Set<T, C>
where C: Send, T: Send,

§

impl<T, C> Sync for Set<T, C>
where C: Sync, T: Sync,

§

impl<T, C> Unpin for Set<T, C>
where C: Unpin,

§

impl<T, C> UnwindSafe for Set<T, C>
where C: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.