mirror of
https://github.com/lua/lua
synced 2024-12-27 12:49:43 +03:00
keep it simple
This commit is contained in:
parent
9c142740c1
commit
b3fe203c36
58
ltable.c
58
ltable.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 1.30 1999/11/22 13:12:07 roberto Exp roberto $
|
** $Id: ltable.c,v 1.31 1999/11/26 18:59:20 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -40,7 +40,7 @@
|
|||||||
** 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
|
||||||
** of its hash value)
|
** of its hash value)
|
||||||
*/
|
*/
|
||||||
Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key) {
|
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||||
unsigned long h;
|
unsigned long h;
|
||||||
switch (ttype(key)) {
|
switch (ttype(key)) {
|
||||||
case LUA_T_NUMBER:
|
case LUA_T_NUMBER:
|
||||||
@ -62,8 +62,7 @@ Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key) {
|
|||||||
h = IntPoint(L, clvalue(key));
|
h = IntPoint(L, clvalue(key));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
lua_error(L, "unexpected type to index table");
|
return NULL; /* invalid key */
|
||||||
h = 0; /* to avoid warnings */
|
|
||||||
}
|
}
|
||||||
LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||||
"a&(x-1) == a%x, for x power of 2");
|
"a&(x-1) == a%x, for x power of 2");
|
||||||
@ -72,13 +71,15 @@ Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key) {
|
|||||||
|
|
||||||
|
|
||||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||||
Node *n = luaH_mainposition(L, t, key);
|
Node *n = luaH_mainposition(t, key);
|
||||||
do {
|
if (!n)
|
||||||
|
lua_error(L, "unexpected type to index table");
|
||||||
|
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);
|
} while (n);
|
||||||
return &luaO_nilobject;
|
return &luaO_nilobject; /* key not found */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -140,49 +141,16 @@ static int newsize (const Hash *t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** the rehash is done in two stages: first, we insert only the elements whose
|
|
||||||
** main position is free, to avoid needless collisions. In the second stage,
|
|
||||||
** we insert the other elements.
|
|
||||||
*/
|
|
||||||
static void rehash (lua_State *L, Hash *t) {
|
static void rehash (lua_State *L, Hash *t) {
|
||||||
int oldsize = t->size;
|
int oldsize = t->size;
|
||||||
Node *nold = t->node;
|
Node *nold = t->node;
|
||||||
int i;
|
int i;
|
||||||
L->nblocks -= gcsize(L, oldsize);
|
L->nblocks -= gcsize(L, oldsize);
|
||||||
setnodevector(L, t, newsize(t)); /* create new array of nodes */
|
setnodevector(L, t, newsize(t)); /* create new array of nodes */
|
||||||
/* first loop; set only elements that can go in their main positions */
|
|
||||||
for (i=0; i<oldsize; i++) {
|
for (i=0; i<oldsize; i++) {
|
||||||
Node *old = nold+i;
|
Node *old = nold+i;
|
||||||
if (ttype(&old->val) == LUA_T_NIL)
|
if (ttype(&old->val) != LUA_T_NIL)
|
||||||
old->next = NULL; /* `remove' it for next loop */
|
luaH_set(L, t, &old->key, &old->val);
|
||||||
else {
|
|
||||||
Node *mp = luaH_mainposition(L, t, &old->key); /* new main position */
|
|
||||||
if (ttype(&mp->key) == LUA_T_NIL) { /* is it empty? */
|
|
||||||
mp->key = old->key; /* put element there */
|
|
||||||
mp->val = old->val;
|
|
||||||
old->next = NULL; /* `remove' it for next loop */
|
|
||||||
}
|
|
||||||
else /* it will be copied in next loop */
|
|
||||||
old->next = mp; /* to be used in next loop */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* update `firstfree' */
|
|
||||||
while (ttype(&t->firstfree->key) != LUA_T_NIL) t->firstfree--;
|
|
||||||
/* second loop; update elements with colision */
|
|
||||||
for (i=0; i<oldsize; i++) {
|
|
||||||
Node *old = nold+i;
|
|
||||||
if (old->next) { /* wasn't already `removed'? */
|
|
||||||
Node *mp = old->next; /* main position */
|
|
||||||
Node *e = t->firstfree; /* actual position */
|
|
||||||
e->key = old->key; /* put element in the free position */
|
|
||||||
e->val = old->val;
|
|
||||||
e->next = mp->next; /* chain actual position in main position's list */
|
|
||||||
mp->next = e;
|
|
||||||
do { /* update `firstfree' */
|
|
||||||
t->firstfree--;
|
|
||||||
} while (ttype(&t->firstfree->key) != LUA_T_NIL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
luaM_free(L, nold); /* free old array */
|
luaM_free(L, nold); /* free old array */
|
||||||
}
|
}
|
||||||
@ -202,8 +170,10 @@ static void rehash (lua_State *L, Hash *t) {
|
|||||||
** (this happens when we use `luaH_move'), there is no problem.
|
** (this happens when we use `luaH_move'), there is no problem.
|
||||||
*/
|
*/
|
||||||
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
||||||
Node *mp = luaH_mainposition(L, t, key);
|
Node *mp = luaH_mainposition(t, key);
|
||||||
Node *n = mp;
|
Node *n = mp;
|
||||||
|
if (!mp)
|
||||||
|
lua_error(L, "unexpected type to index table");
|
||||||
do { /* check whether `key' is somewhere in the chain */
|
do { /* check whether `key' is somewhere in the chain */
|
||||||
if (luaO_equalObj(key, &n->key)) {
|
if (luaO_equalObj(key, &n->key)) {
|
||||||
n->val = *val; /* update value */
|
n->val = *val; /* update value */
|
||||||
@ -217,7 +187,7 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
|||||||
n = t->firstfree; /* get a free place */
|
n = t->firstfree; /* get a free place */
|
||||||
/* is colliding node out of its main position? (can only happens if
|
/* is colliding node out of its main position? (can only happens if
|
||||||
its position if after "firstfree") */
|
its position if after "firstfree") */
|
||||||
if (mp > n && (othern=luaH_mainposition(L, t, &mp->key)) != mp) {
|
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' */
|
||||||
|
Loading…
Reference in New Issue
Block a user