lloyAlloy on GitHub
Queue
Docs

Queue

Updated
Queue<T>
import { Queue } from "@alloy/std/collections"

type Job = { name: string }

local jobs: Queue<Job> = new Queue()
jobs:push({ name = "save" })
while local next = jobs:pop() do
    print(next.name)
end

A first-in, first-out queue over a ring of indices, from the std: push at the back costs the same at any size, and so does pop at the front. Queue.from(t) fills one from an array. A for loop reads it front to back without a pop.

Statics

Queue.new

Queue.new<T>(items: { T }?): Queue<T>

An empty queue, or with items the queue Queue.from(items) gives. The annotation on the binding fixes T of an empty queue.

import { Queue } from "@alloy/std/collections"

local jobs: Queue<string> = new Queue()
jobs:push("build")

Queue.from

Queue.from<T>(items: { T }): Queue<T>

A queue filled from an array, front to back in the array's order.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build", "test" })
print(jobs:pop())

Methods

Queue:push

Queue:push(value: T)

Appends value at the back. The ring of indices makes this cost the same at any size.

import { Queue } from "@alloy/std/collections"

local jobs: Queue<string> = new Queue()
jobs:push("build")

Queue:pop

Queue:pop(): T?

Removes the front item and returns it, or nil when the queue is empty. An emptied queue starts its indices over, so they never run away.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build" })
while local job = jobs:pop() do
    print(job)
end

Queue:peek

Queue:peek(): T?

The front item without removing it, or nil when the queue is empty.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build" })
print(jobs:peek())

Queue:len

Queue:len(): number

The number of items waiting.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build", "test" })
print(jobs:len())

Queue:is_empty

Queue:is_empty(): boolean

Whether the queue holds nothing.

import { Queue } from "@alloy/std/collections"

local jobs: Queue<string> = new Queue()
print(jobs:is_empty())

Queue:clear

Queue:clear()

Drops every item and resets the indices.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build" })
jobs:clear()

Queue:to_array

Queue:to_array(): T[]

The items front to back, as a new array. The queue keeps them; a for loop over the queue reads the same order without a pop.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build", "test" })
print(jobs:to_array():join(", "))

Queue:to_table

Queue:to_table(): { T }

The queue's own table, not a copy: a write through it changes the queue. The keys start at the front, which a pop moves up, so read to_array for a list that starts at 1.

import { Queue } from "@alloy/std/collections"

local jobs = Queue.from({ "build", "test" })
local t = jobs:to_table()
print(t[1])