Lua common data structure and lua Data Structure
The table in Lua is not a simple data structure. It can be used as the basis of other data structures. Such as arrays, records, linear tables, queues, and sets, which can be expressed by tables in Lua.
I. Array
In lua, you can access the elements in the table through the integer subscript to implement arrays. In addition, you do not need to specify the size of the array. The size can dynamically increase as needed.
a = {}for i = 1,100 doa[i] = 0endprint("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 of the array is used to start from 1, and the standard library of Lua is consistent with this habit, therefore, if your array subscript is also from 1, you can directly use the function of the standard library, otherwise you will not be able to directly use it.
2. Two-dimensional array
There are two main methods to represent a matrix in Lua. The first method is to represent an array. That is to say, the element of a table is another table.
local N = 3local M = 3mt = {}for i = 1,N domt[i] = {}for j = 1,M domt[i][j] = i * jendendmt = {}for i = 1, N dofor j = 1, M domt[(i - 1) * M + j] = i * jendend
Iii. Linked List
In Lua, tables is used to easily implement a linked list. Each node is a table, a pointer is a field of the table, and points to another node (table ). For example, to implement a basic linked list with only two fields: values and pointers, the Code is as follows:
list = nilfor i = 1, 10 dolist = { next = list ,value = i}endlocal l = listwhile l do --print(l.value)l = l.nextend
4. queue and bidirectional queue
Although the insert and remove operations provided by the Lua table database can be used to implement the queue, the queue implementation in this way has a low aging rate for large data volumes. The effective method is to use two index subscripts, one represents the first element, and the other represents the last element.
List = {} -- create a function List. new () return {first = 0, last =-1} end -- queue header inserted function List. pushFront (list, value) local first = list. first-1list. first = firstlist [first] = valueend -- insert function List at the end of the queue. popFront (list) local first = list. firstif first> list. last thenerror ("List is empty") endlocal value = list [first] list [first] = nillist. first = first + 1 return valueendfunction List. popBack (list) local last = list. lastif list. first> last thenerror ("List is empty") endlocal value = list [last] list [last] = nillist. last = last-1 return valueend -- test code: local testList = {first = 0, last =-1} local tableTest = 12List. pushFront (testList, tableTest) print (List. popFront (testList ))
V. Stack
The Code is as follows:
Local stackMng = {} stackMng. _ index = stackMngfunction stackMng: new () local temp = {} setretriable (temp, stackMng) return tempendfunction stackMng: init () self. stackList = {} endfunction stackMng: reset () self: init () endfunction stackMng: clear () self. stackList = {} endfunction stackMng: pop () if # self. stackList = 0 thenreturnendif self. stackList [1] thenprint (self. stackList [1]) endreturn table. remove (self. stackList, 1) endfunction stackMng: push (t) table. insert (self. stackList, t) endfunction stackMng: Count () return # self. stackListend -- test code object = stackMng: new () object: init () object: push (1) object: pop ()
Vi. Set
Using table in Lua to implement a set is very simple. See the following code:
reserved = {["while"] = true, ["end"] = true,["function"] = true, ["local"] = true,}for k,v in pairs(reserved) doprint(k,"->",v)end
Lua script (which understands c/c ++) is simple'
It is indeed a Lua script.
Spr = Sprite. new ("sprite.png", 24, 32, VRAM)
This statement declares an object of the Sprite class. (Of course, The lua objects are all simulated by tables .)
Spr: addAnimation ({300,},) -- Walk up
":" Is the syntax of the Lua object call method. {0, 1, 2, 1} is the unique data structure table of Lua, which is the annotation description of lua.
Other while loops and if condition statements have the same meaning as c.
It seems that this is probably a script for controlling the movement of characters on the screen.
I suggest you go to lua's getting started book to understand it.
I learned LUA and read the syntax structure, but I don't know how to study it.
... What do you want to learn with LUA?
But there is a programming language "currency", that is, the data structure. Let's take a look.
Stack (using a maze for big purposes), Binary Trees (using big orders), queues, linked lists, and graphs (seemingly powerful, I Don't Know What To Do)
To learn these basic data structures, you can also learn algorithms, Bubble sorting, and round-table algorithms.
The above is the programming currency
LUA has a "required" class that involves metadata tables and metadata methods. Have you learned this?