Parcourir la source

cleaner interface to `ltable'

Roberto Ierusalimschy il y a 20 ans
Parent
commit
65726f3e2e
6 fichiers modifiés avec 44 ajouts et 32 suppressions
  1. 2 2
      lapi.c
  2. 2 2
      lparser.c
  3. 3 3
      lstate.c
  4. 31 19
      ltable.c
  5. 2 2
      ltable.h
  6. 4 4
      lvm.c

+ 2 - 2
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.22 2004/12/06 17:53:42 roberto Exp roberto $
+** $Id: lapi.c,v 2.23 2004/12/13 12:15:11 roberto Exp $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -542,7 +542,7 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
   lua_lock(L);
   luaC_checkGC(L);
-  sethvalue(L, L->top, luaH_new(L, narray, luaO_log2(nrec) + 1));
+  sethvalue(L, L->top, luaH_new(L, narray, nrec));
   api_incr_top(L);
   lua_unlock(L);
 }

+ 2 - 2
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 2.10 2004/12/03 20:50:25 roberto Exp roberto $
+** $Id: lparser.c,v 2.11 2004/12/07 18:31:16 roberto Exp $
 ** Lua Parser
 ** See Copyright Notice in lua.h
 */
@@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) {
   check_match(ls, '}', '{', line);
   lastlistfield(fs, &cc);
   SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
-  SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1);  /* set initial table size */
+  SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh+1));  /* set initial table size */
 }
 
 /* }====================================================================== */

+ 3 - 3
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.18 2004/12/06 17:53:42 roberto Exp roberto $
+** $Id: lstate.c,v 2.19 2004/12/13 12:15:11 roberto Exp $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -87,9 +87,9 @@ static void f_luaopen (lua_State *L, void *ud) {
   setbit(u->uv.marked, FIXEDBIT);
   setbit(L->marked, FIXEDBIT);
   stack_init(L, L);  /* init stack */
-  sethvalue(L, gt(L), luaH_new(L, 0, 4));  /* table of globals */
+  sethvalue(L, gt(L), luaH_new(L, 0, 20));  /* table of globals */
   hvalue(gt(L))->metatable = luaH_new(L, 0, 0);  /* globals metatable */
-  sethvalue(L, registry(L), luaH_new(L, 4, 4));  /* registry */
+  sethvalue(L, registry(L), luaH_new(L, 6, 20));  /* registry */
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
   luaT_init(L);
   luaX_init(L);

+ 31 - 19
ltable.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 2.11 2004/12/03 20:50:25 roberto Exp roberto $
+** $Id: ltable.c,v 2.12 2004/12/04 18:10:22 roberto Exp $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -197,7 +197,18 @@ static void computesizes  (int nums[], int ntotal, int *narray, int *nhash) {
 }
 
 
-static void numuse (const Table *t, int *narray, int *nhash) {
+static int countint (const TValue *key, int *nums) {
+  int k = arrayindex(key);
+  if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */
+    nums[luaO_log2(k-1)+1]++;  /* count as such */
+    return 1;
+  }
+  else
+    return 0;
+}
+
+
+static void numuse (const Table *t, int *narray, int *nhash, const TValue *ek) {
   int nums[MAXBITS+1];
   int i, lg;
   int totaluse = 0;
@@ -223,14 +234,13 @@ static void numuse (const Table *t, int *narray, int *nhash) {
   while (i--) {
     Node *n = &t->node[i];
     if (!ttisnil(gval(n))) {
-      int k = arrayindex(key2tval(n));
-      if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */
-        nums[luaO_log2(k-1)+1]++;  /* count as such */
-        (*narray)++;
-      }
+      *narray += countint(key2tval(n), nums);
       totaluse++;
     }
   }
+  /* count extra key */
+  *narray += countint(ek, nums);
+  totaluse++;
   computesizes(nums, totaluse, narray, nhash);
 }
 
@@ -268,7 +278,7 @@ static void setnodevector (lua_State *L, Table *t, int lsize) {
 }
 
 
-void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
+static void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
   int i;
   int oldasize = t->sizearray;
   int oldhsize = t->lsizenode;
@@ -310,9 +320,16 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
 }
 
 
-static void rehash (lua_State *L, Table *t) {
+void luaH_resizearray (lua_State *L, Table *t, int nasize) {
+  luaH_resize(L, t, nasize, t->lsizenode);
+}
+
+
+static void rehash (lua_State *L, Table *t, const TValue *ek) {
   int nasize, nhsize;
-  numuse(t, &nasize, &nhsize);  /* compute new sizes for array and hash parts */
+  /* compute new sizes for array and hash parts */
+  numuse(t, &nasize, &nhsize, ek);
+  /* resize the table to new computed sizes */
   luaH_resize(L, t, nasize, luaO_log2(nhsize)+1);
 }
 
@@ -323,7 +340,7 @@ static void rehash (lua_State *L, Table *t) {
 */
 
 
-Table *luaH_new (lua_State *L, int narray, int lnhash) {
+Table *luaH_new (lua_State *L, int narray, int nhash) {
   Table *t = luaM_new(L, Table);
   luaC_link(L, obj2gco(t), LUA_TTABLE);
   t->metatable = NULL;
@@ -334,7 +351,7 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) {
   t->lsizenode = 0;
   t->node = NULL;
   setarrayvector(L, t, narray);
-  setnodevector(L, t, lnhash);
+  setnodevector(L, t, luaO_log2(nhash)+1);
   return t;
 }
 
@@ -356,7 +373,6 @@ void luaH_free (lua_State *L, Table *t) {
 ** position), new key goes to an empty position. 
 */
 static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
-  TValue *val;
   Node *mp = luaH_mainposition(t, key);
   if (!ttisnil(gval(mp))) {  /* main position is not free? */
     /* `mp' of colliding node */
@@ -387,12 +403,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
     else (t->firstfree)--;
   }
   /* no more free places; must create one */
-  setbvalue(gval(mp), 0);  /* avoid new key being removed */
-  rehash(L, t);  /* grow table */
-  val = cast(TValue *, luaH_get(t, key));  /* get new position */
-  lua_assert(ttisboolean(val));
-  setnilvalue(val);
-  return val;
+  rehash(L, t, key);  /* grow table */
+  return luaH_set(L, t, key);  /* re-insert in new table */
 }
 
 

+ 2 - 2
ltable.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.h,v 2.2 2004/03/26 14:02:41 roberto Exp roberto $
+** $Id: ltable.h,v 2.3 2004/10/06 18:34:16 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -25,7 +25,7 @@ TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
 const TValue *luaH_get (Table *t, const TValue *key);
 TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
 Table *luaH_new (lua_State *L, int narray, int lnhash);
-void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
+void luaH_resizearray (lua_State *L, Table *t, int nasize);
 void luaH_free (lua_State *L, Table *t);
 int luaH_next (lua_State *L, Table *t, StkId key);
 

+ 4 - 4
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 2.17 2004/11/01 15:06:50 roberto Exp roberto $
+** $Id: lvm.c,v 2.18 2004/12/03 20:35:33 roberto Exp $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -461,8 +461,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
       }
       case OP_NEWTABLE: {
         int b = GETARG_B(i);
-        b = luaO_fb2int(b);
-        sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
+        int c = GETARG_C(i);
+        sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c) - 1));
         L->ci->savedpc = pc;
         luaC_checkGC(L);  /***/
         base = L->base;
@@ -723,7 +723,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
         if (c == 0) c = cast(int, *pc++);
         last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
         if (last > h->sizearray)  /* needs more space? */
-          luaH_resize(L, h,  last, h->lsizenode);  /* pre-alloc it at once */
+          luaH_resizearray(L, h, last);  /* pre-alloc it at once */
         for (; n > 0; n--) {
           TValue *val = ra+n;
           setobj2t(L, luaH_setnum(L, h, last--), val);