Skip to main content

Queue

A generic Queue class for Luau.

Queues are a type of data structure that follows the First In First Out (FIFO) principle. This means that the first element added to the queue will be the first to be removed.

local queue = Queue.new()

queue:Push(50)
queue:Push(100)
queue:Push(150)

print(queue:Peek()) -- 50

print(queue:Pop()) -- 50
print(queue:Pop()) -- 100
print(queue:Pop()) -- 150

print(queue:Pop()) -- nil (queue is empty)
Type Export

This module exports a type Queue<T> which represents a Queue with values of type T.

MetaMethods

Supports the following metamethods:

  • __tostring: Returns a string representation of the queue.
  • __len: Returns the number of elements in the queue. Equivalent to calling :Size().
  • __iter: Iterates over the values in the queue. Check the private documentation for more information.

Properties

ClassName

Queue.ClassName: "Queue"

Functions

new

static
Queue.new() → Queue<T>

Creates a new Queue

iterating over Queue

metamethod
for  indexnumber, valueT  in  Queue  do

Iterates over the values in the Queue.

local queue = Queue.new()
queue:Push(50)
queue:Push(100)
for i, v in queue do
    print(i, v)
end
1 50
2 100

Peek

Queue:Peek(indexnumber?) → T?

Look at the first value in the queue. If there is an index provided, it will look at the value at that index instead.

local queue = Queue.new()
queue:Push(50)
queue:Push(100)
queue:Push(150)
print(queue:Peek()) -- 50
print(queue:Peek(2)) -- 100
queue:Pop()
print(queue:Peek()) -- 100

Pop

Queue:Pop() → T?

Remove the value at the front of the queue if there is one.

Push

Queue:Push(valueT) → ()

Add a value to the back of the queue.

Prepend

Queue:Prepend(valueT) → ()

Add a value to the front of the queue.

Size

Queue:Size() → number

Get the number of items in the Queue.

IsEmpty

Queue:IsEmpty() → boolean

Check if the queue is empty.

ToArray

Queue:ToArray() → {T}

Converts the Queue into an iterable array.

Has

Queue:Has(valueT) → boolean

Checks to see if a given value exists within the Queue.

CountOccurrences

Queue:CountOccurrences(valueT) → number

Returns the number of occurrences of a given value in the queue.

RemoveFirstOccurrence

Queue:RemoveFirstOccurrence(valueT) → boolean

Removes the first occurrence of a given value in the queue. Returns whether or not it did remove something.

RemoveAllOccurrences

Queue:RemoveAllOccurrences(valueT) → number

Removes all occurrences of a given value in the queue. Returns the number of items removed.

RemoveValueAt

Queue:RemoveValueAt(
indexnumber--

The index of the item to remove.

) → T | nil--

The item that was removed, or nil if the index was out of bounds or the queue is empty.

Removes the item at the given index. Returns whether or not it did remove something. If it did, it will also return the item that was removed. This method should typically only be used in conjunction with the iterator metamethod.

Show raw api
{
    "functions": [
        {
            "name": "__iter",
            "desc": "Iterates over the values in the Queue.\n```lua\nlocal queue = Queue.new()\nqueue:Push(50)\nqueue:Push(100)\nfor i, v in queue do\n    print(i, v)\nend\n```\n```\n1 50\n2 100\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "index: number"
                },
                {
                    "desc": "",
                    "lua_type": "value: T"
                }
            ],
            "function_type": "method",
            "tags": [
                "metamethod"
            ],
            "source": {
                "line": 104,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "__tostring",
            "desc": "Converts the Queue into a string for easy reading.\n```lua\nlocal queue = Queue.new()\nqueue:Push(50)\nqueue:Push(100)\nprint(queue) -- <Queue> {50, 100}\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "tags": [
                "metamethod"
            ],
            "private": true,
            "source": {
                "line": 128,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "__len",
            "desc": "Returns the number of items in the Queue. Equivalent to calling `Queue:Size()`.\n```lua\nlocal queue = Queue.new()\nqueue:Push(50)\nqueue:Push(100)\nprint(#queue) -- 2\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "function_type": "method",
            "tags": [
                "metamethod"
            ],
            "private": true,
            "source": {
                "line": 153,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "new",
            "desc": "Creates a new Queue",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Queue<T>\r\n"
                }
            ],
            "function_type": "static",
            "tags": [
                "static"
            ],
            "source": {
                "line": 161,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Peek",
            "desc": "Look at the first value in the queue.\nIf there is an index provided, it will look at the value at that index instead.\n```lua\nlocal queue = Queue.new()\nqueue:Push(50)\nqueue:Push(100)\nqueue:Push(150)\nprint(queue:Peek()) -- 50\nprint(queue:Peek(2)) -- 100\nqueue:Pop()\nprint(queue:Peek()) -- 100\n```",
            "params": [
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "number?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T?\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 183,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Pop",
            "desc": "Remove the value at the front of the queue if there is one.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T?\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 193,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Push",
            "desc": "Add a value to the back of the queue.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 208,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Prepend",
            "desc": "Add a value to the front of the queue.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 219,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Size",
            "desc": "Get the number of items in the Queue.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 228,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "IsEmpty",
            "desc": "Check if the queue is empty.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 235,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "ToArray",
            "desc": "Converts the Queue into an iterable array.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{T}\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 243,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "Has",
            "desc": "Checks to see if a given value exists within the Queue.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 252,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "CountOccurrences",
            "desc": "Returns the number of occurrences of a given value in the queue.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 264,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "RemoveFirstOccurrence",
            "desc": "Removes the first occurrence of a given value in the queue. Returns whether or not it did remove something.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 277,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "RemoveAllOccurrences",
            "desc": "Removes all occurrences of a given value in the queue. Returns the number of items removed.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 308,
                "path": "lib/queue/src/init.luau"
            }
        },
        {
            "name": "RemoveValueAt",
            "desc": "Removes the item at the given index. Returns whether or not it did remove something.\nIf it did, it will also return the item that was removed. This method should typically\nonly be used in conjunction with the iterator metamethod.",
            "params": [
                {
                    "name": "index",
                    "desc": "The index of the item to remove.",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "The item that was removed, or nil if the index was out of bounds or the queue is empty.",
                    "lua_type": "T | nil"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 340,
                "path": "lib/queue/src/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "ClassName",
            "desc": "",
            "lua_type": "\"Queue\"",
            "source": {
                "line": 83,
                "path": "lib/queue/src/init.luau"
            }
        }
    ],
    "types": [],
    "name": "Queue",
    "desc": "A generic [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) class for Luau.\n\nQueues are a type of data structure that follows the First In First Out (FIFO) principle.\nThis means that the first element added to the queue will be the first to be removed.\n\n```lua\nlocal queue = Queue.new()\n\nqueue:Push(50)\nqueue:Push(100)\nqueue:Push(150)\n\nprint(queue:Peek()) -- 50\n\nprint(queue:Pop()) -- 50\nprint(queue:Pop()) -- 100\nprint(queue:Pop()) -- 150\n\nprint(queue:Pop()) -- nil (queue is empty)\n```\n\n:::tip Type Export\nThis module exports a type `Queue<T>` which represents a Queue with values of type `T`.\n:::\n\n:::info MetaMethods\nSupports the following metamethods:\n- `__tostring`: Returns a string representation of the queue.\n- `__len`: Returns the number of elements in the queue. Equivalent to calling `:Size()`.\n- `__iter`: Iterates over the values in the queue.\nCheck the private documentation for more information.\n:::",
    "source": {
        "line": 40,
        "path": "lib/queue/src/init.luau"
    }
}