diff options
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 21 |
1 files changed, 11 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.28 1999/11/22 13:12:07 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.29 1999/11/22 18:24:50 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -52,24 +52,25 @@ static unsigned long hash_s (const char *s, long l) { | |||
52 | } | 52 | } |
53 | 53 | ||
54 | 54 | ||
55 | void luaS_grow (lua_State *L, stringtable *tb) { | 55 | void luaS_resize (lua_State *L, stringtable *tb, int newsize) { |
56 | int ns = luaO_redimension(L, tb->nuse); /* new size */ | 56 | TaggedString **newhash = luaM_newvector(L, newsize, TaggedString *); |
57 | TaggedString **newhash = luaM_newvector(L, ns, TaggedString *); | ||
58 | int i; | 57 | int i; |
59 | for (i=0; i<ns; i++) newhash[i] = NULL; | 58 | for (i=0; i<newsize; i++) newhash[i] = NULL; |
60 | /* rehash */ | 59 | /* rehash */ |
61 | for (i=0; i<tb->size; i++) { | 60 | for (i=0; i<tb->size; i++) { |
62 | TaggedString *p = tb->hash[i]; | 61 | TaggedString *p = tb->hash[i]; |
63 | while (p) { /* for each node in the list */ | 62 | while (p) { /* for each node in the list */ |
64 | TaggedString *next = p->nexthash; /* save next */ | 63 | TaggedString *next = p->nexthash; /* save next */ |
65 | int h = p->hash%ns; /* new position */ | 64 | int h = p->hash&(newsize-1); /* new position */ |
65 | LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)), | ||
66 | "a&(x-1) == a%x, for x power of 2"); | ||
66 | p->nexthash = newhash[h]; /* chain it in new position */ | 67 | p->nexthash = newhash[h]; /* chain it in new position */ |
67 | newhash[h] = p; | 68 | newhash[h] = p; |
68 | p = next; | 69 | p = next; |
69 | } | 70 | } |
70 | } | 71 | } |
71 | luaM_free(L, tb->hash); | 72 | luaM_free(L, tb->hash); |
72 | tb->size = ns; | 73 | tb->size = newsize; |
73 | tb->hash = newhash; | 74 | tb->hash = newhash; |
74 | } | 75 | } |
75 | 76 | ||
@@ -113,14 +114,14 @@ static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) { | |||
113 | tb->hash[h] = ts; | 114 | tb->hash[h] = ts; |
114 | tb->nuse++; | 115 | tb->nuse++; |
115 | if (tb->nuse > tb->size) /* too crowded? */ | 116 | if (tb->nuse > tb->size) /* too crowded? */ |
116 | luaS_grow(L, tb); | 117 | luaS_resize(L, tb, tb->size*2); |
117 | } | 118 | } |
118 | 119 | ||
119 | 120 | ||
120 | TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) { | 121 | TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) { |
121 | unsigned long h = hash_s(str, l); | 122 | unsigned long h = hash_s(str, l); |
122 | stringtable *tb = &L->string_root[h%NUM_HASHSTR]; | 123 | stringtable *tb = &L->string_root[h%NUM_HASHSTR]; |
123 | int h1 = h%tb->size; | 124 | int h1 = h&(tb->size-1); |
124 | TaggedString *ts; | 125 | TaggedString *ts; |
125 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { | 126 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { |
126 | if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) | 127 | if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) |
@@ -136,7 +137,7 @@ TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) { | |||
136 | TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) { | 137 | TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) { |
137 | unsigned long h = IntPoint(L, udata); | 138 | unsigned long h = IntPoint(L, udata); |
138 | stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; | 139 | stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; |
139 | int h1 = h%tb->size; | 140 | int h1 = h&(tb->size-1); |
140 | TaggedString *ts; | 141 | TaggedString *ts; |
141 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { | 142 | for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { |
142 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) | 143 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) |