Struct tree::set::Set [] [src]

pub struct Set<T, C = Natural<T>> where
    C: Compare<T>, 
{ /* fields omitted */ }

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.

Methods

impl<T> Set<T> where
    T: Ord
[src]

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);

impl<T, C> Set<T, C> where
    C: Compare<T>, 
[src]

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);

Checks if the set is empty.

Examples

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

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

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);

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));

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);

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));

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));

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));

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));

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));

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));

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));

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));

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));

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));

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));

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));

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));

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);

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));

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));

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

impl<T: Clone, C: Clone> Clone for Set<T, C> where
    C: Compare<T>, 
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Formats the value using the given formatter.

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

Returns the "default value" for a type. Read more

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

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

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

Creates a value from an iterator. Read more

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

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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);

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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

This method returns an Ordering between self and other. Read more

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