PriorityQueue

class guppylang.std.collections.PriorityQueue[source]

A queue of values ordered by priority.

Values with the lowest priority value will be popped from the queue first. (e.g. Priority 1 will be returned before priority 2.) To ensure static allocation, the maximum queue size must be specified in advance and is tracked in the type. For example, the PriorityQueue[int, 10] is a queue that can hold at most 10 prioritized integers.

Use empty_priority_queue to construct a new priority_queue.

__iter__() PriorityQueue[T, MAX_SIZE][source]

Returns an iterator over the queued elements paired with their priority.

Elements are yielded in priority order.

__len__() int[source]

Returns the number of elements currently stored in the priority queue.

__next__() Option[tuple[tuple[int, T], PriorityQueue[T, MAX_SIZE]]][source]
buf: array[Option[tuple[int, T]], MAX_SIZE]

Underlying buffer holding the priority queue elements.

INVARIANT: All array elements up to and including index self.size - 1 are option.some variants and all further ones are option.nothing.

discard_empty() None[source]

Discards a priority queue of potentially non-droppable elements assuming that the queue is empty.

Panics if the queue is not empty.

peek() tuple[int, TCopyable, PriorityQueue[TCopyable, MAX_SIZE]][source]

Returns a copy of the next element in the priority queue without removing it.

Panics if the priority queue is empty.

Note that this operation is only allowed if the priority queue elements are copyable.

pop() tuple[int, T, PriorityQueue[T, MAX_SIZE]][source]

Removes the next element from the priority queue.

Panics if the priority queue is empty.

push(value: T @ owned, priority: int) PriorityQueue[T, MAX_SIZE][source]

Adds an element in the correct order to the priority queue.

Panics if the priority queue has already reached its maximum size.

size: int

Index of the next free index in self.buf.