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, linked lists, and queues.

 

1. Array

In Lua, table indexes can be expressed in many ways. If an integer is used to represent the index of the table, you can use the table to implement the array. In Lua, the index usually starts from 1.

-- Two-dimensional array n = 10 m = 10arr = {} for I = 1, n do arr [I] = {} for j = 1, M do arr [I] [J] = I * j endendfor I = 1, n do for j = 1, m do if (J ~ = M) then Io. Write (ARR [I] [J] .. "") else print (ARR [I] [J]) end endend


2. Linked List

In Lua, table is a dynamic entity, so it is convenient to represent the linked list. Each node is represented by table.

list = nilfor i = 1, 10 do    list = { next = list, value = i}endlocal l = listwhile l do    print(l.value)    l = l.nextend

 

3. queue and double-end queue

In Lua, the simple method to implement queues is to call the insert and remove functions in table. However, if the data volume is large, the efficiency is still very slow. The following is a manual implementation, which is much more efficient.

List = {} function list. new () return {First = 0, last =-1} endfunction list. pushfront (list, value) list. first = List. first-1 list [list. first] = valueendfunction list. pushback (list, value) list. last = List. last + 1 list [list. last] = valueendfunction 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 valueendfunction list. popback (list) Local last = List. last if last <list. first then error ("list is empty! ") End local value = list [last] list [last] = nil list. last = last-1 return valueendlp = List. new () list. pushfront (LP, 1) list. pushfront (LP, 2) list. pushback (LP,-1) list. pushback (LP,-2) x = List. popfront (LP) print (x) x = List. popback (LP) print (x) x = List. popfront (LP) print (x) x = List. popback (LP) print (x) x = List. popback (LP) print (x) -- output result -- 2 ---2 -- 1 ---1 -- Lua :... list is empty!

 

4. 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:

 

function insert(Bag,element)    Bag[element]=(Bag[element] or 0)+1endfunction remove(Bag,element)   local count=Bag[element]   if count >0 then Bag[element]=count-1   else Bag[element]=nil   endend   

 

 

5. stringbuild

If you connect a series of strings to a large string in Lua, follow these steps:

Inefficiency:

local buff=""for line in io.lines() do   buff=buff..line.."\n"end

High efficiency:

Local T = {} for line in Io. lines () do if (line = nil) Then break end t [# t + 1] = lineendlocal S = table. concat (T, "\ n") -- concatenates strings in table t

 

Lua Data Structure

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.