Step by step (LUA data structure)

Source: Internet
Author: User

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.
1. array:
you can use an integer to index the table to implement an array in Lua. Therefore, the array in Lua does not have a fixed size, such as:

  1  A ={}< br>  2   for  I =  1 ,  1000   DO  
3 A [I] = 0
4 end
5 Print (" the length of array 'A' is " .. # A)
6 -- the length of array 'A' is 1000

In Lua, any number can be used as the starting index of the array, but in general, 1 is used as the starting index value. Many Lua's built-in functions and functions depend on this feature. Therefore, we try to ensure this rule without a sufficient reason. The following method creates and initializes an array through the table constructor, for example:
Squares = {1, 4, 9, 16, 25}

2. Two-dimensional array:
In Lua, we can use table to construct multi-dimensional arrays in two ways. Specifically, the first method is to implement multi-dimensional arrays through the "array" approach, that is, each element in the one-dimensional array is also a table object, such:

1Mt = {}
2 ForI =1, NDo
3MT [I] = {}
4ForJ =1, MDo
5MT [I] [J] = I * j
6End
7 End

The second method is to expand the index of a two-dimensional array and take a fixed constant as the step of the second dimension, such:

1Mt = {}
2 ForI =1, NDo
3ForJ =1, MDo
4MT [(I-1) * M + J] = I * j
5End
6 End

3. Linked List:
Since tables are dynamic entities, it is very convenient to implement a linked list in Lua. Each node is represented by a table. A "Link" is only a field in the node. This field contains references to other tables, such:

 1 List =Nil 
2 For I = 1 , 10 Do
3 List = { Next = List, value = I}
4 End
5
6 Local L = List
7 While L Do
8 Print (L. value)
9 L = L. Next
10 End

4. queue and two-way queue:
A simple way to implement queues in Lua is to use the table library functions insert and remove. However, this method will lead to the movement of subsequent elements. Therefore, this method is not recommended when the data volume in the queue is large. The followingCodeIs a more efficient implementation method, such:

 1 List = {}
2
3 Function List. New ()
4 Return {First = 0 , Last =- 1 }
5 End
6
7 Function List. pushfront (list, value)
8 Local First = List. First-1
9 List. First = first
10 List [first] = Value
11 End
12
13 Function List. pushback (list, value)
14 Local Last = List. Last + 1
15 List. Last = last
16 List [last] = Value
17 End
18
19 Function List. popfront (list)
20 Local First = List. First
21 If First> list. Last Then
22 Error (" List is empty " )
23 End
24 Local Value = list [first]
25 List [first] = Nil
26 List. First = first + 1
27 Return Value
28 End
29
30 Function List. popback (list)
31 Local Last = List. Last
32 If List. First> last Then
33 Error ( " List is empty " )
34 End
35 Local Value = list [last]
36 List [last] = Nil
37 List. Last = last- 1
38 Return Value
39 End

5. Collection and package ):
Using table in Lua to implement a set is very simple. See the following code:
Reserved = {["while"] = true, ["end"] = true, ["function"] = true ,}
If not reserved ["while"] Then
-- Do something
End
In Lua, we can regard a package as a Multiset. Unlike a normal set, the elements in the container that allow the same key to appear multiple times in the container. The following code simulates the implementation of the data structure by adding counters to the elements in the table, for example:

 1   Function Insert (bag, element)
2 Bag [element] = (BAG [element] Or 0 ) + 1
3 End
4
5 Function Remove (bag, element)
6 Local Count = bag [element]
7 Bag [element] = (count And Count> 1 ) And Count-1 Or Nil
8 End

6. stringbuilder:
To connect multiple strings to a large string in Lua, perform the following operations:

1 LocalBuff =""
2 ForLineIn Io. Lines()Do
3Buff = buff... line .."\ N"
4 End

The above Code does work normally. However, when there are too many lines, this method will lead to a large amount of memory reallocation and Data Copying between memory, the resulting performance overhead is also considerable. In fact, in manyProgramming LanguageString is an immutable object, such as Java. Therefore, if you connect a large string multiple times in this method, the same performance problem will occur. To solve this problem, Java provides the stringbuilder class, while Lua can use the table Concat method to solve this problem. See the following code:

 1  Local T = {}
2 For Line In Io. Lines () Do
3 T [# T + 1 ] = Line .. " \ N "
4 End
5 Local S = Table. Concat (T)
6
7 -- The Concat method can accept two parameters, so the preceding method can be changed:
8 Local T = {}
9 For Line In Io. Lines () Do
10 T [# T +1 ] = Line
11 End
12 Local S = Table. Concat (T, " \ N " )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.