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
ptris a valid allocation ofcap * size_of::<T>()bytes whencap > 0.- Elements at indices
0..lenare fully initialised and satisfy the heap property: for everyi,ctx.less(data[i], data[2*i+1])andctx.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>
impl<'a, T, C: OrdContext<T>> ExPriorityQueue<'a, T, C>
Sourcepub fn new(alloc: &'a dyn Allocator, ctx: C) -> Self
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?
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}Sourcepub fn peek(&self) -> Option<&T>
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.
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Examples found in repository?
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}Sourcepub fn try_push(&mut self, value: T) -> Result<(), T>
pub fn try_push(&mut self, value: T) -> Result<(), T>
Attempts to push value, returning Err(value) if OOM.
Sourcepub fn push_slice(&mut self, items: &[T])where
T: Copy,
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?
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}Sourcepub fn pop(&mut self) -> Option<T>
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?
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}Sourcepub fn remove_at(&mut self, idx: usize) -> T
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.
Sourcepub fn rebuild(&mut self)
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.
Sourcepub fn drain_sorted(&mut self, out: &mut [T])where
T: Copy,
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().