aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-20 15:15:33 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-20 15:15:33 -0300
commit099442c41f2cec6122690e6c8f2e11327613e6f6 (patch)
tree73599b274ea4a9b96906ff8160eeb4a524702a8e /ltable.c
parent27600fe87a6fafdfd4ddddeb390591fe749b480f (diff)
downloadlua-099442c41f2cec6122690e6c8f2e11327613e6f6.tar.gz
lua-099442c41f2cec6122690e6c8f2e11327613e6f6.tar.bz2
lua-099442c41f2cec6122690e6c8f2e11327613e6f6.zip
better separation between basic types
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/ltable.c b/ltable.c
index 1e524f5f..8522e8fe 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.74 2001/01/30 19:48:37 roberto Exp roberto $ 2** $Id: ltable.c,v 1.75 2001/02/01 17:40:48 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -31,7 +31,7 @@
31#define TagDefault LUA_TTABLE 31#define TagDefault LUA_TTABLE
32 32
33 33
34#define hashnum(t,n) (&t->node[lmod((luint32)(lint32)(n), t->size)]) 34#define hashnum(t,n) (&t->node[lmod((lu_hash)(ls_hash)(n), t->size)])
35#define hashstr(t,str) (&t->node[lmod((str)->u.s.hash, t->size)]) 35#define hashstr(t,str) (&t->node[lmod((str)->u.s.hash, t->size)])
36#define hashpointer(t,p) (&t->node[lmod(IntPoint(p), t->size)]) 36#define hashpointer(t,p) (&t->node[lmod(IntPoint(p), t->size)])
37 37
@@ -81,12 +81,26 @@ int luaH_nexti (Hash *t, int i) {
81} 81}
82 82
83 83
84static void setnodevector (lua_State *L, Hash *t, luint32 size) { 84#define check_grow(L, p, n) \
85 if ((p) >= MAX_INT/(n)) luaD_error(L, "table overflow");
86
87/*
88** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
89*/
90static int power2 (lua_State *L, int n) {
91 int p = MINPOWER2;
92 while (p <= n) {
93 check_grow(L, p, 2);
94 p *= 2;
95 }
96 return p;
97}
98
99
100static void setnodevector (lua_State *L, Hash *t, int size) {
85 int i; 101 int i;
86 if (size > MAX_INT)
87 luaD_error(L, "table overflow");
88 t->node = luaM_newvector(L, size, Node); 102 t->node = luaM_newvector(L, size, Node);
89 for (i=0; i<(int)size; i++) { 103 for (i=0; i<size; i++) {
90 t->node[i].next = NULL; 104 t->node[i].next = NULL;
91 t->node[i].key_tt = LUA_TNIL; 105 t->node[i].key_tt = LUA_TNIL;
92 setnilvalue(&t->node[i].val); 106 setnilvalue(&t->node[i].val);
@@ -104,7 +118,7 @@ Hash *luaH_new (lua_State *L, int size) {
104 t->mark = t; 118 t->mark = t;
105 t->size = 0; 119 t->size = 0;
106 t->node = NULL; 120 t->node = NULL;
107 setnodevector(L, t, luaO_power2(size)); 121 setnodevector(L, t, power2(L, size));
108 return t; 122 return t;
109} 123}
110 124
@@ -134,13 +148,15 @@ static void rehash (lua_State *L, Hash *t) {
134 int nelems = numuse(t); 148 int nelems = numuse(t);
135 int i; 149 int i;
136 lua_assert(nelems<=oldsize); 150 lua_assert(nelems<=oldsize);
137 if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */ 151 if (nelems >= oldsize-oldsize/4) { /* using more than 3/4? */
138 setnodevector(L, t, (luint32)oldsize*2); 152 check_grow(L, oldsize, 2);
153 setnodevector(L, t, oldsize*2); /* grow array */
154 }
139 else if (nelems <= oldsize/4 && /* less than 1/4? */ 155 else if (nelems <= oldsize/4 && /* less than 1/4? */
140 oldsize > MINPOWER2) 156 oldsize > MINPOWER2)
141 setnodevector(L, t, oldsize/2); 157 setnodevector(L, t, oldsize/2); /* shrink array */
142 else 158 else
143 setnodevector(L, t, oldsize); 159 setnodevector(L, t, oldsize); /* just rehash; keep the same size */
144 for (i=0; i<oldsize; i++) { 160 for (i=0; i<oldsize; i++) {
145 Node *old = nold+i; 161 Node *old = nold+i;
146 if (ttype(&old->val) != LUA_TNIL) { 162 if (ttype(&old->val) != LUA_TNIL) {