commit 2b301d711b1fedf2fc31beba67e2c096b6d933a0
parent 10bdd838440b082aaf70748571b443f7c941db81
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 28 Nov 1994 13:10:32 -0200
new hash function; hash value for strings are kept with the string
Diffstat:
M | hash.c | | | 18 | +++++++++--------- |
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/hash.c b/hash.c
@@ -3,7 +3,7 @@
** 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 "opcode.h"
@@ -56,15 +56,15 @@ static int hashindex (Hash *t, Object *ref) /* hash function */
return (((int)nvalue(ref))%nhash(t));
case LUA_T_STRING:
{
- int h;
- char *name = svalue(ref);
- for (h=0; *name!=0; name++) /* interpret name as binary number */
- {
- h <<= 8;
- h += (unsigned char) *name; /* avoid sign extension */
- h %= nhash(t); /* make it a valid index */
+ unsigned long h = tsvalue(ref)->hash;
+ if (h == 0)
+ {
+ char *name = svalue(ref);
+ while (*name)
+ h = ((h<<5)-h)^(unsigned char)*(name++);
+ tsvalue(ref)->hash = h;
}
- return h;
+ return h%nhash(t); /* make it a valid index */
}
case LUA_T_FUNCTION:
return (((int)bvalue(ref))%nhash(t));