Struct rb_tree::RBQueue [−][src]
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: Copy + Fn(&T, &T) -> Ordering,
[src]
P: Copy + Fn(&T, &T) -> Ordering,
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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: Copy + Fn(&T, &T) -> Ordering,
[src]
T: PartialOrd,
P: Copy + Fn(&T, &T) -> Ordering,
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<T: Debug, P> Debug for RBQueue<T, P> where
P: Copy + Fn(&T, &T) -> Ordering,
[src]
P: Copy + Fn(&T, &T) -> Ordering,
impl<T: Debug, P> Display for RBQueue<T, P> where
P: Copy + Fn(&T, &T) -> Ordering,
[src]
P: Copy + Fn(&T, &T) -> Ordering,
impl<T, P> From<RBQueue<T, P>> for RBTree<T> where
T: PartialOrd,
P: Copy + Fn(&T, &T) -> Ordering,
[src]
T: PartialOrd,
P: Copy + Fn(&T, &T) -> Ordering,
impl<T, P> IntoIterator for RBQueue<T, P> where
P: Copy + Fn(&T, &T) -> Ordering,
[src]
P: Copy + Fn(&T, &T) -> Ordering,
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> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
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]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,