好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Lua中的table

Lua中的table

 Lua中的table不是一种简单的数据结构,它可以作为其它数据结构的基础。如数组、记录、线性表、队列和集合等,在Lua中都可以通过table来表示。         
    1. 数组:
    使用整数来索引table即可在Lua中实现数组。因此,Lua中的数组没有固定的大小,如:

 1  a = {}
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

    在Lua中,可以让任何数作为数组的起始索引,但通常而言,都会使用1作为其起始索引值。而且很多Lua的内置功能和函数都依赖这一特征,因此在没有充分理由的前提下,尽量保证这一规则。下面的方法是通过table的构造器来创建并初始化一个数组的,如:
     squares = {1, 4, 9, 16, 25}

    2. 二维数组:
    在Lua中我们可以通过两种方式来利用table构造多维数组。其中,第一种方式通过“数组的数组”的方式来实现多维数组的,即在一维数组上的每个元素也同样为table对象,如:

 1  mt = {}
2 for i = 1 , N do
3 mt[i] = {}
4 for j = 1 , M do
5 mt[i][j] = i * j
6 end
7 end

    第二种方式是将二维数组的索引展开,并以固定的常量作为第二维度的步长,如:

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


    3. 链表:
    由于table是动态的实体,所以在Lua中实现链表是很方便的。其中,每个结点均以table来表示,一个“链接”只是结点中的一个字段,该字段包含对其它table的引用,如:

  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. 队列与双向队列:
    在Lua中实现队列的简单方法是使用table库函数insert和remove。但是由于这种方法会导致后续元素的移动,因此当队列的数据量较大时,不建议使用该方法。下面的代码是一种更高效的实现方式,如:

  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. 集合和包(Bag):
    在Lua中用table实现集合是非常简单的,见如下代码:
    reserved = { ["while"] = true, ["end"] = true, ["function"] = true, }
    if not reserved["while"] then
        --do something
    end
    在Lua中我们可以将包(Bag)看成MultiSet,与普通集合不同的是该容器中允许key相同的元素在容器中多次出现。下面的代码通过为table中的元素添加计数器的方式来模拟实现该数据结构,如:

 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:
    如果想在Lua中将多个字符串连接成为一个大字符串的话,可以通过如下方式实现,如:

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

    上面的代码确实可以正常的完成工作,然而当行数较多时,这种方法将会导致大量的内存重新分配和内存间的数据拷贝,由此而带来的性能开销也是相当可观的。事实上,在很多编程语言中String都是不可变对象,如Java,因此如果通过该方式多次连接较大字符串时,均会导致同样的性能问题。为了解决该问题,Java中提供了StringBuilder类,而Lua中则可以利用table的concat方法来解决这一问题,见如下代码:

  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 -- concat方法可以接受两个参数,因此上面的方式还可以改为:
8 local t = {}
9 for line in io.lines () do
10 t[#t + 1 ] = line
11 end
12 local s = table.concat (t, " \n " )

 

分类:  Lua编程

http://www.cnblogs.com/stephen-liu74/archive/2012/06/25/2417894.html

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于Lua中的table的详细内容...

  阅读:50次

上一篇: 架构培训

下一篇:系统过载保护