Struct flex_algo::priority_queue::PriorityQueue
source · pub struct PriorityQueue<F, T>where
F: Fn(&T, &T) -> bool,
T: PartialOrd + Debug,{ /* 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>where
F: Fn(&T, &T) -> bool,
T: PartialOrd + Debug,
impl<F, T> PriorityQueue<F, T>where
F: Fn(&T, &T) -> bool,
T: PartialOrd + Debug,
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);