Skip to main content

ExPriorityQueue

Struct ExPriorityQueue 

Source
pub struct ExPriorityQueue<'a, T, C: OrdContext<T>> { /* private fields */ }
Expand description

A binary-heap priority queue backed by an explicit allocator.

The “top” element is always the one for which ctx.less(top, other) is true for every other element — i.e. the element that the context considers smallest. Use a min-context for a min-heap, a max-context for a max-heap.

§Invariants

  • ptr is a valid allocation of cap * size_of::<T>() bytes when cap > 0.
  • Elements at indices 0..len are fully initialised and satisfy the heap property: for every i, ctx.less(data[i], data[2*i+1]) and ctx.less(data[i], data[2*i+2]) are false (parent ≤ children).

§Lifetime

The allocator reference 'a must outlive the ExPriorityQueue.

Implementations§

Source§

impl<'a, T, C: OrdContext<T>> ExPriorityQueue<'a, T, C>

Source

pub fn new(alloc: &'a dyn Allocator, ctx: C) -> Self

Creates a new, empty priority queue.

No allocation is performed until the first element is pushed.

Examples found in repository?
examples/priority_queue.rs (line 15)
11fn main() {
12    let alloc = SystemAllocator;
13    let ctx = MinIntContext;
14
15    let mut pq = ExPriorityQueue::new(&alloc, ctx);
16
17    pq.push(50);
18    pq.push(10);
19    pq.push(30);
20
21    let data = [5, 1, 100];
22    pq.push_slice(&data);
23
24    while let Some(top) = pq.pop() {
25        println!("Pop: {}", top);
26    }
27}
Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

Source

pub fn is_empty(&self) -> bool

Returns true if the queue is empty.

Source

pub fn capacity(&self) -> usize

Returns the current buffer capacity in elements.

Source

pub fn peek(&self) -> Option<&T>

Returns a reference to the top (smallest/largest) element, or None if the queue is empty.

The top element is always at index 0 in the heap array.

Source

pub fn push(&mut self, value: T)

Pushes value onto the queue.

§Panics

Panics if the backing allocator cannot grow the buffer.

Examples found in repository?
examples/priority_queue.rs (line 17)
11fn main() {
12    let alloc = SystemAllocator;
13    let ctx = MinIntContext;
14
15    let mut pq = ExPriorityQueue::new(&alloc, ctx);
16
17    pq.push(50);
18    pq.push(10);
19    pq.push(30);
20
21    let data = [5, 1, 100];
22    pq.push_slice(&data);
23
24    while let Some(top) = pq.pop() {
25        println!("Pop: {}", top);
26    }
27}
Source

pub fn try_push(&mut self, value: T) -> Result<(), T>

Attempts to push value, returning Err(value) if OOM.

Source

pub fn push_slice(&mut self, items: &[T])
where T: Copy,

Pushes all elements in items and rebuilds the heap in O(n) time.

More efficient than calling push in a loop when many elements are added at once.

§Panics

Panics if the backing allocator fails during growth.

Examples found in repository?
examples/priority_queue.rs (line 22)
11fn main() {
12    let alloc = SystemAllocator;
13    let ctx = MinIntContext;
14
15    let mut pq = ExPriorityQueue::new(&alloc, ctx);
16
17    pq.push(50);
18    pq.push(10);
19    pq.push(30);
20
21    let data = [5, 1, 100];
22    pq.push_slice(&data);
23
24    while let Some(top) = pq.pop() {
25        println!("Pop: {}", top);
26    }
27}
Source

pub fn pop(&mut self) -> Option<T>

Removes and returns the top element, or None if empty.

Swaps the root with the last element, decrements len, and sifts the new root down to restore the heap property.

Examples found in repository?
examples/priority_queue.rs (line 24)
11fn main() {
12    let alloc = SystemAllocator;
13    let ctx = MinIntContext;
14
15    let mut pq = ExPriorityQueue::new(&alloc, ctx);
16
17    pq.push(50);
18    pq.push(10);
19    pq.push(30);
20
21    let data = [5, 1, 100];
22    pq.push_slice(&data);
23
24    while let Some(top) = pq.pop() {
25        println!("Pop: {}", top);
26    }
27}
Source

pub fn remove_at(&mut self, idx: usize) -> T

Removes the element at idx and returns it.

Sifts up and down to restore the heap property regardless of whether the replacement element is smaller or larger than its neighbours.

§Panics

Panics if idx >= self.len.

Source

pub fn rebuild(&mut self)

Rebuilds the heap from scratch in O(n) via bottom-up heapification.

Called automatically by push_slice; can also be called manually after bulk insertion via unsafe direct writes.

Source

pub fn drain_sorted(&mut self, out: &mut [T])
where T: Copy,

Drains the queue in sorted order into out.

Repeatedly pops the top element and copies it into successive positions of out. After this call the queue is empty.

§Panics

Panics if out.len() < self.len().

Source

pub fn for_each<F: FnMut(&T)>(&self, f: F)

Calls f with a reference to each element in unspecified order.

Elements are visited in heap-array order (not sorted order).

Trait Implementations§

Source§

impl<T, C: OrdContext<T>> Drop for ExPriorityQueue<'_, T, C>

Source§

fn drop(&mut self)

Drops all elements and releases the backing buffer.

Auto Trait Implementations§

§

impl<'a, T, C> Freeze for ExPriorityQueue<'a, T, C>
where C: Freeze,

§

impl<'a, T, C> !RefUnwindSafe for ExPriorityQueue<'a, T, C>

§

impl<'a, T, C> !Send for ExPriorityQueue<'a, T, C>

§

impl<'a, T, C> !Sync for ExPriorityQueue<'a, T, C>

§

impl<'a, T, C> Unpin for ExPriorityQueue<'a, T, C>
where C: Unpin, T: Unpin,

§

impl<'a, T, C> UnsafeUnpin for ExPriorityQueue<'a, T, C>
where C: UnsafeUnpin,

§

impl<'a, T, C> !UnwindSafe for ExPriorityQueue<'a, T, C>

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.