zoukankan      html  css  js  c++  java
  • lua 取table长度

    http://blog.csdn.net/wangmanjie/article/details/52793902

    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必须是整形才能用#取。

  • 相关阅读:
    怎样简单编写一个html网页
    C# 委托实现冒泡排序
    C# 运算符
    EF 多表联查方法
    Log4net 配置文件
    vs调试 iis发布之后的项目
    继承 ,构造方法使用
    C#扩展方法
    partial 部分类
    WeakReference 弱引用
  • 原文地址:https://www.cnblogs.com/freebird92/p/6759029.html
Copyright © 2011-2022 走看看