A high-performance Deque (Queue/Stack) library for Lua.
A high-performance Double-Ended Queue (Deque) implementation for Lua. This library provides a unified structure to handle Queues (FIFO), Stacks (LIFO), and general-purpose list manipulations with efficiency.
-
Fast Operations:
push,pop, anddequeuerun in$O(1)$ time. - Memory Managed: Automatically clears internal references to support the Lua Garbage Collector.
-
Sequential Reliability: Uses internal pointers (
first/last) to avoid common pitfalls with the Lua#operator. - Built-in Iterators: Native support for forward and reverse traversal.
Download tables.lua and place it in your project directory. Load it using:
local tables = require("tables")
Note: Always initialize your structure with tables.create() before use.
- As a Queue (First-In, First-Out)
local queue = tables.create()
tables.push(queue, "Order 1")
tables.push(queue, "Order 2")
print(tables.dequeue(queue)) -- Output: Order 1
- As a Stack (Last-In, First-Out)
local stack = tables.create()
tables.push(stack, "Level 1")
tables.push(stack, "Level 2")
print(tables.pop(stack)) -- Output: Level 2
local list = tables.create()
tables.push(list, "A")
tables.push(list, "B")
tables.push(list, "A")
local found, positions = tables.contains(list, "A")
if found then
print("Found 'A' at indices:")
tables.print(positions)
end
- Function Description
create()Initializes a new structure.push(q, val)Adds an element to the end.dequeue(q)Removes and returns the first element.pop(q)Removes and returns the last element.peekFirst(q)Returns the first element without removing it.peekLast(q)Returns the last element without removing it.length(q)Returns the total number of elements.contains(q, val)Returns true and a table of positions if found.reverse(q)Reverses the structure in-place.removeValue(q, val)Removes all occurrences of a specific value.tems(q)Returns a forward iterator for for loops.itemsReverse(q)Returns a backward iterator for for loops.
This project is open-source and available under the MIT License.
For the Lua Community.