Misc.Queue module

A double queue.

Taken from Programming in Lua Queues and Double Queues and modified to not allow nil values, and returns nil if pop_first or pop_last is used when the queue is empty.

Usage

local Queue = require('__stdlib__/stdlib/lists/queue')

Functions

count() Shortcut for Queue.size
is_empty(queue) Returns true if the given queue is empty.
load(queue) Load global.queue or queues during on_load, as metatables are not persisted.
new(_, ...) Constructs a new Queue object.
peek() Shortcut for Queue.peek_first
peek_first(queue) Return the element at the front of the queue and remove it from the queue.
peek_last(queue) Return the element at the back of the queue.
pop() Shortcut for Queue.pop_first
pop_first(queue) Retrieve the element at the front of the queue and remove it from the queue.
pop_last(queue) Retrieve the element at the back of the queue and remove it from the queue.
push() Shortcut for Queue.push_last
push_first(queue, value) Push a new element to the front of the queue.
push_last(queue, value) Push a new element to the back of the queue.
size(queue) Returns the number of items in the queue.

Functions

# count()

Shortcut for Queue.size

# is_empty(queue)

Returns true if the given queue is empty.

Parameters:
  • queue : (Queue) the queue to check
Returns:
  • (boolean) true if empty, false otherwise
# load(queue)

Load global.queue or queues during on_load, as metatables are not persisted.

This is only needed if you are using the queue as an object and storing it in global.

Parameters: Usage:
global.myqueue1 = Queue.new()
 script.on_load(function() Queue.load(global.myqueue))
# new(_, ...)

Constructs a new Queue object.

Parameters:
  • _
  • ...
Returns:
  • (Queue) a new, empty queue
# peek()

Shortcut for Queue.peek_first

# peek_first(queue)

Return the element at the front of the queue and remove it from the queue.

Parameters:
  • queue : (Queue) the queue to retrieve the element from
Returns:
  • (Mixed) the element at the front of the queue
# peek_last(queue)

Return the element at the back of the queue.

Parameters:
  • queue : (Queue) the queue to retrieve the element from
Returns:
  • (Mixed) the element at the back of the queue
# pop()

Shortcut for Queue.pop_first

# pop_first(queue)

Retrieve the element at the front of the queue and remove it from the queue.

Parameters:
  • queue : (Queue) the queue to retrieve the element from
Returns:
  • (Mixed) value the element at the front of the queue
# pop_last(queue)

Retrieve the element at the back of the queue and remove it from the queue.

Parameters:
  • queue : (Queue) the queue to retrieve the element from
Returns:
  • (Mixed) the element at the back of the queue
# push()

Shortcut for Queue.push_last

# push_first(queue, value)

Push a new element to the front of the queue.

Parameters:
  • queue : (Queue) the queue to push an element to
  • value : (Mixed) the element to push
# push_last(queue, value)

Push a new element to the back of the queue.

Parameters:
  • queue : (Queue) the queue to push an element to
  • value : (Mixed) the element to push
# size(queue)

Returns the number of items in the queue.

Parameters:
  • queue : (Queue) the queue to check
Returns:
  • (number) the number of items in the queue