diff options
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 26 |
1 files changed, 7 insertions, 19 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** hash manager for lua | 3 | ** hash manager for lua |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp roberto $"; | 6 | char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | 9 | ||
@@ -13,7 +13,6 @@ char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp roberto $"; | |||
13 | #include "table.h" | 13 | #include "table.h" |
14 | #include "lua.h" | 14 | #include "lua.h" |
15 | 15 | ||
16 | #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0)) | ||
17 | 16 | ||
18 | #define nhash(t) ((t)->nhash) | 17 | #define nhash(t) ((t)->nhash) |
19 | #define nuse(t) ((t)->nuse) | 18 | #define nuse(t) ((t)->nuse) |
@@ -24,19 +23,18 @@ char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp roberto $"; | |||
24 | #define val(n) (&(n)->val) | 23 | #define val(n) (&(n)->val) |
25 | 24 | ||
26 | 25 | ||
27 | #define REHASH_LIMIT 0.70 /* avoid more than this % full */ | 26 | #define REHASH_LIMIT 0.70 /* avoid more than this % full */ |
28 | 27 | ||
29 | 28 | ||
30 | static Hash *listhead = NULL; | 29 | static Hash *listhead = NULL; |
31 | 30 | ||
32 | 31 | ||
33 | |||
34 | /* hash dimensions values */ | 32 | /* hash dimensions values */ |
35 | static Word dimensions[] = | 33 | static Word dimensions[] = |
36 | {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421, | 34 | {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421, |
37 | 12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */ | 35 | 12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */ |
38 | 36 | ||
39 | static Word redimension (Word nhash) | 37 | Word luaI_redimension (Word nhash) |
40 | { | 38 | { |
41 | Word i; | 39 | Word i; |
42 | for (i=0; dimensions[i]!=0; i++) | 40 | for (i=0; dimensions[i]!=0; i++) |
@@ -58,17 +56,7 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */ | |||
58 | case LUA_T_NUMBER: | 56 | case LUA_T_NUMBER: |
59 | return (((Word)nvalue(ref))%nhash(t)); | 57 | return (((Word)nvalue(ref))%nhash(t)); |
60 | case LUA_T_STRING: | 58 | case LUA_T_STRING: |
61 | { | 59 | return (Word)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */ |
62 | unsigned long h = tsvalue(ref)->hash; | ||
63 | if (h == 0) | ||
64 | { | ||
65 | char *name = svalue(ref); | ||
66 | while (*name) | ||
67 | h = ((h<<5)-h)^(unsigned char)*(name++); | ||
68 | tsvalue(ref)->hash = h; | ||
69 | } | ||
70 | return (Word)h%nhash(t); /* make it a valid index */ | ||
71 | } | ||
72 | case LUA_T_FUNCTION: | 60 | case LUA_T_FUNCTION: |
73 | return (((IntPoint)ref->value.tf)%nhash(t)); | 61 | return (((IntPoint)ref->value.tf)%nhash(t)); |
74 | case LUA_T_CFUNCTION: | 62 | case LUA_T_CFUNCTION: |
@@ -87,7 +75,7 @@ int lua_equalObj (Object *t1, Object *t2) | |||
87 | { | 75 | { |
88 | case LUA_T_NIL: return 1; | 76 | case LUA_T_NIL: return 1; |
89 | case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); | 77 | case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); |
90 | case LUA_T_STRING: return streq(svalue(t1), svalue(t2)); | 78 | case LUA_T_STRING: return svalue(t1) == svalue(t2); |
91 | case LUA_T_ARRAY: return avalue(t1) == avalue(t2); | 79 | case LUA_T_ARRAY: return avalue(t1) == avalue(t2); |
92 | case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf; | 80 | case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf; |
93 | case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); | 81 | case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); |
@@ -126,7 +114,7 @@ static Node *hashnodecreate (Word nhash) | |||
126 | static Hash *hashcreate (Word nhash) | 114 | static Hash *hashcreate (Word nhash) |
127 | { | 115 | { |
128 | Hash *t = new(Hash); | 116 | Hash *t = new(Hash); |
129 | nhash = redimension((Word)((float)nhash/REHASH_LIMIT)); | 117 | nhash = luaI_redimension((Word)((float)nhash/REHASH_LIMIT)); |
130 | nodevector(t) = hashnodecreate(nhash); | 118 | nodevector(t) = hashnodecreate(nhash); |
131 | nhash(t) = nhash; | 119 | nhash(t) = nhash; |
132 | nuse(t) = 0; | 120 | nuse(t) = 0; |
@@ -237,7 +225,7 @@ static void rehash (Hash *t) | |||
237 | Word i; | 225 | Word i; |
238 | Word nold = nhash(t); | 226 | Word nold = nhash(t); |
239 | Node *vold = nodevector(t); | 227 | Node *vold = nodevector(t); |
240 | nhash(t) = redimension(nhash(t)); | 228 | nhash(t) = luaI_redimension(nhash(t)); |
241 | nodevector(t) = hashnodecreate(nhash(t)); | 229 | nodevector(t) = hashnodecreate(nhash(t)); |
242 | for (i=0; i<nold; i++) | 230 | for (i=0; i<nold; i++) |
243 | { | 231 | { |