Struct rb_tree::RBTree[][src]

pub struct RBTree<T: PartialOrd> { /* fields omitted */ }
Expand description

A red black tree that can be used to store elements sorted by their PartialOrd provided ordering.

Implementations

impl<T: PartialOrd> RBTree<T>[src]

pub fn new() -> RBTree<T>[src]

Creates and returns a new RBTree.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(3);
t.insert(2);
assert_eq!(t.take(&2).unwrap(), 2);

pub fn into_queue<P>(self, comp: P) -> RBQueue<T, P> where
    P: Copy + Fn(&T, &T) -> Ordering
[src]

Turns this tree into a queue with the given the comparison method.

Example:

use rb_tree::{RBTree, RBQueue};
use std::cmp::Ordering::{Equal, Less, Greater};

let mut t = RBTree::new();
t.insert(3);
t.insert(2);
t.insert(1);

// reverse order queue
let mut q = t.into_queue(|l, r| {
    match l - r {
        i32::MIN..=-1_i32 => Greater,
        0 => Equal,
        1_i32..=i32::MAX => Less
    }
});
assert_eq!(q.pop().unwrap(), 3);
assert_eq!(q.pop().unwrap(), 2);
assert_eq!(q.pop().unwrap(), 1);
assert_eq!(q.pop(), None);

pub fn clear(&mut self)[src]

Clears all entries from the tree.

Example:

use rb_tree::RBTree;

let mut tree = RBTree::new();
tree.insert(2);
tree.insert(5);
tree.clear();
assert_eq!(tree.len(), 0);
assert!(!tree.contains(&2));

pub fn drain(&mut self) -> Drain<T>

Notable traits for Drain<T>

impl<T: PartialOrd> Iterator for Drain<T> type Item = T;
[src]

Clears the tree and returns all values as an iterator in their PartialOrd order.

Example:

use rb_tree::RBTree;

let mut tree = RBTree::new();
tree.insert(2);
tree.insert(5);
assert_eq!(tree.len(), 2);
let mut drain = tree.drain();
assert_eq!(drain.next().unwrap(), 2);
assert_eq!(drain.next().unwrap(), 5);
assert!(drain.next().is_none());
assert_eq!(tree.len(), 0);

pub fn ordered(&self) -> Vec<&T>[src]

Returns a vector presenting the contained elements of the RBTree in the order by which they are prioritised (that is, in the in-order tree traversal order).

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(3);
t.insert(1);
t.insert(2);
let order = t.ordered();
assert_eq!(*order[1], 2);

pub fn len(&self) -> usize[src]

Returns the number of elements contained in the tree.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(3);
t.insert(1);
t.insert(2);
assert_eq!(t.len(), 3);
t.remove(&2);
assert_eq!(t.len(), 2);

pub fn is_empty(&self) -> bool[src]

Returns true if there are no items present in the tree, false otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
assert!(t.is_empty());
t.insert(3);
assert!(!t.is_empty());

pub fn insert(&mut self, val: T) -> bool[src]

Inserts a new element into the RBTree. Returns true if this item was not already in the tree, and false otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
assert_eq!(t.insert("Hello".to_string()), true);
assert_eq!(t.insert("Hello".to_string()), false);

pub fn replace(&mut self, val: T) -> Option<T>[src]

Inserts a new element into the RBTree. Returns None if this item was not already in the tree, and the previously contained item otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
assert_eq!(t.replace("Hello".to_string()), None);
assert_eq!(t.replace("Hello".to_string()), Some("Hello".to_string()));

pub fn contains(&self, val: &T) -> bool[src]

Returns true if the tree contains the specified item, false otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(2);
assert!(!t.contains(&3));
assert!(t.contains(&2));

pub fn get<K: PartialOrd<T>>(&self, val: &K) -> Option<&T>[src]

Returns the item specified if contained, None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(1);
assert_eq!(*t.get(&1).unwrap(), 1);
assert_eq!(t.get(&2), None);

pub fn take<K: PartialOrd<T>>(&mut self, val: &K) -> Option<T>[src]

Removes an item the tree. Returns the matching item if it was contained in the tree, None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(4);
t.insert(2);
assert_eq!(t.take(&2).unwrap(), 2);
assert_eq!(t.len(), 1);
assert_eq!(t.take(&2), None);

pub fn remove<K: PartialOrd<T>>(&mut self, val: &K) -> bool[src]

Removes an item the tree. Returns true if it was contained in the tree, false otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(4);
t.insert(2);
assert_eq!(t.remove(&2), true);
assert_eq!(t.len(), 1);
assert_eq!(t.remove(&2), false);

pub fn pop(&mut self) -> Option<T>[src]

Removes the item at the front of the priority queue that the RBTree represents if any elements are present, or None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(2);
t.insert(1);
t.insert(3);
assert_eq!(t.pop().unwrap(), 1);

pub fn peek(&self) -> Option<&T>[src]

Peeks the item at the front of the priority queue that the RBTree represents if any elements are present, or None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(2);
t.insert(1);
t.insert(3);
assert_eq!(*t.peek().unwrap(), 1);

pub fn pop_back(&mut self) -> Option<T>[src]

Removes the item at the back of the priority queue that the RBTree represents if any elements are present, or None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(2);
t.insert(1);
t.insert(3);
assert_eq!(t.pop_back().unwrap(), 3);

pub fn peek_back(&self) -> Option<&T>[src]

Peeks the item at the back of the priority queue that the RBTree represents if any elements are present, or None otherwise.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(2);
t.insert(1);
t.insert(3);
assert_eq!(*t.peek_back().unwrap(), 3);

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

Notable traits for Iter<'a, T>

impl<'a, T: PartialOrd> Iterator for Iter<'a, T> type Item = &'a T;
[src]

Returns an iterator over the elements contained in this RBTree.

Example:

use rb_tree::RBTree;

let mut t = RBTree::new();
t.insert(3);
t.insert(1);
t.insert(5);
assert_eq!(t.iter().collect::<Vec<&usize>>(), vec!(&1, &3, &5));

pub fn difference<'a>(&'a self, other: &'a RBTree<T>) -> Difference<'a, T>

Notable traits for Difference<'a, T>

impl<'a, T: PartialOrd> Iterator for Difference<'a, T> type Item = &'a T;
[src]

Returns an iterator representing the difference between the items in this RBTree and those in another RBTree, i.e. the values in self but not in other.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..5).for_each(|v| {t2.insert(v);});
assert_eq!(
    t1.difference(&t2).collect::<Vec<&usize>>(),
    vec!(&0, &1)
);
assert_eq!(
    t2.difference(&t1).collect::<Vec<&usize>>(),
    vec!(&3, &4)
);

pub fn symmetric_difference<'a>(
    &'a self,
    other: &'a RBTree<T>
) -> SymmetricDifference<'a, T>

Notable traits for SymmetricDifference<'a, T>

impl<'a, T: PartialOrd> Iterator for SymmetricDifference<'a, T> type Item = &'a T;
[src]

Returns an iterator representing the symmetric difference between the items in this RBTree and those in another, i.e. the values in self or other but not in both.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..5).for_each(|v| {t2.insert(v);});
assert_eq!(
    t1.symmetric_difference(&t2).collect::<Vec<&usize>>(),
    vec!(&0, &1, &3, &4)
);
assert_eq!(
    t2.symmetric_difference(&t1).collect::<Vec<&usize>>(),
    vec!(&0, &1, &3, &4)
);

pub fn intersection<'a>(&'a self, other: &'a RBTree<T>) -> Intersection<'a, T>

Notable traits for Intersection<'a, T>

impl<'a, T: PartialOrd> Iterator for Intersection<'a, T> type Item = &'a T;
[src]

Returns an iterator representing the intersection of this RBTree and another, i.e. the values that appear in both self and other.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..5).for_each(|v| {t2.insert(v);});
assert_eq!(
    t1.intersection(&t2).collect::<Vec<&usize>>(),
    vec!(&2)
);

pub fn union<'a>(&'a self, other: &'a RBTree<T>) -> Union<'a, T>

Notable traits for Union<'a, T>

impl<'a, T: PartialOrd> Iterator for Union<'a, T> type Item = &'a T;
[src]

Returns an iterator representing the union of this RBTree and another, i.e. the values that appear in at least one of the RBTrees.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..5).for_each(|v| {t2.insert(v);});
assert_eq!(
    t1.union(&t2).collect::<Vec<&usize>>(),
    vec!(&0, &1, &2, &3, &4)
);

pub fn is_disjoint(&self, other: &RBTree<T>) -> bool[src]

Returns true if this RBTree and another are disjoint, i.e. there are no values in self that appear in other and vice versa, false otherwise.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..5).for_each(|v| {t2.insert(v);});
assert!(!t1.is_disjoint(&t2));
t2.pop(); // remove '2' from t2
assert!(t1.is_disjoint(&t2));

pub fn is_subset(&self, other: &RBTree<T>) -> bool[src]

Returns true if this RBTree is a subset of another, i.e. at least all values in self also appear in other, false otherwise.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
let mut t3 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..10).for_each(|v| {t2.insert(v);});
(3..7).for_each(|v| {t3.insert(v);});
assert!(!t1.is_subset(&t2));
assert!(t3.is_subset(&t2));

pub fn is_superset(&self, other: &RBTree<T>) -> bool[src]

Returns true if this RBTree is a superset of another, i.e. at least all values in other also appear in self, false otherwise.

Example:

use rb_tree::RBTree;

let mut t1 = RBTree::new();
let mut t2 = RBTree::new();
let mut t3 = RBTree::new();
(0..3).for_each(|v| {t1.insert(v);});
(2..10).for_each(|v| {t2.insert(v);});
(3..7).for_each(|v| {t3.insert(v);});
assert!(!t2.is_superset(&t1));
assert!(t2.is_superset(&t3));

pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)[src]

Retains in this RBTree only those values for which the passed closure returns true.

Example:

use rb_tree::RBTree;

let mut t: RBTree<usize> = (0..10).collect();
t.retain(|v| v % 2 == 0);
assert_eq!(t.iter().collect::<Vec<&usize>>(), vec!(&0, &2, &4, &6, &8));

Trait Implementations

impl<T: Clone + PartialOrd> Clone for RBTree<T>[src]

fn clone(&self) -> RBTree<T>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: PartialOrd + Debug> Debug for RBTree<T>[src]

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

Formats the value using the given formatter. Read more

impl<T: PartialOrd> Default for RBTree<T>[src]

fn default() -> Self[src]

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

impl<T: PartialOrd + Debug> Display for RBTree<T>[src]

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

Formats the value using the given formatter. Read more

impl<'a, T: PartialOrd + Copy + 'a> Extend<&'a T> for RBTree<T>[src]

fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)[src]

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<T: PartialOrd> Extend<T> for RBTree<T>[src]

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)[src]

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<T, P> From<RBQueue<T, P>> for RBTree<T> where
    T: PartialOrd,
    P: Copy + Fn(&T, &T) -> Ordering
[src]

fn from(q: RBQueue<T, P>) -> Self[src]

Performs the conversion.

impl<T: PartialOrd> FromIterator<T> for RBTree<T>[src]

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self[src]

Creates a value from an iterator. Read more

impl<T: PartialOrd> IntoIterator for RBTree<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

fn into_iter(self) -> IntoIter<T>

Notable traits for IntoIter<T>

impl<T: PartialOrd> Iterator for IntoIter<T> type Item = T;
[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<T> RefUnwindSafe for RBTree<T> where
    T: RefUnwindSafe

impl<T> Send for RBTree<T> where
    T: Send

impl<T> Sync for RBTree<T> where
    T: Sync

impl<T> Unpin for RBTree<T> where
    T: Unpin

impl<T> UnwindSafe for RBTree<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.