diff options
-rw-r--r-- | ltable.c | 42 |
1 files changed, 18 insertions, 24 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.22 1999/05/21 19:41:49 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.23 1999/08/16 20:52:00 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 | */ |
@@ -54,8 +54,8 @@ static long int hashindex (const TObject *ref) { | |||
54 | 54 | ||
55 | 55 | ||
56 | Node *luaH_present (const Hash *t, const TObject *key) { | 56 | Node *luaH_present (const Hash *t, const TObject *key) { |
57 | int tsize = nhash(t); | 57 | const int tsize = nhash(t); |
58 | long int h = hashindex(key); | 58 | const long int h = hashindex(key); |
59 | int h1 = h%tsize; | 59 | int h1 = h%tsize; |
60 | Node *n = node(t, h1); | 60 | Node *n = node(t, h1); |
61 | /* keep looking until an entry with "ref" equal to key or nil */ | 61 | /* keep looking until an entry with "ref" equal to key or nil */ |
@@ -81,7 +81,7 @@ void luaH_free (Hash *frees) { | |||
81 | 81 | ||
82 | 82 | ||
83 | static Node *hashnodecreate (int nhash) { | 83 | static Node *hashnodecreate (int nhash) { |
84 | Node *v = luaM_newvector(nhash, Node); | 84 | Node *const v = luaM_newvector(nhash, Node); |
85 | int i; | 85 | int i; |
86 | for (i=0; i<nhash; i++) | 86 | for (i=0; i<nhash; i++) |
87 | ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL; | 87 | ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL; |
@@ -90,7 +90,7 @@ static Node *hashnodecreate (int nhash) { | |||
90 | 90 | ||
91 | 91 | ||
92 | Hash *luaH_new (int nhash) { | 92 | Hash *luaH_new (int nhash) { |
93 | Hash *t = luaM_new(Hash); | 93 | Hash *const t = luaM_new(Hash); |
94 | nhash = luaO_redimension(nhash*3/2); | 94 | nhash = luaO_redimension(nhash*3/2); |
95 | nodevector(t) = hashnodecreate(nhash); | 95 | nodevector(t) = hashnodecreate(nhash); |
96 | nhash(t) = nhash; | 96 | nhash(t) = nhash; |
@@ -103,22 +103,22 @@ Hash *luaH_new (int nhash) { | |||
103 | 103 | ||
104 | 104 | ||
105 | static int newsize (Hash *t) { | 105 | static int newsize (Hash *t) { |
106 | Node *v = t->node; | 106 | Node *const v = t->node; |
107 | int size = nhash(t); | 107 | const int size = nhash(t); |
108 | int realuse = 0; | 108 | int realuse = 0; |
109 | int i; | 109 | int i; |
110 | for (i=0; i<size; i++) { | 110 | for (i=0; i<size; i++) { |
111 | if (ttype(val(v+i)) != LUA_T_NIL) | 111 | if (ttype(val(v+i)) != LUA_T_NIL) |
112 | realuse++; | 112 | realuse++; |
113 | } | 113 | } |
114 | return luaO_redimension((realuse+1)*2); /* +1 is the new element */ | 114 | return luaO_redimension(realuse*2); |
115 | } | 115 | } |
116 | 116 | ||
117 | 117 | ||
118 | static void rehash (Hash *t) { | 118 | static void rehash (Hash *t) { |
119 | int nold = nhash(t); | 119 | const int nold = nhash(t); |
120 | Node *vold = nodevector(t); | 120 | Node *const vold = nodevector(t); |
121 | int nnew = newsize(t); | 121 | const int nnew = newsize(t); |
122 | int i; | 122 | int i; |
123 | nodevector(t) = hashnodecreate(nnew); | 123 | nodevector(t) = hashnodecreate(nnew); |
124 | nhash(t) = nnew; | 124 | nhash(t) = nnew; |
@@ -136,25 +136,19 @@ static void rehash (Hash *t) { | |||
136 | 136 | ||
137 | 137 | ||
138 | void luaH_set (Hash *t, const TObject *ref, const TObject *val) { | 138 | void luaH_set (Hash *t, const TObject *ref, const TObject *val) { |
139 | Node *n = luaH_present(t, ref); | 139 | Node *const n = luaH_present(t, ref); |
140 | if (ttype(ref(n)) != LUA_T_NIL) | 140 | *val(n) = *val; |
141 | *val(n) = *val; | 141 | if (ttype(ref(n)) == LUA_T_NIL) { /* new node? */ |
142 | else { | 142 | *ref(n) = *ref; /* set key */ |
143 | TObject buff; | 143 | nuse(t)++; /* count it */ |
144 | buff = *val; /* rehash may invalidate this address */ | 144 | if ((long)nuse(t)*3L > (long)nhash(t)*2L) /* check size */ |
145 | if ((long)nuse(t)*3L > (long)nhash(t)*2L) { | ||
146 | rehash(t); | 145 | rehash(t); |
147 | n = luaH_present(t, ref); | ||
148 | } | ||
149 | nuse(t)++; | ||
150 | *ref(n) = *ref; | ||
151 | *val(n) = buff; | ||
152 | } | 146 | } |
153 | } | 147 | } |
154 | 148 | ||
155 | 149 | ||
156 | int luaH_pos (const Hash *t, const TObject *r) { | 150 | int luaH_pos (const Hash *t, const TObject *r) { |
157 | Node *n = luaH_present(t, r); | 151 | Node *const n = luaH_present(t, r); |
158 | luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); | 152 | luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); |
159 | return n-(t->node); | 153 | return n-(t->node); |
160 | } | 154 | } |