LuaSTG-x Core API
|
类型 table 实现了关联数组,也就是说,这个数组不仅仅以数字做索引,
除了 nil 和 NaN 之外的所有 Lua 值 都可以做索引。
(*Not a Number* 是一个特殊的数字,它用于表示未定义或表示不了的运算结果,比如 0/0。)
表可以是 异构 的; 也就是说,表内可以包含任何类型的值( nil 除外)。
任何键的值若为 nil 就不会被记入表结构内部。换言之,对于表内不存在的键,
都对应着值 nil 。
表是 Lua 中唯一的数据结构,它可被用于表示普通数组、序列、符号表、集合、记录、
图、树等等。对于记录,Lua 使用域名作为索引。语言提供了 a.name
这样的语法糖
来替代 a["name"]
这种写法以方便记录这种结构的使用。
在 Lua 中有多种便利的方式创建表。
和索引一样,表中每个域的值也可以是任何类型。需要特别指出的是:既然函数是一等公民,
那么表的域也可以是函数。这样,表就可以携带 方法 了。
索引一张表的原则遵循语言中的直接比较规则。当且仅当 i
与 j
直接比较相等时
(即不通过元方法的比较),表达式 a[i]
与 a[j]
表示了表中相同的元素。
无论何时,若一个操作需要取表的长度,这张表必须是一个真序列,或是拥有 __len
元方法。
所有的函数都忽略传入参数的那张表中的非数字键。
表处理库提供了表处理的通用函数,所有函数都放在表 table
中。
将给定函数应用于列表的每个元素。
返回由各元素(该函数返回了非nil/false)的结果组成的列表
t | table |
fn | fun(v:any):any |
对列表的每个元素应用给定函数。连接所有结果并返回组合列表。
t | table |
fn | fun(v:any):table |
提供一个列表,其所有元素都是字符串或数字,返回字符串
list[i]..sep..list[i+1] ··· sep..list[j]
。
sep
的默认值是空串,
i
的默认值是 1 ,
j
的默认值是 #list
。
如果 i
比 j
大,返回空串。
list | table |
sep | string |
i | number |
j | number |
Copy values from source to target with default values.
Example: table.deploy(self, params, { a = 1 })
t | object |
src | table |
default | table |
isclone | boolean @will use table.clone(src) if this is true. |
Dump a table to strings.
Example: for i, line in ipairs(table.dump(t)) do print(line) end
t | table |
description | string |
nesting | number |
返回一个新集合,其中仅包含给定谓词为其返回 true 的集合的元素
t | table |
fn | fun(v:any):boolean |
在 list
的位置 pos
处插入元素 value
,
并后移元素 list[pos], list[pos+1], ···, list[#list]
。
pos
的默认值为 #list+1
,
因此调用 table.insert(t,x)
会将 x
插在列表 t
的末尾。
list | table |
pos | number |
value | any |
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.)
table | table |
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.
移除 list
中 pos
位置上的元素,并返回这个被移除的值。
当 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
的最后一个元素。
list | table |
pos | number |
在表内从 list[1]
到 list[#list]
原地
对其间元素按指定次序排序。
如果提供了 comp
,
它必须是一个可以接收两个列表内元素为参数的函数。
当第一个元素需要排在第二个元素之前时,返回真
(因此 not comp(list[i+1],list[i])
在排序结束后将为真)。
如果没有提供 comp
,
将使用标准 Lua 操作 <
作为替代品。
排序算法并不稳定;
即当两个元素次序相等时,它们在排序后的相对位置可能会改变。
@generic V
list | table<number, V> |
comp | fun(a:V, b:V):number |
Copy values from source to target when key is missing.
t | object |
src | table |