LuaSTG-x Core API
table Class Reference

Public Member Functions

static function append (local t1, local t2)
 
static function average (local t)
 
static function average_by (local t, local fn)
 
static function choose (local t, local fn)
 
static function clone (local t)
 
static function collect (local t, local fn)
 
static function concat (local list, local sep, local i, local j)
 
static function deploy (local t, local src, local default, local isclone)
 
static function dump (local t, local description, local nesting)
 
static function exists (local t, local fn)
 
static function exists2 (local t1, local t2, local fn)
 
static function ffind (local t, local fn)
 
static function ffindkey (local t, local fn)
 
static function filter (local t, local fn)
 
static function forall (local t, local fn)
 
static function has (local t, local v)
 
static function insert (local list, local pos, local value)
 
static function is_empty (local t)
 
static function length (local t)
 
static function map (local t, local fn)
 
static function maxn (local table)
 
static function pack (local ...)
 
static function remove (local list, local pos)
 
static function sort (local list, local comp)
 
static function supplement (local t, local src)
 

Detailed Description

类型 table 实现了关联数组,也就是说,这个数组不仅仅以数字做索引,

除了 nil 和 NaN 之外的所有 Lua 值 都可以做索引。

(*Not a Number* 是一个特殊的数字,它用于表示未定义或表示不了的运算结果,比如 0/0。)

表可以是 异构 的; 也就是说,表内可以包含任何类型的值( nil 除外)。

任何键的值若为 nil 就不会被记入表结构内部。换言之,对于表内不存在的键,

都对应着值 nil

表是 Lua 中唯一的数据结构,它可被用于表示普通数组、序列、符号表、集合、记录、

图、树等等。对于记录,Lua 使用域名作为索引。语言提供了 a.name 这样的语法糖

来替代 a["name"] 这种写法以方便记录这种结构的使用。

在 Lua 中有多种便利的方式创建表。

和索引一样,表中每个域的值也可以是任何类型。需要特别指出的是:既然函数是一等公民,

那么表的域也可以是函数。这样,表就可以携带 方法 了。

索引一张表的原则遵循语言中的直接比较规则。当且仅当 ij 直接比较相等时

(即不通过元方法的比较),表达式 a[i]a[j] 表示了表中相同的元素。

无论何时,若一个操作需要取表的长度,这张表必须是一个真序列,或是拥有 __len 元方法。

所有的函数都忽略传入参数的那张表中的非数字键。

表处理库提供了表处理的通用函数,所有函数都放在表 table 中。

Member Function Documentation

◆ append()

static function append ( local  t1,
local  t2 
)

◆ average()

static function average ( local  t)

返回元素的平均值

◆ average_by()

static function average_by ( local  t,
local  fn 
)

返回通过将函数应用于每个元素而生成的元素平均值

◆ choose()

static function choose ( local  t,
local  fn 
)

将给定函数应用于列表的每个元素。

返回由各元素(该函数返回了非nil/false)的结果组成的列表

Parameters
ttable
fnfun(v:any):any

◆ clone()

static function clone ( local  t)

Clone a table

from cocos2dx

Parameters
ttable
Returns
table

◆ collect()

static function collect ( local  t,
local  fn 
)

对列表的每个元素应用给定函数。连接所有结果并返回组合列表。

Parameters
ttable
fnfun(v:any):table

◆ concat()

static function concat ( local  list,
local  sep,
local  i,
local  j 
)

提供一个列表,其所有元素都是字符串或数字,返回字符串

list[i]..sep..list[i+1] ··· sep..list[j]

sep 的默认值是空串,

i 的默认值是 1 ,

j 的默认值是 #list

如果 ij 大,返回空串。

重载
fun(list:table):string
重载
fun(list:table, sep:string):string
重载
fun(list:table, sep:string, i:number):string
Parameters
listtable
sepstring
inumber
jnumber
Returns
string

◆ deploy()

static function deploy ( local  t,
local  src,
local  default,
local  isclone 
)

Copy values from source to target with default values.

Example: table.deploy(self, params, { a = 1 })

Parameters
tobject
srctable
defaulttable
iscloneboolean @will use table.clone(src) if this is true.

◆ dump()

static function dump ( local  t,
local  description,
local  nesting 
)

Dump a table to strings.

Example: for i, line in ipairs(table.dump(t)) do print(line) end

Parameters
ttable
descriptionstring
nestingnumber
Returns
table

◆ exists()

static function exists ( local  t,
local  fn 
)

◆ exists2()

static function exists2 ( local  t1,
local  t2,
local  fn 
)

◆ ffind()

static function ffind ( local  t,
local  fn 
)

返回给定函数为其返回 true 的第一个元素

◆ ffindkey()

static function ffindkey ( local  t,
local  fn 
)

返回满足给定谓词的列表中第一个元素的索引

◆ filter()

static function filter ( local  t,
local  fn 
)

返回一个新集合,其中仅包含给定谓词为其返回 true 的集合的元素

Parameters
ttable
fnfun(v:any):boolean

◆ forall()

static function forall ( local  t,
local  fn 
)

测试集合的所有元素是否满足给定谓词

◆ has()

static function has ( local  t,
local  v 
)
Parameters
ttable
Returns
boolean

◆ insert()

static function insert ( local  list,
local  pos,
local  value 
)

list 的位置 pos 处插入元素 value

并后移元素 list[pos], list[pos+1], ···, list[#list]

pos 的默认值为 #list+1

因此调用 table.insert(t,x) 会将 x 插在列表 t 的末尾。

重载
fun(list:table, value:any):number
Parameters
listtable
posnumber
valueany
Returns
number

◆ is_empty()

static function is_empty ( local  t)

如果列表不包含任何元素,则返回 true;否则返回 false

◆ length()

static function length ( local  t)

◆ map()

static function map ( local  t,
local  fn 
)

创建一个新集合,其元素是将给定函数应用于集合的每个元素的结果

◆ maxn()

static function maxn ( local  table)

Returns the largest positive numerical index of the given table, or zero

if the table has no positive numerical indices. (To do its job this function

does a linear traversal of the whole table.)

Parameters
tabletable
Returns
number

◆ pack()

static function pack ( local ...  )

Returns a new table with all arguments stored into keys 1, 2, etc. and

with a field "`n`" with the total number of arguments. Note that the

resulting table may not be a sequence, if some arguments are nil.

Returns
table

◆ remove()

static function remove ( local  list,
local  pos 
)

移除 listpos 位置上的元素,并返回这个被移除的值。

pos 是在 1 到 #list 之间的整数时,

它向前移动元素 list[pos+1], list[pos+2], ···, list[#list]

并删除元素 list[#list]

索引 pos 可以是 #list + 1 ,或在 #list 为 0 时可以是 0 ;

在这些情况下,函数删除元素 list[pos]

pos 默认为 #list

因此调用 table.remove(l) 将移除表 l 的最后一个元素。

重载
fun(list:table):any
Parameters
listtable
posnumber
Returns
any

◆ sort()

static function sort ( local  list,
local  comp 
)

在表内从 list[1]list[#list] 原地

对其间元素按指定次序排序。

如果提供了 comp

它必须是一个可以接收两个列表内元素为参数的函数。

当第一个元素需要排在第二个元素之前时,返回真

(因此 not comp(list[i+1],list[i]) 在排序结束后将为真)。

如果没有提供 comp

将使用标准 Lua 操作 < 作为替代品。

排序算法并不稳定;

即当两个元素次序相等时,它们在排序后的相对位置可能会改变。

@generic V

重载
fun(list:table):number
Parameters
listtable<number, V>
compfun(a:V, b:V):number
Returns
number

◆ supplement()

static function supplement ( local  t,
local  src 
)

Copy values from source to target when key is missing.

Parameters
tobject
srctable

The documentation for this class was generated from the following files: