Lua Array Matrice List set bag
1.Lua Array
For instance, after the following code, any attempt to access a field outside the range 1–1000 will return nil, instead of zero
a = {} -- new array for i = 1, 1000 do a[i] = 0 end
The length operator (‘#’) uses this fact to find the size of an array:
print(#a) --> 1000
You can start an array at index 0, 1, or any other value:
-- creates an array with indices from -5 to 5
a = {} for i = -5, 5 do a[i] = 0 end
2.Matrices and Multi-Dimensional Arrays
There are two main ways to represent matrices in Lua
1.The first one is to use an array of arrays, that is, a table wherein each element is another table.
For instance, you can create a matrix of zeros with dimensions N by M with the following code
mt = {} -- create the matrix for i = 1, N do mt[i] = {} -- create a new row for j = 1, M do mt[i][j] = 0 end end
this is certainly more verbose, but it gives you more flexibility.
2. The second way to represent a matrix in Lua is by composing the two indices into a single one.
mt = {} -- create the matrix for i = 1, N do for j = 1, M do mt[(i - 1)*M + j] = 0 end end
3.Linked Lists
Because tables are dynamic entities, it is easy to implement linked lists in Lua.
For instance, let us implement a basic list, where each node has two fields, next and value. A simple variable is the list root:
list = nil
--To insert an element at the beginning of the list, with a value v, we do: list = {next = list, value = v}
--To traverse the list, we write: local l = list while l do <visit l.value> l = l.next end
4.Queues and Double Queues
1.A simple way to implement queues in Lua is with functions insert and remove from the table library.
function ListNew () return {first = 0, last = -1} end
To avoid polluting the global space, we will define all list operations inside a table, properly called List (that is, we will create a module). Therefore, we rewrite our last example like this:
List = {} function List.new () return {first = 0, last = -1} end
Now, we can insert or remove an element at both ends in constant time:
function List.pushfirst (list, value) local first = list.first - 1 list.first = first list[first] = value end function List.pushlast (list, value) local last = list.last + 1 list.last = last list[last] = value end function List.popfirst (list) local first = list.first if first > list.last then error("list is empty") end local value = list[first] list[first] = nil -- to allow garbage collection list.first = first + 1 return value end function List.poplast (list) local last = list.last if list.first > last then error("list is empty") end local value = list[last] list[last] = nil -- to allow garbage collection list.last = last - 1 return value end
5.Sets and Bags
In Lua, an efficient and simple way to represent such sets is to put the set elements as indices in a table.
reserved = { ["while"] = true, ["end"] = true, ["function"] = true, ["local"] = true, } for w in allwords() do if not reserved[w] then <do something with ’w’> -- 'w' is not a reserved word end end
You can have a clearer initialization using an auxiliary function to build the set:
function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end reserved = Set{"while", "end", "function", "local", }
Bags, also called multisets, differ from regular sets in that each element can appear multiple times.
function insert (bag, element) bag[element] = (bag[element] or 0) + 1 end
To remove an element we decrement its counter:
function remove (bag, element) local count = bag[element] bag[element] = (count and count > 1) and count - 1 or nil end
We only keep the counter if it already exists and it is still greater than zero.
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步