6.20.7 Queues

Two standard datastructures are stacks and queues. A stack is a last-in-first-out (LIFO) structure; in Asymptote, arrays can be used as stacks via the push and pop methods. A queue is a first-in-first-out (FIFO) structure; the collections.queue(T) module provides a queue implementation.

Queue_T makeQueue(T[] initialData)

returns a queue initialized with a copy of the array initialData. The parameter initialData is required in order to specify the type T in case there are multiple Queue_T types in the current scope. If no initial data is needed, use makeQueue(new T[]) to create an empty array as a parameter.

The Queue_T structure has the following methods:

void push(T item)

adds item to the end of the queue.

T pop()

removes and returns the first item in the queue. If the queue is empty, an error is thrown.

T peek()

returns the first item in the queue without removing it. If the queue is empty, an error is thrown.

int size()

returns the number of items in the queue.

Iter_T operator iter()

returns an iterator over the items in the queue, in the order they will be returned by pop().

If an item is pushed or popped, then any iterators created before that change are undermined and may behave unpredictably. The current implementation attempts to throw errors if an iterator is used after being undermined, but this behavior is not guaranteed and might be removed in future versions for performance reasons.

In addition to these methods, there is also an autounraveled implicit cast from Queue_T to Iterable_T. This cast allows a Queue_T to be passed as a parameter to utilities like zip or enumerate.