Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Latest commit

 

History

History
208 lines (178 loc) · 16.6 KB

cp.collect.Queue.md

File metadata and controls

208 lines (178 loc) · 16.6 KB

docs » cp.collect.Queue


A "double-ended queue" implementation. This allows pushing and popping values to the left or right side of the queue. This can be used for classic 'stack' and 'queue' uses - for a stack, push and pop from one end, for a queue, push and pop from opposite ends.

# will always return the size of the queue.

The left-most item in the queue wil always be at index 1, the right-most will be at index #.

You can iterate via ipairs, but as with all tables, the queue contains any nil values, it will stop at that point. To iterate the whole queue, you need to use the # operator. Eg:

local q = Queue(1, nil, 3)
for i,v in ipairs(q) do print(v) end  -- Outputs "1"
for i = 1, #q do print(v) end -- Outputs "1", "nil", "3"

API Overview

API Documentation

Functions

Signature cp.collect.Queue.contains(queue, item) -> boolean
Type Function
Description Checks if the queue contains the specified item.
Parameters
  • queue - The queue to check.
  • item - The item to check for.
Returns
  • true if the item is in the queue.
Signature cp.collect.Queue.len(queue) -> anything
Type Function
Description Returns the number of items in the queue.
Parameters
  • queue - The queue to check.
Returns
  • The total number of items.
Signature cp.collect.Queue.peekLeft(queue) -> anything
Type Function
Description Returns the left-most value from the queue without removig it.
Parameters
  • queue - The queue to peek into.
Returns
  • The left-most value of the Queue.
Signature cp.collect.Queue.peekRight(queue) -> anything
Type Function
Description Returns the right-most value from the queue without removig it.
Parameters
  • queue - The queue to peek into.
Returns
  • The right-most value of the Queue.
Signature cp.collect.Queue.popLeft(queue) -> anything
Type Function
Description Removes the left-most value from the queue and returns it.
Parameters
  • queue - The queue to pop from.
Returns
  • The left-most value of the Queue.
Signature cp.collect.Queue.popRight(queue) -> anything
Type Function
Description Removes the right-most value from the queue and returns it.
Parameters
  • queue - The queue to pop from.
Returns
  • The right-most value of the Queue.
Signature cp.collect.Queue.pushLeft(queue, ...) -> cp.collect.Queue
Type Function
Description Pushes the values to the left side of the queue.
Parameters
  • queue - The queue to push into.
  • ... - The values to push.
Returns
  • The same Queue instance.
Signature cp.collect.Queue.pushRight(queue, ...) -> cp.collect.Queue
Type Function
Description Pushes the values to the right side of the queue.
Parameters
  • queue - The queue to push into.
  • ... - The values to push.
Returns
  • The same Queue instance.
Signature cp.collect.Queue:removeItem(item) -> number
Type Function
Description Attempts to remove the specified item from the queue.
Parameters
  • item - The item to remove, if present.
Returns
  • The index of the item, or nil if not found.
Signature cp.collect.Queue.removeItem(queue, item) -> number
Type Function
Description Attempts to remove the specified item from the queue.
Parameters
  • queue - The queue to modify.
  • item - The item to remove, if present.
Returns
  • The index of the item, or nil if not found.

Constructors

Signature cp.collect.Queue.new([...]) -> cp.collect.Queue
Type Constructor
Description Creates a new Queue.
Parameters
  • ... - The optional list of values to add to the right of the queue.
Returns
  • the new Queue.
Notes
  • You can also create a new queue by calling Queue(..) directly.

Methods

Signature cp.collect.Queue:contains(item) -> boolean
Type Method
Description Checks if the queue contains the specified item.
Parameters
  • item - The item to check for.
Returns
  • true if the item is in the queue.
Signature cp.collect.Queue:len(queue) -> anything
Type Method
Description Returns the number of items in the queue.
Parameters
  • queue - The queue to check.
Returns
  • The total number of items.
Signature cp.collect.Queue:peekLeft() -> anything
Type Method
Description Returns the left-most value from the queue without removig it.
Parameters
  • None
Returns
  • The left-most value of the Queue.
Signature cp.collect.Queue:peekRight() -> anything
Type Method
Description Returns the right-most value from the queue without removig it.
Parameters
  • queue - The queue to peek into.
Returns
  • The right-most value of the Queue.
Signature cp.collect.Queue:popLeft() -> anything
Type Method
Description Removes the left-most value from the queue and returns it.
Parameters
  • None
Returns
  • The left-most value of the Queue.
Signature cp.collect.Queue:popRight() -> anything
Type Method
Description Removes the right-most value from the queue and returns it.
Parameters
  • None
Returns
  • The right-most value of the Queue.
Signature cp.collect.Queue:pushLeft(...) -> cp.collect.Queue
Type Method
Description Pushes the values to the left side of the queue.
Parameters
  • ... - The values to push.
Returns
  • The same Queue instance.
Signature cp.collect.Queue:pushRight(...) -> cp.collect.Queue
Type Method
Description Pushes the values to the right side of the queue.
Parameters
  • ... - The values to push.
Returns
  • The same Queue instance.