Queue
A queue is a first-in-first-out (FIFO) data structure object.
Values are appended at one end and removed from the other by default (push appends to the front, pop removes from the back), so the earliest-pushed value is the first one popped.
Both push and pop accept an optional flag to operate on the opposite end instead, which lets the same object also be used as a deque.
Elements are stored between the back and front indices, which are advanced/rewound as values are pushed and popped, and are reset to 0 whenever the queue becomes empty.
Constructor
Queue(): QueueFields
| Name | Type | Description |
|---|---|---|
front | integer | The front of the queue. Read-only |
back | integer | The back of the queue. Read-only |
Methods
clear | Empties the queue, removing every value and resetting front and back to 0. |
isEmpty | Checks if the queue is empty. |
iterator | Returns an iterator for the queue. |
peek | Returns the value at the front of the queue or the back if from_tail is true. |
pop | Removes and returns the value at the back of the queue or the front if from_tail is true. |
push | Appends a value to the end of the queue or the front if to_head is true. |
#Queue:clear
function Queue:clear()
Empties the queue, removing every value and resetting front and back to 0.
#Queue:isEmpty
function Queue:isEmpty(): boolean
Checks if the queue is empty.
| Type | Name | Description |
|---|---|---|
boolean | is_empty | Returns true if the queue has no values, otherwise false. |
#Queue:iterator
function Queue:iterator(from_tail?: boolean): (fun( queue: Queue<T>, from_tail: boolean ): T), Queue<T>, boolean
Returns an iterator for the queue.
| Name | Type | Description |
|---|---|---|
from_tailoptional | boolean | If true, returns an iterator for the back of the queue. |
#Queue:peek
function Queue:peek(from_tail?: boolean): T | nil
Returns the value at the front of the queue or the back if from_tail is true.
| Name | Type | Description |
|---|---|---|
from_tailoptional | boolean | If true, returns the value at the back of the queue. |
| Type | Name | Description |
|---|---|---|
T | nil | value | The value at the front of the queue, or nil if the queue is empty. |
#Queue:pop
function Queue:pop(from_tail?: boolean): T | nil
Removes and returns the value at the back of the queue or the front if from_tail is true.
| Name | Type | Description |
|---|---|---|
from_tailoptional | boolean | If true, removes and returns the value at the front of the queue. |
| Type | Name | Description |
|---|---|---|
T | nil | value | The value at the back of the queue or the front if from_tail is true, or nil if the queue is empty. |
#Queue:push
function Queue:push(value: T, to_head?: boolean)
Appends a value to the end of the queue or the front if to_head is true.
| Name | Type | Description |
|---|---|---|
value | T | The value to append. |
to_headoptional | boolean | If true, appends the value to the front of the queue. |