您的位置:首页 > 编程语言 > Lua

lua里 table的长度#table 的计算方法

2013-03-14 16:36 309 查看
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);
}


先判断最后一个元素是不是nil, 若不是则直接返回, 否则用2分查找查到不是nil的元素,并返回当前个数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: