原文:http://www.2cto.com/kf/201501/370498.html
首先要明确的是lua中有两部分:数组部分和hash表部分。而基本上所有操作都是先数组后hash表。
local test1 = { 1 , 2 , 3 , 4 , 5 }
print(#test1)
打印结果: 5
local test1 = { 1, 3 , 5 , 2 , 4 }
print(#test1)
打印结果: 5 (好吧。。。。当然跟上面一样,都是作为数组中的值。。。)
local test1 = {[1] = 1 , [2] = 2 , [3] = 3 , [4] = 4 ,[5] = 5}
print(#test1)
打印结果: 5 (这里table中没有数组部分了,只有hash表部分)
local test1 = {[1] = 1 , [3] = 3 , [4] = 4 , [6] = 6 ,[2] = 2}
print(#test1)
打印结果: 6
for i = 1 , #test1 do
print(test1[i])
end
如果全部打印出来, 1 2 3 4 nil 6
明明写的table中只有5个元素,怎么会变成6那。。。。这里的原因就要看下lua源码实现
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
*/
int luaH_getn (Table *t) {
unsigned int j = t->sizearray;
if (j > 0 && ttisnil(&t->array[j - 1])) {
/* there is a boundary in the array part: (binary) search for it */
unsigned int i = 0;
while (j - i > 1) {
unsigned int m = (i+j)/2;
if (ttisnil(&t->array[m - 1])) j = m;
else i = m;
}
return i;
}
/* else must find a boundary in hash part */
else if (isdummy(t->node)) /* hash part is empty? */
return j; /* that is easy... */
else return unbound_search(t, j);
}
还是先数组,数组没有后hash部分。再来看下关于hash表部分的取长度
static int unbound_search (Table *t, unsigned int j) {
unsigned int i = j; /* i is zero or a present index */
j++;
/* find `i' and `j' such that i is present and j is not */
while (!ttisnil(luaH_getint(t, j))) {
i = j;
j *= 2;
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
/* table was built with bad purposes: resort to linear search */
i = 1;
while (!ttisnil(luaH_getint(t, i))) i++;
return i - 1;
}
}
/* now do a binary search between them */
while (j - i > 1) {
unsigned int m = (i+j)/2;
if (ttisnil(luaH_getint(t, m))) j = m;
else i = m;
}
return i;
}
j++保证j是hash部分的第一个值,从j开始,如果j位置是有值的,那么将j扩大两倍,再检查两倍之后hash表中是否可以取到值,直到找到没有值的地方,这个值就在i 到 j这个区间中。然后再用折半查找找到 i 到 j之间找到的最后一个nil的,前面的就是它的长度了。 错略看来。luaH_getint用来取值
const TValue *luaH_getint (Table *t, int key)而它的声明看来 ,第二个参数是key,通过key来取value, 而外面对传入的key是++的操作 可知计算长度用来寻找的这个key一定是个整形,而且还得是连续的(不一定)。(当然这个是不深究细节实现错略看下来的分析。。。。。)
再来验证下:
local test1 = {1 , 3 , [4] = 4 , [6] = 6 ,[2] = 2}
print(#test1)
打印结果: 2
也就是上面源码中,会先遍历数组部分,数组部分有就结束,没有再遍历hash表部分
local test1 = {[4] = 4 , [6] = 6 ,[2] = 2}
print(#test1)
打印结果:0
数组之后的第一位是j++ 如果value是nil, i 是 0 ,j 是1 返回值是0
看两个一起的:
local test1 = {[1] = 1 , [2] = 2 ,[4] = 4 ,[6] = 6}
print(#test1)
local test1 = {[1] = 1, [2] = 2 ,[5] = 5 ,[6] = 6}
print(#test1)
两个的输出结果是6和2 ,而且要是将第一个打印出来 是1 2 3 4 nil 6 中间差一个就能打出来后面的,差两个就不行了 why?
就是因为上面源码中得算法。
举个例子
local test1 = {[1] = 1 , [2] = 2, [3] = 3 ,[4] = 4 ,[6] = 6}
第一个while循环结束, i == 4 ,j == 8, 通过下面的折半查找(具体细节还是拿笔算下吧。。。) 最后i == 6了
而local test1 = {[1] = 1, [2] = 2 ,[5] = 5 ,[6] = 6}
第一个while循环后, i == 2 , j == 4 , 折半查找后 i == 2
恩 就是这样了,如果不清楚这个的话,那么在实际操作的时候,会遇到很奇怪的问题而浪费大量时间。。。。
最后local test1 = { [‘a’] = 1, [‘b’] = 2 ,[‘c’] = 3}
print(#test1)
打印结果: 0 key必须是整形才能用#取。
其他取数组长度形式
如果是字符串或者其他形式的,还是采用循环pairs这种形式去取为好