specialized versions for luaH_set (numbers and strings)

This commit is contained in:
Roberto Ierusalimschy 2001-01-10 16:56:11 -02:00
parent 08496eea8b
commit dabb19fc17
5 changed files with 92 additions and 87 deletions

6
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.114 2000/12/28 12:55:41 roberto Exp roberto $ ** $Id: lapi.c,v 1.115 2001/01/10 17:41:50 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -267,7 +267,7 @@ LUA_API void lua_gettable (lua_State *L, int index) {
LUA_API void lua_rawget (lua_State *L, int index) { LUA_API void lua_rawget (lua_State *L, int index) {
StkId t = Index(L, index); StkId t = Index(L, index);
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1); *(L->top - 1) = *luaH_get(hvalue(t), L->top - 1);
} }
@ -338,7 +338,7 @@ LUA_API void lua_rawset (lua_State *L, int index) {
LUA_API void lua_rawseti (lua_State *L, int index, int n) { LUA_API void lua_rawseti (lua_State *L, int index, int n) {
StkId o = Index(L, index); StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
*luaH_setint(L, hvalue(o), n) = *(L->top-1); *luaH_setnum(L, hvalue(o), n) = *(L->top-1);
L->top--; L->top--;
} }

4
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.111 2000/12/28 12:55:41 roberto Exp roberto $ ** $Id: ldo.c,v 1.112 2001/01/10 16:58:11 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -332,7 +332,7 @@ struct lua_longjmp {
static void message (lua_State *L, const char *s) { static void message (lua_State *L, const char *s) {
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE); const TObject *em = luaH_getstr(L->gt, luaS_newliteral(L, LUA_ERRORMESSAGE));
if (ttype(em) == LUA_TFUNCTION) { if (ttype(em) == LUA_TFUNCTION) {
*L->top = *em; *L->top = *em;
incr_top; incr_top;

128
ltable.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 1.61 2000/12/22 16:57:46 roberto Exp roberto $ ** $Id: ltable.c,v 1.62 2000/12/28 12:55:41 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,7 +23,6 @@
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h" #include "lstate.h"
#include "lstring.h"
#include "ltable.h" #include "ltable.h"
@ -31,6 +30,9 @@
#define TagDefault LUA_TTABLE #define TagDefault LUA_TTABLE
#define hashnum(t,n) (&t->node[(luint32)(lint32)(n)&(t->size-1)])
#define hashstr(t,str) (&t->node[(str)->u.s.hash&(t->size-1)])
/* /*
** returns the `main' position of an element in a table (that is, the index ** returns the `main' position of an element in a table (that is, the index
@ -40,11 +42,9 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
luint32 h; luint32 h;
switch (ttype(key)) { switch (ttype(key)) {
case LUA_TNUMBER: case LUA_TNUMBER:
h = (luint32)(lint32)nvalue(key); return hashnum(t, nvalue(key));
break;
case LUA_TSTRING: case LUA_TSTRING:
h = tsvalue(key)->u.s.hash; return hashstr(t, tsvalue(key));
break;
case LUA_TUSERDATA: case LUA_TUSERDATA:
h = IntPoint(tsvalue(key)); h = IntPoint(tsvalue(key));
break; break;
@ -57,31 +57,26 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
default: default:
return NULL; /* invalid key */ return NULL; /* invalid key */
} }
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
"a&(x-1) == a%x, for x power of 2");
return &t->node[h&(t->size-1)]; return &t->node[h&(t->size-1)];
} }
static const TObject *luaH_getany (lua_State *L, const Hash *t, static const TObject *luaH_getany (const Hash *t, const TObject *key) {
const TObject *key) {
Node *n = luaH_mainposition(t, key); Node *n = luaH_mainposition(t, key);
if (!n) while (n) {
lua_error(L, "table index is nil");
else do {
if (luaO_equalObj(key, &n->key)) if (luaO_equalObj(key, &n->key))
return &n->val; return &n->val;
n = n->next; n = n->next;
} while (n); }
return &luaO_nilobject; /* key not found */ return &luaO_nilobject; /* key not found */
} }
/* specialized version for numbers */ /* specialized version for numbers */
const TObject *luaH_getnum (const Hash *t, lua_Number key) { const TObject *luaH_getnum (const Hash *t, lua_Number key) {
Node *n = &t->node[(luint32)(lint32)key&(t->size-1)]; Node *n = hashnum(t, key);
do { do {
if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key) if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
return &n->val; return &n->val;
n = n->next; n = n->next;
} while (n); } while (n);
@ -91,9 +86,9 @@ const TObject *luaH_getnum (const Hash *t, lua_Number key) {
/* specialized version for strings */ /* specialized version for strings */
const TObject *luaH_getstr (const Hash *t, TString *key) { const TObject *luaH_getstr (const Hash *t, TString *key) {
Node *n = &t->node[key->u.s.hash&(t->size-1)]; Node *n = hashstr(t, key);
do { do {
if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key) if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
return &n->val; return &n->val;
n = n->next; n = n->next;
} while (n); } while (n);
@ -101,11 +96,11 @@ const TObject *luaH_getstr (const Hash *t, TString *key) {
} }
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { const TObject *luaH_get (const Hash *t, const TObject *key) {
switch (ttype(key)) { switch (ttype(key)) {
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key)); case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key)); case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
default: return luaH_getany(L, t, key); default: return luaH_getany(t, key);
} }
} }
@ -115,7 +110,7 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
if (ttype(key) == LUA_TNIL) if (ttype(key) == LUA_TNIL)
i = 0; /* first iteration */ i = 0; /* first iteration */
else { else {
const TObject *v = luaH_get(L, t, key); const TObject *v = luaH_get(t, key);
if (v == &luaO_nilobject) if (v == &luaO_nilobject)
lua_error(L, "invalid key for `next'"); lua_error(L, "invalid key for `next'");
i = (int)(((const char *)v - i = (int)(((const char *)v -
@ -224,30 +219,17 @@ static void rehash (lua_State *L, Hash *t) {
/* /*
** inserts a key into a hash table; first, check whether key is ** inserts a new key into a hash table; first, check whether key's main
** already present; if not, check whether key's main position is free; ** position is free; if not, check whether colliding node is in its main
** if not, check whether colliding node is in its main position or not; ** position or not; if it is not, move colliding node to an empty place and
** if it is not, move colliding node to an empty place and put new key ** put new key in its main position; otherwise (colliding node is in its main
** in its main position; otherwise (colliding node is in its main position), ** position), new key goes to an empty position.
** new key goes to an empty position.
*/ */
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
Node *mp = luaH_mainposition(t, key);
Node *n = mp;
if (!mp)
lua_error(L, "table index is nil");
do { /* check whether `key' is somewhere in the chain */
if (luaO_equalObj(key, &n->key))
return &n->val; /* that's all */
else n = n->next;
} while (n);
/* `key' not found; must insert it */
if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */ if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
Node *othern; /* main position of colliding node */ Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */
n = t->firstfree; /* get a free place */ Node *n = t->firstfree; /* get a free place */
/* is colliding node out of its main position? (can only happens if if (othern != mp) { /* is colliding node out of its main position? */
its position is after "firstfree") */
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
/* yes; move colliding node into free position */ /* yes; move colliding node into free position */
while (othern->next != mp) othern = othern->next; /* find previous */ while (othern->next != mp) othern = othern->next; /* find previous */
othern->next = n; /* redo the chain with `n' in place of `mp' */ othern->next = n; /* redo the chain with `n' in place of `mp' */
@ -273,25 +255,57 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
} }
TObject *luaH_setint (lua_State *L, Hash *t, int key) { static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
TObject index; Node *mp = luaH_mainposition(t, key);
ttype(&index) = LUA_TNUMBER; Node *n = mp;
nvalue(&index) = key; if (!mp)
return luaH_set(L, t, &index); lua_error(L, "table index is nil");
do { /* check whether `key' is somewhere in the chain */
if (luaO_equalObj(key, &n->key))
return &n->val; /* that's all */
else n = n->next;
} while (n);
return newkey(L, t, mp, key); /* `key' not found; must insert it */
} }
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, lua_Number val) { TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
TObject *value, index; TObject kobj;
ttype(&index) = LUA_TSTRING; Node *mp = hashnum(t, key);
tsvalue(&index) = key; Node *n = mp;
value = luaH_set(L, t, &index); do { /* check whether `key' is somewhere in the chain */
ttype(value) = LUA_TNUMBER; if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
nvalue(value) = val; return &n->val; /* that's all */
else n = n->next;
} while (n);
/* `key' not found; must insert it */
ttype(&kobj) = LUA_TNUMBER;
nvalue(&kobj) = key;
return newkey(L, t, mp, &kobj);
} }
const TObject *luaH_getglobal (lua_State *L, const char *name) { TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
return luaH_getstr(L->gt, luaS_new(L, name)); TObject kobj;
Node *mp = hashstr(t, key);
Node *n = mp;
do { /* check whether `key' is somewhere in the chain */
if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
return &n->val; /* that's all */
else n = n->next;
} while (n);
/* `key' not found; must insert it */
ttype(&kobj) = LUA_TSTRING;
tsvalue(&kobj) = key;
return newkey(L, t, mp, &kobj);
}
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
switch (ttype(key)) {
case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
default: return luaH_setany(L, t, key);
}
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 1.25 2000/11/24 17:39:56 roberto Exp roberto $ ** $Id: ltable.h,v 1.26 2000/12/04 18:33:40 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,16 +16,14 @@
Hash *luaH_new (lua_State *L, int nhash); Hash *luaH_new (lua_State *L, int nhash);
void luaH_free (lua_State *L, Hash *t); void luaH_free (lua_State *L, Hash *t);
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); const TObject *luaH_get (const Hash *t, const TObject *key);
const TObject *luaH_getnum (const Hash *t, lua_Number key); const TObject *luaH_getnum (const Hash *t, lua_Number key);
const TObject *luaH_getstr (const Hash *t, TString *key); const TObject *luaH_getstr (const Hash *t, TString *key);
void luaH_remove (Hash *t, TObject *key); void luaH_remove (Hash *t, TObject *key);
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key); TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
Node * luaH_next (lua_State *L, const Hash *t, const TObject *r); Node * luaH_next (lua_State *L, const Hash *t, const TObject *r);
TObject *luaH_setint (lua_State *L, Hash *t, int key); TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key);
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, lua_Number val); TObject *luaH_setstr (lua_State *L, Hash *t, TString *key);
luint32 luaH_hash (lua_State *L, const TObject *key);
const TObject *luaH_getglobal (lua_State *L, const char *name);
/* exported only for debugging */ /* exported only for debugging */
Node *luaH_mainposition (const Hash *t, const TObject *key); Node *luaH_mainposition (const Hash *t, const TObject *key);

31
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.149 2000/12/28 12:55:41 roberto Exp roberto $ ** $Id: lvm.c,v 1.150 2001/01/10 17:41:50 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -122,7 +122,7 @@ const TObject *luaV_gettable (lua_State *L, StkId t) {
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */ ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
luaT_gettm(L, tg, TM_GETTABLE) == NULL)) { /* or no TM? */ luaT_gettm(L, tg, TM_GETTABLE) == NULL)) { /* or no TM? */
/* do a primitive get */ /* do a primitive get */
const TObject *h = luaH_get(L, hvalue(t), L->top-1); const TObject *h = luaH_get(hvalue(t), L->top-1);
/* result is no nil or there is no `index' tag method? */ /* result is no nil or there is no `index' tag method? */
if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL)) if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL))
return h; /* return result */ return h; /* return result */
@ -195,21 +195,11 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) {
void luaV_setglobal (lua_State *L, TString *s) { void luaV_setglobal (lua_State *L, TString *s) {
const TObject *oldvalue = luaH_getstr(L->gt, s); TObject *oldvalue = luaH_setstr(L, L->gt, s);
Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL); Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
if (tm == NULL) { /* is there a tag method? */ if (tm == NULL) /* no tag methods? */
if (oldvalue != &luaO_nilobject) { *oldvalue = *(L->top - 1); /* raw set */
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */ else { /* call tag method */
*(TObject *)oldvalue = *(L->top - 1);
}
else {
TObject key;
ttype(&key) = LUA_TSTRING;
tsvalue(&key) = s;
*luaH_set(L, L->gt, &key) = *(L->top - 1);
}
}
else {
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); /* new value */ *(L->top+2) = *(L->top-1); /* new value */
*(L->top+1) = *oldvalue; *(L->top+1) = *oldvalue;
@ -320,12 +310,15 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
static void luaV_pack (lua_State *L, StkId firstelem) { static void luaV_pack (lua_State *L, StkId firstelem) {
TObject *nf;
int i; int i;
Hash *htab = luaH_new(L, 0); Hash *htab = luaH_new(L, 0);
for (i=0; firstelem+i<L->top; i++) for (i=0; firstelem+i<L->top; i++)
*luaH_setint(L, htab, i+1) = *(firstelem+i); *luaH_setnum(L, htab, i+1) = *(firstelem+i);
/* store counter in field `n' */ /* store counter in field `n' */
luaH_setstrnum(L, htab, luaS_newliteral(L, "n"), i); nf = luaH_setstr(L, htab, luaS_newliteral(L, "n"));
ttype(nf) = LUA_TNUMBER;
nvalue(nf) = i;
L->top = firstelem; /* remove elements from the stack */ L->top = firstelem; /* remove elements from the stack */
ttype(L->top) = LUA_TTABLE; ttype(L->top) = LUA_TTABLE;
hvalue(L->top) = htab; hvalue(L->top) = htab;
@ -498,7 +491,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
Hash *arr = hvalue(top-n-1); Hash *arr = hvalue(top-n-1);
L->top = top-n; /* final value of `top' (in case of errors) */ L->top = top-n; /* final value of `top' (in case of errors) */
for (; n; n--) for (; n; n--)
*luaH_setint(L, arr, n+aux) = *(--top); *luaH_setnum(L, arr, n+aux) = *(--top);
break; break;
} }
case OP_SETMAP: { case OP_SETMAP: {