mirror of
https://github.com/lua/lua
synced 2024-12-24 19:36:50 +03:00
small optimizations(?)
This commit is contained in:
parent
3314f49ec4
commit
933bead92e
85
ltable.c
85
ltable.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 1.16 1998/12/30 13:14:46 roberto Exp $
|
** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -24,8 +24,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static long int hashindex (TObject *ref)
|
static long int hashindex (TObject *ref) {
|
||||||
{
|
|
||||||
long int h;
|
long int h;
|
||||||
switch (ttype(ref)) {
|
switch (ttype(ref)) {
|
||||||
case LUA_T_NUMBER:
|
case LUA_T_NUMBER:
|
||||||
@ -54,29 +53,38 @@ static long int hashindex (TObject *ref)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int present (Hash *t, TObject *key)
|
static Node *present (Hash *t, TObject *key) {
|
||||||
{
|
|
||||||
int tsize = nhash(t);
|
int tsize = nhash(t);
|
||||||
long int h = hashindex(key);
|
long int h = hashindex(key);
|
||||||
int h1 = h%tsize;
|
int h1 = h%tsize;
|
||||||
TObject *rf = ref(node(t, h1));
|
Node *n = node(t, h1);
|
||||||
if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
|
/* keep looking until an entry with "ref" equal to key or nil */
|
||||||
|
if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
|
||||||
|
: ttype(ref(n)) != LUA_T_NIL)) {
|
||||||
int h2 = h%(tsize-2) + 1;
|
int h2 = h%(tsize-2) + 1;
|
||||||
do {
|
do {
|
||||||
h1 += h2;
|
h1 += h2;
|
||||||
if (h1 >= tsize) h1 -= tsize;
|
if (h1 >= tsize) h1 -= tsize;
|
||||||
rf = ref(node(t, h1));
|
n = node(t, h1);
|
||||||
} while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
|
} while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
|
||||||
|
: ttype(ref(n)) != LUA_T_NIL));
|
||||||
}
|
}
|
||||||
return h1;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
void luaH_free (Hash *frees) {
|
||||||
** Alloc a vector node
|
while (frees) {
|
||||||
*/
|
Hash *next = (Hash *)frees->head.next;
|
||||||
static Node *hashnodecreate (int nhash)
|
L->nblocks -= gcsize(frees->nhash);
|
||||||
{
|
luaM_free(nodevector(frees));
|
||||||
|
luaM_free(frees);
|
||||||
|
frees = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Node *hashnodecreate (int nhash) {
|
||||||
Node *v = luaM_newvector(nhash, Node);
|
Node *v = luaM_newvector(nhash, Node);
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<nhash; i++)
|
for (i=0; i<nhash; i++)
|
||||||
@ -84,26 +92,6 @@ static Node *hashnodecreate (int nhash)
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
** Delete a hash
|
|
||||||
*/
|
|
||||||
static void hashdelete (Hash *t)
|
|
||||||
{
|
|
||||||
luaM_free(nodevector(t));
|
|
||||||
luaM_free(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaH_free (Hash *frees)
|
|
||||||
{
|
|
||||||
while (frees) {
|
|
||||||
Hash *next = (Hash *)frees->head.next;
|
|
||||||
L->nblocks -= gcsize(frees->nhash);
|
|
||||||
hashdelete(frees);
|
|
||||||
frees = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Hash *luaH_new (int nhash) {
|
Hash *luaH_new (int nhash) {
|
||||||
Hash *t = luaM_new(Hash);
|
Hash *t = luaM_new(Hash);
|
||||||
@ -130,6 +118,7 @@ static int newsize (Hash *t) {
|
|||||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void rehash (Hash *t) {
|
static void rehash (Hash *t) {
|
||||||
int nold = nhash(t);
|
int nold = nhash(t);
|
||||||
Node *vold = nodevector(t);
|
Node *vold = nodevector(t);
|
||||||
@ -141,7 +130,7 @@ static void rehash (Hash *t) {
|
|||||||
for (i=0; i<nold; i++) {
|
for (i=0; i<nold; i++) {
|
||||||
Node *n = vold+i;
|
Node *n = vold+i;
|
||||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
|
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
|
||||||
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
*present(t, ref(n)) = *n; /* copy old node to new hash */
|
||||||
nuse(t)++;
|
nuse(t)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,29 +138,28 @@ static void rehash (Hash *t) {
|
|||||||
luaM_free(vold);
|
luaM_free(vold);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the hash node is present, return its pointer, otherwise return
|
** If the hash node is present, return its pointer, otherwise return
|
||||||
** null.
|
** null.
|
||||||
*/
|
*/
|
||||||
TObject *luaH_get (Hash *t, TObject *ref)
|
TObject *luaH_get (Hash *t, TObject *ref) {
|
||||||
{
|
Node *n = present(t, ref);
|
||||||
int h = present(t, ref);
|
if (ttype(ref(n)) != LUA_T_NIL) return val(n);
|
||||||
if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
|
|
||||||
else return &luaO_nilobject;
|
else return &luaO_nilobject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the hash node is present, return its pointer, otherwise create a luaM_new
|
** If the hash node is present, return its pointer, otherwise create a new
|
||||||
** node for the given reference and also return its pointer.
|
** node for the given reference and also return its pointer.
|
||||||
*/
|
*/
|
||||||
TObject *luaH_set (Hash *t, TObject *ref)
|
TObject *luaH_set (Hash *t, TObject *ref) {
|
||||||
{
|
Node *n = present(t, ref);
|
||||||
Node *n = node(t, present(t, ref));
|
|
||||||
if (ttype(ref(n)) == LUA_T_NIL) {
|
if (ttype(ref(n)) == LUA_T_NIL) {
|
||||||
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
||||||
rehash(t);
|
rehash(t);
|
||||||
n = node(t, present(t, ref));
|
n = present(t, ref);
|
||||||
}
|
}
|
||||||
nuse(t)++;
|
nuse(t)++;
|
||||||
*ref(n) = *ref;
|
*ref(n) = *ref;
|
||||||
@ -192,18 +180,17 @@ static Node *hashnext (Hash *t, int i) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
n = node(t, i);
|
n = node(t, i);
|
||||||
}
|
}
|
||||||
return node(t, i);
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *luaH_next (Hash *t, TObject *r) {
|
Node *luaH_next (Hash *t, TObject *r) {
|
||||||
if (ttype(r) == LUA_T_NIL)
|
if (ttype(r) == LUA_T_NIL)
|
||||||
return hashnext(t, 0);
|
return hashnext(t, 0);
|
||||||
else {
|
else {
|
||||||
int i = present(t, r);
|
Node *n = present(t, r);
|
||||||
Node *n = node(t, i);
|
|
||||||
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
||||||
2, "key not found");
|
2, "key not found");
|
||||||
return hashnext(t, i+1);
|
return hashnext(t, (n-(t->node))+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user