pub struct PriorityQueue<F, T>{ /* private fields */ }
Expand description
PriorityQueue
This data structure implements a Priority Queue with a comparator function to specify the Min/Max heap. The queue is implemented as a heap of indexes.
Implementations§
Source§impl<F, T> PriorityQueue<F, T>
impl<F, T> PriorityQueue<F, T>
Sourcepub fn new(comparator: F) -> Self
pub fn new(comparator: F) -> Self
Create a new PriorityQueue with a comparator function
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Return the size of the PriorityQueue
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
assert_eq!(pq.size(), 0);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return true if the PriorityQueue is empty
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
assert_eq!(pq.is_empty(), true);
Sourcepub fn push(&mut self, value: T) -> usize
pub fn push(&mut self, value: T) -> usize
Push element into Priority Queue and return the size of the PriorityQueue
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
pq.push(14);
pq.push(10);
let len = pq.push(12);
assert_eq!(len, 3);
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Return the first element of the heap, or None
if it is empty.
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
pq.push(14);
pq.push(10);
pq.push(12);
assert_eq!(pq.pop().unwrap(), 10);
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Return the first element of the heap, or None
if it is empty without change the heap.
§Example
use flex_algo::priority_queue::PriorityQueue;
let mut pq = PriorityQueue::new(|a: &usize,b: &usize| a < b);
pq.push(14);
pq.push(10);
pq.push(12);
assert_eq!(pq.peek().unwrap(), &10);
Trait Implementations§
Auto Trait Implementations§
impl<F, T> Freeze for PriorityQueue<F, T>where
F: Freeze,
impl<F, T> RefUnwindSafe for PriorityQueue<F, T>where
F: RefUnwindSafe,
T: RefUnwindSafe,
impl<F, T> Send for PriorityQueue<F, T>
impl<F, T> Sync for PriorityQueue<F, T>
impl<F, T> Unpin for PriorityQueue<F, T>
impl<F, T> UnwindSafe for PriorityQueue<F, T>where
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more