Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua commonly uses data structures


May 12, 2021 Lua


Table of contents


Table in Lua is not a simple data structure, it can be used as the basis for other data structures. A rrays, records, linear tables, queues, and collections can all be represented by table in Lua.

One, array

In lua, accessing the elements in the table through the integer undersring simply implements the array. A nd arrays do not have to specify a size in advance, and the size can grow dynamically as needed.

a = {}
for i = 1,100 do
    a[i] = 0
end
print("The length of array 'a' is " .. #a)

squares = {1, 4, 9, 16, 25}
print("The length of array 'a' is " .. #squares)

In Lua, the following table for arrays starts at 1, and Lua's standard library is consistent with this habit, so if your array undersring also starts from 1, you can use the functions of the standard library directly, otherwise you can't use them directly.

Two-dimensional array

There are two main ways to represent a matrix in Lua, the first of which is represented by an array of arrays. T hat is, the element of one table is another.

local N = 3
local M = 3
mt = {}
for i = 1,N do
    mt[i] = {}
    for j = 1,M do
        mt[i][j] = i * j
    end
end

mt = {}
for i = 1, N do
    for j = 1, M do
        mt[(i - 1) * M + j] = i * j
    end
end

Third, the list

It's easy to implement a list with tables in Lua, where each node is a table, the pointer is one domain of the table, and points to another node (table). For example, to implement a basic list of only two domains: values and pointers, the code is as follows:

list = nil
for i = 1, 10 do
    list = { next = list ,value = i}
end

local l = list
while l do 
    --print(l.value)
    l = l.next
end

Queues and two-way queues

Although queues can be implemented using the insert and remove operations provided by Lua's table library, the queues implemented in this way are too inefficient for large amounts of data, effectively using two index undersrings, one representing the first element and the other representing the last element.

List = {}

--创建
function List.new()
    return {first = 0,last = -1}
end

--队列头插入
function List.pushFront(list,value)
    local first = list.first - 1
    list.first = first
    list[first] = value
end

--队列尾插入
function List.popFront(list)
    local first = list.first
    if first > list.last then
        error("List is empty")
    end

    local value = list[first]
    list[first] = nil
    list.first = first + 1
    return value
end

function List.popBack(list)
    local last = list.last
    if list.first > last then
        error("List is empty")
    end
    local value = list[last]
    list[last] = nil
    list.last = last - 1 
    return value
end

--测试代码
local testList = {first = 0,last = -1}
local tableTest = 12

List.pushFront(testList,tableTest)
print( List.popFront(testList))

Five, stack

Simple implementation of stack functionality, the code is as follows:

local stackMng = {}
stackMng.__index = stackMng

function stackMng:new()
    local temp = {}
    setmetatable(temp,stackMng)
    return temp
end

function stackMng:init()
    self.stackList = {}
end

function stackMng:reset()
    self:init()
end

function stackMng:clear()
    self.stackList = {}
end

function stackMng:pop()
    if #self.stackList == 0 then
        return
    end
    if self.stackList[1] then
        print(self.stackList[1])
    end

    return table.remove(self.stackList,1)
end

function stackMng:push(t)
    table.insert(self.stackList,t)
end

function stackMng:Count()
    return #self.stackList
end

--测试代码
object = stackMng:new()
object:init()
object:push(1)
object:pop()

Six, the collection

Implementing a collection with table in Lua is very simple, see the following code:

reserved = {
["while"] = true, ["end"] = true,
["function"] = true, ["local"] = true,
}

for k,v in pairs(reserved) do
    print(k,"->",v)
end