Struct 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,

Source

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);
 
Source

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);
Source

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);
Source

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);
Source

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);
Source

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§

Source§

impl<F, T> Debug for PriorityQueue<F, T>
where F: Fn(&T, &T) -> bool + Debug, T: PartialOrd + Debug + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F, T> Freeze for PriorityQueue<F, T>
where F: Freeze,

§

impl<F, T> RefUnwindSafe for PriorityQueue<F, T>

§

impl<F, T> Send for PriorityQueue<F, T>
where F: Send, T: Send,

§

impl<F, T> Sync for PriorityQueue<F, T>
where F: Sync, T: Sync,

§

impl<F, T> Unpin for PriorityQueue<F, T>
where F: Unpin, T: Unpin,

§

impl<F, T> UnwindSafe for PriorityQueue<F, T>
where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.