From 8bcf6228765e56be19feb90c8805cc2fb2223188 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Jun 2000 13:31:41 -0300 Subject: new signature for `luaH_set' --- lapi.c | 4 ++-- lbuiltin.c | 26 +++++++++++++++++--------- ltable.c | 51 +++++++++++++++++++++++---------------------------- ltable.h | 10 ++++------ lvm.c | 22 ++++++++-------------- lvm.h | 3 +-- 6 files changed, 55 insertions(+), 61 deletions(-) diff --git a/lapi.c b/lapi.c index fcc0ba51..a660aae2 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.81 2000/05/24 13:54:49 roberto Exp roberto $ +** $Id: lapi.c,v 1.82 2000/05/26 19:17:57 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -144,7 +144,7 @@ void lua_rawset (lua_State *L) { luaA_checkCargs(L, 3); if (ttype(L->top-3) != TAG_TABLE) lua_error(L, "indexed expression not a table"); - luaH_set(L, avalue(L->top-3), L->top-2, L->top-1); + *luaH_set(L, avalue(L->top-3), L->top-2) = *(L->top-1); L->top -= 3; } diff --git a/lbuiltin.c b/lbuiltin.c index 19cdb6d6..ac45f361 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.112 2000/06/02 19:08:56 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.113 2000/06/05 20:15:33 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -402,6 +402,13 @@ void luaB_getn (lua_State *L) { } +/* auxiliar function */ +static void t_move (lua_State *L, Hash *t, int from, int to) { + TObject *p = luaH_setint(L, t, to); /* may change following `get' */ + *p = *luaH_getnum(t, from); +} + + void luaB_tinsert (lua_State *L) { Hash *a = gettable(L, 1); lua_Object v = lua_getparam(L, 3); @@ -413,10 +420,10 @@ void luaB_tinsert (lua_State *L) { v = luaL_nonnullarg(L, 2); pos = n+1; } - luaV_setn(L, a, n+1); /* a.n = n+1 */ + luaH_setstrnum(L, a, luaS_new(L, "n"), n+1); /* a.n = n+1 */ for (; n>=pos; n--) - luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */ - luaH_setint(L, a, pos, v); /* a[pos] = v */ + t_move(L, a, n, n+1); /* a[n+1] = a[n] */ + *luaH_setint(L, a, pos) = *v; /* a[pos] = v */ } @@ -427,9 +434,9 @@ void luaB_tremove (lua_State *L) { if (n <= 0) return; /* table is "empty" */ luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ for ( ;possize; Node *nold = t->node; - int newsize = numuse(t); + int nelems = numuse(t); int i; - LUA_ASSERT(L, newsize<=oldsize, "wrong count"); - if (newsize >= oldsize-oldsize/4) /* using more than 3/4? */ + LUA_ASSERT(L, nelems<=oldsize, "wrong count"); + if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */ setnodevector(L, t, (lint32)oldsize*2); - else if (newsize <= oldsize/4 && /* less than 1/4? */ + else if (nelems <= oldsize/4 && /* less than 1/4? */ oldsize > MINPOWER2) setnodevector(L, t, oldsize/2); else @@ -207,35 +207,28 @@ static void rehash (lua_State *L, Hash *t) { for (i=0; ival) != TAG_NIL) - luaH_set(L, t, &old->key, &old->val); + *luaH_set(L, t, &old->key) = old->val; } luaM_free(L, nold); /* free old array */ } /* -** sets a pair key-value in a hash table; first, check whether key is +** inserts a key into a hash table; first, check whether key is ** already present; if not, check whether key's main position is free; ** if not, check whether colliding node is in its main position or not; -** if it is not, move colliding node to an empty place and put new pair +** if it is not, move colliding node to an empty place and put new key ** in its main position; otherwise (colliding node is in its main position), -** new pair goes to an empty position. -** Tricky point: the only place where an old element is moved is when -** we move the colliding node to an empty place; nevertheless, its old -** value is still in that position until we set the value for the new -** pair; therefore, even when `val' points to an element of this table -** (this happens when we use `luaH_move'), there is no problem. +** new key goes to an empty position. */ -void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { +TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { Node *mp = luaH_mainposition(t, key); Node *n = mp; if (!mp) lua_error(L, "unexpected type to index table"); do { /* check whether `key' is somewhere in the chain */ - if (luaO_equalObj(key, &n->key)) { - n->val = *val; /* update value */ - return; /* that's all */ - } + if (luaO_equalObj(key, &n->key)) + return &n->val; /* that's all */ else n = n->next; } while (n); /* `key' not found; must insert it */ @@ -243,7 +236,7 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { Node *othern; /* main position of colliding node */ n = t->firstfree; /* get a free place */ /* is colliding node out of its main position? (can only happens if - its position if after "firstfree") */ + its position is after "firstfree") */ if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) { /* yes; move colliding node into free position */ while (othern->next != mp) othern = othern->next; /* find previous */ @@ -259,30 +252,32 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { } } mp->key = *key; - mp->val = *val; - for (;;) { /* check free places */ + for (;;) { /* correct `firstfree' */ if (ttype(&t->firstfree->key) == TAG_NIL) - return; /* OK; table still has a free place */ + return &mp->val; /* OK; table still has a free place */ else if (t->firstfree == t->node) break; /* cannot decrement from here */ else (t->firstfree)--; } rehash(L, t); /* no more free places */ + return luaH_set(L, t, key); /* `rehash' invalidates this insertion */ } -void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) { +TObject *luaH_setint (lua_State *L, Hash *t, int key) { TObject index; ttype(&index) = TAG_NUMBER; nvalue(&index) = key; - luaH_set(L, t, &index, val); + return luaH_set(L, t, &index); } -void luaH_setstr (lua_State *L, Hash *t, TString *key, const TObject *val) { - TObject index; +void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) { + TObject *value, index; ttype(&index) = TAG_STRING; tsvalue(&index) = key; - luaH_set(L, t, &index, val); + value = luaH_set(L, t, &index); + ttype(value) = TAG_NUMBER; + nvalue(value) = val; } diff --git a/ltable.h b/ltable.h index 52053f8f..458075b6 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 1.21 2000/06/05 20:07:53 roberto Exp roberto $ +** $Id: ltable.h,v 1.22 2000/06/05 20:15:33 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -14,18 +14,16 @@ #define key(n) (&(n)->key) #define val(n) (&(n)->val) -#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getnum(t, from))) - Hash *luaH_new (lua_State *L, int nhash); void luaH_free (lua_State *L, Hash *t); const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); const TObject *luaH_getnum (const Hash *t, Number key); const TObject *luaH_getstr (const Hash *t, TString *key); void luaH_remove (Hash *t, TObject *key); -void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); +TObject *luaH_set (lua_State *L, Hash *t, const TObject *key); int luaH_pos (lua_State *L, const Hash *t, const TObject *r); -void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); -void luaH_setstr (lua_State *L, Hash *t, TString *key, const TObject *val); +TObject *luaH_setint (lua_State *L, Hash *t, int key); +void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val); unsigned long luaH_hash (lua_State *L, const TObject *key); const TObject *luaH_getglobal (lua_State *L, const char *name); diff --git a/lvm.c b/lvm.c index c264fd96..76661bfb 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.111 2000/06/05 14:56:18 roberto Exp roberto $ +** $Id: lvm.c,v 1.112 2000/06/05 20:15:33 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -65,13 +65,6 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ } -void luaV_setn (lua_State *L, Hash *t, int val) { - TObject value; - ttype(&value) = TAG_NUMBER; nvalue(&value) = val; - luaH_setstr(L, t, luaS_new(L, "n"), &value); -} - - static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) { Closure *c = luaF_newclosure(L, nelems); L->top -= nelems; @@ -147,7 +140,7 @@ void luaV_settable (lua_State *L, StkId t, StkId top) { else { /* object is a table... */ im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE); if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */ - luaH_set(L, avalue(t), t+1, top-1); + *luaH_set(L, avalue(t), t+1) = *(top-1); return; } /* else it has a `settable' method, go through to next command */ @@ -191,7 +184,7 @@ void luaV_setglobal (lua_State *L, TString *s, StkId top) { TObject key; ttype(&key) = TAG_STRING; tsvalue(&key) = s; - luaH_set(L, L->gt, &key, top-1); + *luaH_set(L, L->gt, &key) = *(top-1); } } else { @@ -311,8 +304,9 @@ void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) { htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */ ttype(tab) = TAG_TABLE; for (i=0; itop = top-n; /* final value of `top' (in case of errors) */ for (; n; n--) - luaH_setint(L, arr, n+aux, --top); + *luaH_setint(L, arr, n+aux) = *(--top); break; } @@ -486,8 +480,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { Hash *arr = avalue(finaltop-1); L->top = finaltop; /* final value of `top' (in case of errors) */ for (; n; n--) { - luaH_set(L, arr, top-2, top-1); top-=2; + *luaH_set(L, arr, top) = *(top+1); } break; } diff --git a/lvm.h b/lvm.h index ae57346b..01fc6f92 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 1.21 2000/04/19 13:36:25 roberto Exp roberto $ +** $Id: lvm.h,v 1.22 2000/05/08 19:32:53 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -20,7 +20,6 @@ void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab); int luaV_tonumber (TObject *obj); int luaV_tostring (lua_State *L, TObject *obj); -void luaV_setn (lua_State *L, Hash *t, int val); void luaV_gettable (lua_State *L, StkId top); void luaV_settable (lua_State *L, StkId t, StkId top); void luaV_getglobal (lua_State *L, TString *s, StkId top); -- cgit v1.2.3-55-g6feb