queue module

Lua queue implementation.

Based on "Queues and Double Queues" from Programming in Lua: http://www.lua.org/pil/11.4.html

Usage

local queue = require("__flib__.queue")

Functions

new() Create a new queue.
load(tbl) Set a queue's metatable to allow directly calling the module's methods.
push_left(tbl, value) Push an element onto the beginning of the queue.
push_right(tbl, value) Push an element onto the end of the queue.
pop_left(tbl) Retrieve an element from the beginning of the queue.
pop_right(tbl) Retrieve an element from the end of the queue.
iter_left(tbl) Iterate over a queue's elements from the beginning to the end.
iter_right(tbl) Iterate over a queue's elements from the end to the beginning.
length(tbl) Get the length of the queue.

Functions

# new()

Create a new queue.

Returns:
# load(tbl)

Set a queue's metatable to allow directly calling the module's methods.

This will need to be re-called if the game is saved and loaded.

Parameters: Usage:
local MyQueue = queue.load(queue.new())
MyQueue:push_right("My string")
local len = MyQueue:length() -- 1
# push_left(tbl, value)

Push an element onto the beginning of the queue.

Parameters:
  • tbl : (table)
  • value : (any)
# push_right(tbl, value)

Push an element onto the end of the queue.

Parameters:
  • tbl : (table)
  • value : (any)
# pop_left(tbl)

Retrieve an element from the beginning of the queue.

Parameters: Returns:
  • (any)
# pop_right(tbl)

Retrieve an element from the end of the queue.

Parameters: Returns:
  • (any)
# iter_left(tbl)

Iterate over a queue's elements from the beginning to the end.

Parameters: Returns: Usage:
local my_queue = queue.new()
for i = 1, 10 do
  queue.push_right(my_queue, 1)
end

-- Will print 1 through 10 in order
for num in queue.iter_left(my_queue) do
  log(i)
end
# iter_right(tbl)

Iterate over a queue's elements from the end to the beginning.

Parameters: Returns: Usage:
local my_queue = queue.new()
for i = 1, 10 do
  queue.push_right(my_queue, 1)
end

-- Will print 10 through 1 in reverse order
for num in queue.iter_right(my_queue) do
  log(i)
end
# length(tbl)

Get the length of the queue.

Parameters: Returns: