Struct rb_tree::RBQueue [−][src]
Expand description
A priority queue implemented using a red black tree. The ordering supplied must satisfy the assymetry and transitivity rules as outlined by the dorumentation of std::cmp::PartialOrd.
Implementations
impl<T, P> RBQueue<T, P> where
P: Fn(&T, &T) -> Ordering,
[src]
impl<T, P> RBQueue<T, P> where
P: Fn(&T, &T) -> Ordering,
[src]pub fn new(cmp: P) -> RBQueue<T, P>
[src]
pub fn new(cmp: P) -> RBQueue<T, P>
[src]Creates and returns a new RBQueue that will order entries based on cmp.
It is a logic error to use a closure
where two non-identical items map to the
same value. If cmp
returns Equal, then
the two keys are considered the same.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<(i8, i8), _>::new(|l, r| { let l_sum = l.0 + l.1; let r_sum = r.0 + r.1; if l_sum == r_sum { if l.0 == r.0 { std::cmp::Ordering::Equal } else if l.0 < r.0 { std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater } } else if l_sum < r_sum { std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater } }); t.insert((1, 1)); t.insert((1, 2)); t.insert((1, -1)); assert_eq!(t.peek().unwrap(), &(1, -1));
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
[src]Clears all entries from the queue.
Example:
use rb_tree::RBQueue; let mut q = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); q.insert(2); q.insert(5); q.clear(); assert_eq!(q.len(), 0); assert!(!q.contains(&2));
pub fn drain(&mut self) -> Drain<T>ⓘ
[src]
pub fn drain(&mut self) -> Drain<T>ⓘ
[src]Clears the queue and returns all values as an iterator in their order.
Example:
use rb_tree::RBQueue; let mut q = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); q.insert(2); q.insert(5); assert_eq!(q.len(), 2); let mut drain = q.drain(); assert_eq!(drain.next().unwrap(), 2); assert_eq!(drain.next().unwrap(), 5); assert!(drain.next().is_none()); assert_eq!(q.len(), 0);
pub fn ordered(&self) -> Vec<&T>
[src]
pub fn ordered(&self) -> Vec<&T>
[src]Returns a vector presenting the contained elements of the RBQueue in the order by which they are prioritised (that is, in the in-order tree traversal order).
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(3); t.insert(1); t.insert(2); let order = t.ordered(); assert_eq!(*order[1], 2);
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
[src]Returns the number of elements contained in the tree.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); 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]
pub fn is_empty(&self) -> bool
[src]Returns true if there are no items present in the tree, false otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); assert!(t.is_empty()); t.insert(3); assert!(!t.is_empty());
pub fn insert(&mut self, val: T) -> bool
[src]
pub fn insert(&mut self, val: T) -> bool
[src]Inserts a new element into the RBQueue. Returns true if this item was not already in the tree, and false otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<String, _>::new(|l, r| l.partial_cmp(r).unwrap()); 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]
pub fn replace(&mut self, val: T) -> Option<T>
[src]Inserts a new element into the RBQueue. Returns None if this item was not already in the tree, and the previously contained item otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<String, _>::new(|l, r| l.partial_cmp(r).unwrap()); 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]
pub fn contains(&self, val: &T) -> bool
[src]Returns true if the tree contains the specified item, false otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(2); assert!(!t.contains(&3)); assert!(t.contains(&2));
pub fn get(&self, val: &T) -> Option<&T>
[src]
pub fn get(&self, val: &T) -> Option<&T>
[src]Returns the item specified if contained, None otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(1); assert_eq!(*t.get(&1).unwrap(), 1); assert_eq!(t.get(&2), None);
pub fn take(&mut self, val: &T) -> Option<T>
[src]
pub fn take(&mut self, val: &T) -> 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::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); 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(&mut self, val: &T) -> bool
[src]
pub fn remove(&mut self, val: &T) -> bool
[src]Removes an item the tree. Returns true if it was contained in the tree, false otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); 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]
pub fn pop(&mut self) -> Option<T>
[src]Removes the item at the front of the priority queue that the RBQueue represents if any elements are present, or None otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(2); t.insert(1); t.insert(3); assert_eq!(t.pop().unwrap(), 1);
pub fn peek(&self) -> Option<&T>
[src]
pub fn peek(&self) -> Option<&T>
[src]Peeks the item at the front of the priority queue that the RBQueue represents if any elements are present, or None otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(2); t.insert(1); t.insert(3); assert_eq!(*t.peek().unwrap(), 1);
pub fn pop_back(&mut self) -> Option<T>
[src]
pub fn pop_back(&mut self) -> Option<T>
[src]Removes the item at the back of the priority queue that the RBQueue represents if any elements are present, or None otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(2); t.insert(1); t.insert(3); assert_eq!(t.pop_back().unwrap(), 3);
pub fn peek_back(&self) -> Option<&T>
[src]
pub fn peek_back(&self) -> Option<&T>
[src]Peeks the item at the back of the priority queue that the RBQueue represents if any elements are present, or None otherwise.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(2); t.insert(1); t.insert(3); assert_eq!(*t.peek_back().unwrap(), 3);
pub fn iter(&self) -> Iter<'_, T>ⓘ
[src]
pub fn iter(&self) -> Iter<'_, T>ⓘ
[src]Returns an iterator over the elements contained in this RBQueue.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<i8, _>::new(|l, r| l.partial_cmp(r).unwrap()); t.insert(3); t.insert(1); t.insert(5); assert_eq!(t.iter().collect::<Vec<&i8>>(), vec!(&1, &3, &5));
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)
[src]
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)
[src]Retains in this RBQueue only those values for which the passed closure returns true.
Example:
use rb_tree::RBQueue; let mut t = RBQueue::<usize, _>::new(|l, r| l.partial_cmp(r).unwrap()); for i in 0usize..10usize { t.insert(i); } t.retain(|v| v % 2 == 0); assert_eq!(t.iter().collect::<Vec<&usize>>(), vec!(&0, &2, &4, &6, &8));
impl<T, P> RBQueue<T, P> where
T: PartialOrd,
P: Fn(&T, &T) -> Ordering,
[src]
impl<T, P> RBQueue<T, P> where
T: PartialOrd,
P: Fn(&T, &T) -> Ordering,
[src]pub fn into_set(self) -> RBTree<T>
[src]
pub fn into_set(self) -> RBTree<T>
[src]Turns this queue into a set (RBTree)
Example:
use rb_tree::{RBQueue, RBTree}; use std::cmp::Ordering::{Equal, Less, Greater}; let mut q = RBQueue::new(|l, r| { match l - r { i32::MIN..=-1_i32 => Greater, 0 => Equal, 1_i32..=i32::MAX => Less } }); q.insert(1); q.insert(2); q.insert(3); let mut t = q.into_set(); assert_eq!(t.pop().unwrap(), 1); assert_eq!(t.pop().unwrap(), 2); assert_eq!(t.pop().unwrap(), 3); assert_eq!(t.pop(), None);
Trait Implementations
impl<'a, T, P> Extend<&'a T> for RBQueue<T, P> where
T: Copy + 'a,
P: Fn(&T, &T) -> Ordering,
[src]
impl<'a, T, P> Extend<&'a T> for RBQueue<T, P> where
T: Copy + 'a,
P: Fn(&T, &T) -> Ordering,
[src]fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
[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]
fn extend_one(&mut self, item: A)
[src]extend_one
)Extends a collection with exactly one element.
fn extend_reserve(&mut self, additional: usize)
[src]
fn extend_reserve(&mut self, additional: usize)
[src]extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
impl<T, P> Extend<T> for RBQueue<T, P> where
P: Fn(&T, &T) -> Ordering,
[src]
impl<T, P> Extend<T> for RBQueue<T, P> where
P: Fn(&T, &T) -> Ordering,
[src]fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
[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]
fn extend_one(&mut self, item: A)
[src]extend_one
)Extends a collection with exactly one element.
fn extend_reserve(&mut self, additional: usize)
[src]
fn extend_reserve(&mut self, additional: usize)
[src]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]
impl<T, P> From<RBQueue<T, P>> for RBTree<T> where
T: PartialOrd,
P: Copy + Fn(&T, &T) -> Ordering,
[src]Auto Trait Implementations
impl<T, P> RefUnwindSafe for RBQueue<T, P> where
P: RefUnwindSafe,
T: RefUnwindSafe,
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, P> Send for RBQueue<T, P> where
P: Send,
T: Send,
P: Send,
T: Send,
impl<T, P> Sync for RBQueue<T, P> where
P: Sync,
T: Sync,
P: Sync,
T: Sync,
impl<T, P> Unpin for RBQueue<T, P> where
P: Unpin,
T: Unpin,
P: Unpin,
T: Unpin,
impl<T, P> UnwindSafe for RBQueue<T, P> where
P: UnwindSafe,
T: UnwindSafe,
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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