Queue

class guppylang.std.collections.Queue[source]

A first-in-first-out (FIFO) growable collection of values.

To ensure static allocation, the maximum queue size must be specified in advance and is tracked in the type. For example, Queue[int, 10] is a queue that can hold at most 10 integers.

Implemented as a circular buffer, giving O(1) push and pop.

Use empty_queue to construct a new queue.

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

Returns an iterator over the elements in the queue from bottom to top.

__len__() int[source]

Returns the number of elements currently stored in the queue.

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

Underlying circular buffer holding the queue elements.

Elements are stored contiguously from self.start up to and including self.end, wrapping around modulo MAX_SIZE.

The self.size field tracks the number of elements currently in the queue, so we can distinguish between full and empty states. Without this, then the queue would be limited to MAX_SIZE - 1 since we cannot distinguish completely full and completely empty states with just using self.start and self.end.

discard_empty() None[source]

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

Panics if the queue is not empty.

end: int

Index of the next free slot in self.buf.

peek() tuple[TCopyable, Queue[TCopyable, MAX_SIZE]][source]

Returns a copy of the top element of the queue without removing it.

Panics if the queue is empty.

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

pop() tuple[T, Queue[T, MAX_SIZE]][source]

Removes the next element from the queue and returns it.

Panics if the queue is empty.

push(elem: T @ owned) Queue[T, MAX_SIZE][source]

Adds an element to the end of the queue.

Panics if the queue has already reached its maximum size.

size: int

Number of elements currently stored in the queue.

start: int

Index of the current front of the queue (first element to be popped).