LUA源码分析三:table分析(1)

来源:互联网 发布:ipad pro毛笔字软件 编辑:程序博客网 时间:2024/05/21 11:35

http://lin-style.iteye.com/blog/976945


版本整理日期:2011/3/27

分析函数:getn,tinsert 

 

table在里面数据方式比较直观,但是算法很复杂。一些算法的坑会慢慢补上。

先总括下table的数据结构:

1)由一个hash表和一个数组构成,当插入一个小标元素,会根据当前数组的大小决定插入哪儿

2)hash表上会有冲突,通过一个链表的形式组织冲突的元素

3)通过源码,我们还能得到的是一些table的使用技巧方式,尤其是在大数据量上的效率开销

4)我在分析的方法上是这样的:首先分析luaH_getn这个函数,通过获取的方式来对table存储的方式有个大致了解;然后分析tinsert,两者进行迭代阅读

 

 

 

Cpp代码  收藏代码
  1. //ltable.c  
  2. int luaH_getn (Table *t) {  
  3.  //array数组个数  
  4.   unsigned int j = t->sizearray;  
  5.   if (j > 0 && ttisnil(&t->array[j - 1])) {  
  6.     /* there is a boundary in the array part: (binary) search for it */  
  7.     unsigned int i = 0;  
  8.     while (j - i > 1) {  
  9.       unsigned int m = (i+j)/2;  
  10.       if (ttisnil(&t->array[m - 1])) j = m;  
  11.       else i = m;  
  12.     }  
  13.     return i;  
  14.   }  
  15.   /* else must find a boundary in hash part */  
  16.   else if (t->node == dummynode)  /* hash part is empty? */  
  17.     return j;  /* that is easy... */  
  18.   else return unbound_search(t, j);  
  19. }  

 

可以看出,对个数的统计方式有3种
1.array个数大于0,并且末尾的元素为空。比如{1,2,3,nil}, 返回值为3
2.array个数大于0,末尾不为空。比如t={1,nil,nil,3},返回为4
3.unbound_search方式的统计.需要2个条件
  1)j<=0, t->node!=dummynode
  2)j>0,t->array[j - 1]不为空,t->node!=dummynode
  
并且还可以得到的信息是,
1.array是个数组,并且元素的排列上是紧密的。
2.即使末尾为空,也算是table的空间,但是返回的个数略有不同
3.array的存放上有一定的阀值,如果超出按链表方式存放
4.统计的方式(while)是个二分的查找

 

 

Cpp代码  收藏代码
  1. //ltable.c  
  2. static int unbound_search (Table *t, unsigned int j) {  
  3.   unsigned int i = j;  /* i is zero or a present index */  
  4.   j++;  
  5.   /* find `i' and `j' such that i is present and j is not */  
  6.   while (!ttisnil(luaH_getnum(t, j))) {  
  7.     i = j;  
  8.     j *= 2;  
  9.     if (j > cast(unsigned int, MAX_INT)) {  /* overflow? */  
  10.       /* table was built with bad purposes: resort to linear search */  
  11.       i = 1;  
  12.       while (!ttisnil(luaH_getnum(t, i))) i++;  
  13.       return i - 1;  
  14.     }  
  15.   }  
  16.   /* now do a binary search between them */  
  17.   while (j - i > 1) {  
  18.     unsigned int m = (i+j)/2;  
  19.     if (ttisnil(luaH_getnum(t, m))) j = m;  
  20.     else i = m;  
  21.   }  
  22.   return i;  
  23. }  
 

 

 

观察第一个while,有2种情况

1.对j进行判断,i等于j扩大两倍前的值。如果luaH_getnum取到的值为空,则退出循环

2.如果j扩大两倍大于MAX_INT时,则以i从1开始,线性的查找。(是否可以优化为从j/4开始?)


经过第一个while后,取到下面的情况。

有值,有值,有值(i在这),有值(下一轮while要查找的目标),空值,空值(j可能在这)

继续二分查找,找到最末尾有值的下标。然后返回。


那么这个函数的功能就可以总结为,先从1-MAX_INT中探测到一个范围,然后从这个范围中查找

末尾的有值节点


继续跟入luaH_getnum

 

 

Cpp代码  收藏代码
  1. //ltable.c  
  2. /* 
  3. ** search function for integers 
  4. */  
  5. const TValue *luaH_getnum (Table *t, int key) {  
  6.   /* (1 <= key && key <= t->sizearray) */  
  7.   /* 
  8.     cast很熟悉了,值转换. 
  9.     从这个判断可以得到信息是sizearray可以存储一定量的数据, 
  10.     如果超出则用其他方式存储 
  11.   */  
  12.   if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))  
  13.     return &t->array[key-1];  
  14.   else {  
  15.     lua_Number nk = cast_num(key);  
  16.     Node *n = hashnum(t, nk);  
  17.     do {  /* check whether `key' is somewhere in the chain */  
  18.         /* 
  19.             这里也预读到一些信息 
  20.             1.基本上不是为了出错校验 
  21.             2.看来是和插入的算法有关。插入的算法可能会导致一些重复 
  22.             3.能存多少数据,跟nk的类型有关。虽然nk是double,但是小数能否用到 
  23.                 还得继续跟。否则就是一个2^32的总数了 
  24.         */  
  25.       if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))  
  26.         return gval(n);  /* that's it */  
  27.       else n = gnext(n);  
  28.     } while (n);  
  29.     return luaO_nilobject;  
  30.   }  
  31. }  
 

 

 

else段里走的流程大概是,根据nk从hashnum里面取到Node,然后进行判断,如果不符合则next个Node.

看来hashnum是个算法。先把gkey,nvalue,luai_numeq几个宏和Node结构体拆开看看

 

 

Cpp代码  收藏代码
  1. //ltable.c  
  2. #define gkey(n)     (&(n)->i_key.nk)  
  3. #define gval(n)     (&(n)->i_val)  
  4. #define gnext(n)    ((n)->i_key.nk.next)  
  5. //luaconf.h  
  6. #define luai_numeq(a,b)     ((a)==(b))  
  7. //lobject.h  
  8. #define nvalue(o)   check_exp(ttisnumber(o), (o)->value.n)  
  9.   
  10. //lobject.h  
  11. #define TValuefields    Value value; int tt  
  12.   
  13. typedef union TKey {  
  14.   struct {  
  15.     TValuefields;  
  16.     struct Node *next;  /* for chaining */  
  17.   } nk;  
  18.   TValue tvk;  
  19. } TKey;  
  20.   
  21.   
  22. typedef struct Node {  
  23.   TValue i_val;  
  24.   TKey i_key;  
  25. } Node;  
 

TValuefields;其实也是个TValue的结构体。用宏扩展开,可以少一层访问的封装。或者可以看成是

TValue多加了个Node *Next,这样可以保证和ttisnumber兼容访问。有了这些,那么可以对这句重新

认识

 

Cpp代码  收藏代码
  1. //hashnum表示一种算法,取到目标节点  
  2. Node *n = hashnum(t, nk);  
  3. do  
  4. {  
  5.       
  6.     /* 
  7.         算法导致的节点可能会多个,则根据key里的n来和nk校验 
  8.         这个就很好理解了,比如1-100算到的因子都是20。20这个node下可能挂了100个节点。然后 
  9.         依次比较取出正确的。 
  10.     */  
  11.     if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))  
  12. }while(n)  

 

最后来看下hashnum算法

 

Cpp代码  收藏代码
  1. //lobject.h  
  2. #define twoto(x)    (1<<(x))  
  3. #define sizenode(t) (twoto((t)->lsizenode))  
  4.   
  5. //ltable.h  
  6. #define gnode(t,i)  (&(t)->node[i])  
  7.   
  8. //ltable.c  
  9. /* 
  10. ** for some types, it is better to avoid modulus by power of 2, as 
  11. ** they tend to have many 2 factors. 
  12. */  
  13. /* 
  14. 算法比想象中的简单。sizenode对table里的lsizenode取一个指数,在源代码也能找到这段注释: 
  15. lu_byte lsizenode;  /* log2 of size of `node' array */  
  16. -1表示0~(1<<(x)-1)范围,|1没弄明白什么意思。防止出现小于0的吗?  
  17. hashmod仅仅是为了大下标的处理。   
  18. */  
  19.   
  20. #define hashmod(t,n)    (gnode(t, ((n) % ((sizenode(t)-1)|1))))  

 

 

 

Cpp代码  收藏代码
  1. //跟调do..while里面的hashnum函数  
  2. static Node *hashnum (const Table *t, lua_Number n) {  
  3.   unsigned int a[numints];  
  4.   int i;  
  5.   if (luai_numeq(n, 0))  /* avoid problems with -0 */  
  6.     return gnode(t, 0);  
  7.   /* 
  8.     lua_Number是个double型,这里是把n的高低4个字节相加, 
  9.     作为去模的对象   
  10.   */  
  11.   memcpy(a, &n, sizeof(a));  
  12.   //#define numints     cast_int(sizeof(lua_Number)/sizeof(int))  
  13.   for (i = 1; i < numints; i++) a[0] += a[i];  
  14.   return hashmod(t, a[0]);  
  15. }  

 


 

那么luaH_getnum这个函数就很好理解了。首先判断传入的key是否满足当前sizearray,如果满足则返回。

否则可以看成一个大下标的存储,根据hashmod算法取到节点。

再回头总结下调用的源码文件架构
ltablib.c(getn)
ltablib.c(aux_getn)
lauxlib.h(luaL_getn)
lapi.c(lua_objlen)
lobject.h(hvalue)//hvalue很奇怪,从gc中取到table结构
ltable.c(luaH_getn)
   
另起段
ltable.c(luaH_getn)
lobject.h(ttisnil)
ltable.c(unbound_search)
lobject.h(ttisnil)
ltable.c(luaH_getnum)
limits.h(cast_num,cast)
ltable.h(gkey,gval,gnext)
ltable.c(hashnum)
ltable.c(hashmod)
ltable.h(gnode)
lobject.h(sizenode)
ltable.h(gnode)
luaconf.h(luai_numeq)
   
总结下第一回分析getn所得到的信息
1.table存储的方式有array,node
2.nil也算是一个元素(如果不是在末尾),并且常用二分法查找
3.node是通过一个取模方式,并且会重复。 

接着继续分析tinsert函数
Cpp代码  收藏代码
  1. //ltablib.c  
  2. static int tinsert (lua_State *L) {  
  3.     //调用的是getn,取回table的元素总数。  
  4.   imt e = aux_getn(L, 1) + 1;  /* first empty element */  
  5.   int pos;  /* where to insert new element */  
  6.   switch (lua_gettop(L)) {  
  7.     case 2: {  /* called with only 2 arguments */  
  8.       pos = e;  /* insert new element at the end */  
  9.       break;  
  10.     }  
  11.     /* 
  12.         如果插入的范围小于当前元素总数,则移出空位 
  13.     */  
  14.     case 3: {  
  15.       int i;  
  16.       pos = luaL_checkint(L, 2);  /* 2nd argument is the position */  
  17.       if (pos > e) e = pis;  /* `grow' array if necessary */  
  18.       for (i = e; i > pis; i--) {  /* move up elements */  
  19.         lua_rawgeti(L, 1, i+1);  
  20.         lua_rawseti(L, 1, i);  /* t[i] = t[i-1] */  
  21.       }  
  22.       break;  
  23.     }  
  24.     default: {  
  25.       return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));  
  26.     }  
  27.   }  
  28.   luaL_setn(L, 1, e);  /* new size */  
  29.   lua_rawseti(L, 1, pos);  /* t[pos] = v */  
  30.   return 0;  
  31. }  
 tinsert函数很简单,重点跟调lua_rawseti

Cpp代码  收藏代码
  1. //lapi.c  
  2. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {  
  3.   StkId o;  
  4.   lua_lock(L);  
  5.   api_checknelems(L, 1);  
  6.   o = index2adr(L, idx);  
  7.   api_check(L, ttistable(o));  
  8.   /* 
  9.     L->top上的排列为 table, 插入的位置,插入的值. 
  10.     L->top-1不知道取的啥值,略过。跟调发现取的是插入的值。 
  11.     luaH_setnum的名字比较奇怪,按道理叫luaH_getnum比较合适,不过也间接 
  12.     的透露了信息, 
  13.     1.首先要确保空间节点的存在 
  14.     2.对于空间必然有一个扩展的操作,而不是预先安排 
  15.   */  
  16.   setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);  
  17.   //这段先不管  
  18.   luaC_barriert(L, hvalue(o), L->top-1);  
  19.   //栈减1  
  20.   L->top--;  
  21.   lua_unlock(L);  
  22. }  
 
继续走luaH_setnum(L, hvalue(o), n)

Cpp代码  收藏代码
  1. //ltable.c  
  2. //这个函数叫setnum,  
  3. TValue *luaH_setnum (lua_State *L, Table *t, int key) {  
  4.     /* 
  5.         看到了吧,会先尝试一个获取。取不到则进行扩展key的节点 
  6.         luaH_getnum前面分析过了,这里略过 
  7.     */  
  8.   const TValue *p = luaH_getnum(t, key);  
  9.   if (p != luaO_nilobject)  
  10.     return cast(TValue *, p);  
  11.   else {  
  12.     TValue k;  
  13.     setnvalue(&k, cast_num(key));  
  14.     return newkey(L, t, &k);  
  15.   }  
  16. }  
 也很简单,继续走newkey

Cpp代码  收藏代码
  1. //ltable.c  
  2. /* 
  3. ** inserts a new key into a hash table; first, check whether key's main  
  4. ** position is free. If not, check whether colliding node is in its main  
  5. ** position or not: if it is not, move colliding node to an empty place and  
  6. ** put new key in its main position; otherwise (colliding node is in its main  
  7. ** position), new key goes to an empty position.  
  8. */  
  9. /* 
  10.     有点悲剧,这个函数看来比较复杂,否则也不会这么多注释。段首所透露的信息大概如下: 
  11.     1.key所对应的hash节点可能存在可能不存在(也就是冲突) 
  12.     2.如果存在,那么有2个点需要互斥:main position、an empty place。暂时不知道 
  13.       这2个所指什么意思 
  14.     3.通过几个if not,otherwise可知,main position只能有一个key占着,剩下的要去 
  15.       an empty place 
  16.     4.虽然key所对应的hash节点在,但是main position不一定有key. 
  17. */  
  18. static TValue *newkey (lua_State *L, Table *t, const TValue *key) {  
  19.   /* 
  20.     又冒了一个mainposition出来,跟调一下发现是通过hash算法得到的节点 
  21.     上文跟过,不细展开了 
  22.   */  
  23.   Node *mp = mainposition(t, key);  
  24.   /* 
  25.     !ttisnil(gval(mp))表示一个新的节点, 
  26.     mp == dummynode同上。 
  27.     先解决if之外的。之外的意思很简单,把值给新得到的节点。 
  28.     对应Node结构体查看 
  29.         typedef struct Node { 
  30.           TValue i_val;   
  31.           TKey i_key;   //把key记录在i_key结构里边 
  32.         } Node; 
  33.              
  34.         继续刨下面难的那段 
  35.   */  
  36.   if (!ttisnil(gval(mp)) || mp == dummynode) {  
  37.     Node *othern;  
  38.     /* 
  39.         getfreepos里面操作的是t->lastfree值,怎么来的不知道。 
  40.         只知道也是个指针数组       
  41.     */  
  42.     Node *n = getfreepos(t);  /* get a free place */  
  43.     if (n == NULL) {  /* cannot find a free place? */  
  44.         /* 
  45.             如果为空,那么rehash一个值,这个值相对比较NB, 
  46.             不然luaH_set又要执行newkey。我把这段放到了后面,大家 
  47.             可以先跳到后面1.1 
  48.         */    
  49.       rehash(L, t, key);  /* grow table */  
  50.       return luaH_set(L, t, key);  /* re-insert key into grown table */  
  51.     }  
  52.     lua_assert(n != dummynode);  
  53.     /* 
  54.         第一个if可以反过来分析: 
  55.         最终目的是把mp赋给n 
  56.         n是作为一个末尾节点。说明这是一个链表结构 
  57.         othern节点是通过key2tval宏取得的,该宏扩展开来&(n)->i_key.tvk。可以看成是key 
  58.         的value值。 
  59.         while()语句可以得出一个互斥信息:通过key算出来的mp是同个节点,因此把一样的节点 
  60.         串起来。 
  61.     */  
  62.     othern = mainposition(t, key2tval(mp));  
  63.     if (othern != mp) {  /* is colliding node out of its main position? */  
  64.       /* yes; move colliding node into free position */  
  65.       while (gnext(othern) != mp) othern = gnext(othern);  /* find previous */  
  66.       gnext(othern) = n;  /* redo the chain with `n' in place of `mp' */  
  67.       *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */  
  68.       gnext(mp) = NULL;  /* now `mp' is free */  
  69.       setnilvalue(gval(mp));  
  70.     }  
  71.     /* 
  72.         这段就很简单了,说明当前还没有插入其他的相同算法key, 
  73.         则n做为链表头 
  74.     */  
  75.     else {  /* colliding node is in its own main position */  
  76.       /* new node will go into free position */  
  77.       gnext(n) = gnext(mp);  /* chain new position */  
  78.       gnext(mp) = n;  
  79.       mp = n;  
  80.     }  
  81.   }  
  82.   gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;  
  83.   luaC_barriert(L, t, key);  
  84.   lua_assert(ttisnil(gval(mp)));  
  85.   return gval(mp);  
  86. }  
 
Cpp代码  收藏代码
  1. /* 
  2.     这段函数算法可以暂时不分析得非常非常细致。我们可以先用黑盒测试它的一些行为。 
  3.     比如用insert插入的两次下标不在同个位置,那么getn得到的是0;如果插入的位置是序列 
  4.     的,那么rehash会认为是排列紧密的,getn得到是2。可以看出,这个函数主要是个重新 
  5.     归纳和分配节点算法的东西。后面留个坑,慢慢补。那么table中插入元素的排列位置如下图: 
  6. */  
  7. static void rehash (lua_State *L, Table *t, const TValue *ek) {  
  8.   int nasize, na;  
  9.   int nums[MAXBITS+1];  /* nums[i] = number of keys between 2^(i-1) and 2^i */  
  10.   int i;  
  11.   int totaluse;  
  12.   //MAXBITS为 26。清零  
  13.   for (i=0; i<=MAXBITS; i++) nums[i] = 0;  /* reset counts */  
  14.     //小段1.1.1,计算这个数组中已有的元素个数  
  15.   nasize = numusearray(t, nums);  /* count keys in array part */  
  16.   totaluse = nasize;  /* all those keys are integer keys */  
  17.   //小段1.1.2,计算已使用的hash节点个数  
  18.   totaluse += numusehash(t, nums, &nasize);  /* count keys in hash part */  
  19.   /* count extra key */  
  20.   //小段1.1.3,调用的核心算法也是hash的一种  
  21.   nasize += countint(ek, nums);  
  22.   totaluse++;  
  23.   /* compute new size for array part */  
  24.   //小段1.1.4,没看太懂,只是做了个黑盒测试。会根据下标重算一个大小  
  25.   na = computesizes(nums, &nasize);  
  26.   /* resize the table to new computed sizes */  
  27.   //小段1.1.5,这个版本留坑。第一次table目的已达到  
  28.   resize(L, t, nasize, totaluse - na);  
  29. }  
 
Cpp代码  收藏代码
  1. /* 
  2.     1.1.1 
  3.     先观察退出的条件:i>lim, lim为ttlg,ttlg变化范围是个2^lg, 
  4.     i是个ttlg叠加的和,包括空和非空的元素. 
  5.     而当i>lim时,lim的值实际上是t->sizearray。这个思路和上文的while 
  6.     很像,都是一个预加的试探方法。当ttlg叠加的和大于t->sizearray,说明已经统计完毕。 
  7. */  
  8. static int numusearray (const Table *t, int *nums) {  
  9.   int lg;  
  10.   int ttlg;  /* 2^lg */  
  11.   int ause = 0;  /* summation of `nums' */  
  12.   int i = 1;  /* count to traverse all array keys */  
  13.   for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) {  /* for each slice */  
  14.     int lc = 0;  /* counter */  
  15.     int lim = ttlg;  
  16.     if (lim > t->sizearray) {  
  17.       lim = t->sizearray;  /* adjust upper limit */  
  18.       if (i > lim)  
  19.         break;  /* no more elements to count */  
  20.     }  
  21.     /* count elements in range (2^(lg-1), 2^lg] */  
  22.     for (; i <= lim; i++) {  
  23.       if (!ttisnil(&t->array[i-1]))  
  24.         lc++;  
  25.     }  
  26.     //lg,表示对应的下标  
  27.     nums[lg] += lc;  
  28.     ause += lc;  
  29.   }  
  30.   return ause;  
  31. }  
 
Cpp代码  收藏代码
  1. /* 
  2.     1.1.2 
  3.     返回值totaluse,表示非空的t->node数 
  4.     ause,通过countint统计 
  5.      
  6. */  
  7. static int numusehash (const Table *t, int *nums, int *pnasize) {  
  8.   int totaluse = 0;  /* total number of elements */  
  9.   int ause = 0;  /* summation of `nums' */  
  10.   //#define sizenode(t) (twoto((t)->lsizenode))  
  11.   int i = sizenode(t);  
  12.   while (i--) {  
  13.     Node *n = &t->node[i];  
  14.     if (!ttisnil(gval(n))) {  
  15.         //#define key2tval(n)   (&(n)->i_key.tvk)  
  16.         //countint见1.1.2.1  
  17.       ause += countint(key2tval(n), nums);  
  18.       totaluse++;  
  19.     }  
  20.   }  
  21.   *pnasize += ause;  
  22.   return totaluse;  
  23. }  
 

Cpp代码  收藏代码
  1. /* 
  2.     1.1.2.1 / 1.1.3 
  3. */  
  4. static int countint (const TValue *key, int *nums) {  
  5.   int k = arrayindex(key);  
  6.   if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */  
  7.     nums[ceillog2(k)]++;  /* count as such */  
  8.     return 1;  
  9.   }  
  10.   else  
  11.     return 0;  
  12. }  
  13.   
  14. /* 
  15. ** returns the index for `key' if `key' is an appropriate key to live in 
  16. ** the array part of the table, -1 otherwise. 
  17. */  
  18. //没看太懂,为了浮点数偏差比较吗?  
  19. static int arrayindex (const TValue *key) {  
  20.   if (ttisnumber(key)) {  
  21.     lua_Number n = nvalue(key);  
  22.     int k;  
  23.     //一条汇编扩展,浮点变整形  
  24.     lua_number2int(k, n);  
  25.     if (luai_numeq(cast_num(k), n))  
  26.       return k;  
  27.   }  
  28.   return -1;  /* `key' did not match some condition */  
  29. }  
  30.   
  31. //ceillog2对应  
  32. //没看懂,略过  
  33. #define ceillog2(x) (luaO_log2((x)-1) + 1)  
  34. int luaO_log2 (unsigned int x) {  
  35.   static const lu_byte log_2[256] = {  
  36.     0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,  
  37.     6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,  
  38.     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  
  39.     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  
  40.     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,  
  41.     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,  
  42.     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,  
  43.     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8  
  44.   };  
  45.   int l = -1;  
  46.   while (x >= 256) { l += 8; x >>= 8; }  
  47.   return l + log_2[x];  
  48.   
  49. }  
 
1.1.3,1.1.4的代码就不贴了。作为第一次分析table,已经满足了。剩下都是算法的坑。首先要做的是去搜索有没有这种算法名,借助外部的资料来学习算法更有效。
0 0
原创粉丝点击