new hash function; hash value for strings are kept with the string

This commit is contained in:
Roberto Ierusalimschy
1994-11-28 13:10:51 -02:00
parent 10bdd83844
commit 2b301d711b

18
hash.c
View File

@@ -3,7 +3,7 @@
** hash manager for lua ** hash manager for lua
*/ */
char *rcs_hash="$Id: hash.c,v 2.18 1994/11/17 13:58:57 roberto Exp roberto $"; char *rcs_hash="$Id: hash.c,v 2.20 1994/11/25 19:27:03 roberto Exp $";
#include "mem.h" #include "mem.h"
#include "opcode.h" #include "opcode.h"
@@ -56,15 +56,15 @@ static int hashindex (Hash *t, Object *ref) /* hash function */
return (((int)nvalue(ref))%nhash(t)); return (((int)nvalue(ref))%nhash(t));
case LUA_T_STRING: case LUA_T_STRING:
{ {
int h; unsigned long h = tsvalue(ref)->hash;
char *name = svalue(ref); if (h == 0)
for (h=0; *name!=0; name++) /* interpret name as binary number */ {
{ char *name = svalue(ref);
h <<= 8; while (*name)
h += (unsigned char) *name; /* avoid sign extension */ h = ((h<<5)-h)^(unsigned char)*(name++);
h %= nhash(t); /* make it a valid index */ tsvalue(ref)->hash = h;
} }
return h; return h%nhash(t); /* make it a valid index */
} }
case LUA_T_FUNCTION: case LUA_T_FUNCTION:
return (((int)bvalue(ref))%nhash(t)); return (((int)bvalue(ref))%nhash(t));