summaryrefslogtreecommitdiff
path: root/src/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltable.c')
-rw-r--r--src/ltable.c78
1 files changed, 46 insertions, 32 deletions
diff --git a/src/ltable.c b/src/ltable.c
index 04f2a34..7e15b71 100644
--- a/src/ltable.c
+++ b/src/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 2.111 2015/06/09 14:21:13 roberto Exp $
+** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -85,7 +85,7 @@ static const Node dummynode_ = {
/*
** Hash for floating-point numbers.
** The main computation should be just
-** n = frepx(n, &i); return (n * INT_MAX) + i
+** n = frexp(n, &i); return (n * INT_MAX) + i
** but there are some numerical subtleties.
** In a two-complement representation, INT_MAX does not has an exact
** representation as a float, but INT_MIN does; because the absolute
@@ -101,7 +101,7 @@ static int l_hashfloat (lua_Number n) {
lua_Integer ni;
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
- lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL);
+ lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
return 0;
}
else { /* normal case */
@@ -124,14 +124,8 @@ static Node *mainposition (const Table *t, const TValue *key) {
return hashmod(t, l_hashfloat(fltvalue(key)));
case LUA_TSHRSTR:
return hashstr(t, tsvalue(key));
- case LUA_TLNGSTR: {
- TString *s = tsvalue(key);
- if (s->extra == 0) { /* no hash? */
- s->hash = luaS_hash(getstr(s), s->u.lnglen, s->hash);
- s->extra = 1; /* now it has its hash */
- }
- return hashstr(t, tsvalue(key));
- }
+ case LUA_TLNGSTR:
+ return hashpow2(t, luaS_hashlongstr(tsvalue(key)));
case LUA_TBOOLEAN:
return hashboolean(t, bvalue(key));
case LUA_TLIGHTUSERDATA:
@@ -139,6 +133,7 @@ static Node *mainposition (const Table *t, const TValue *key) {
case LUA_TLCF:
return hashpointer(t, fvalue(key));
default:
+ lua_assert(!ttisdeadkey(key));
return hashpointer(t, gcvalue(key));
}
}
@@ -463,7 +458,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
Node *f = getfreepos(t); /* get a free place */
if (f == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */
- /* whatever called 'newkey' takes care of TM cache and GC barrier */
+ /* whatever called 'newkey' takes care of TM cache */
return luaH_set(L, t, key); /* insert key into grown table */
}
lua_assert(!isdummy(f));
@@ -501,7 +496,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
*/
const TValue *luaH_getint (Table *t, lua_Integer key) {
/* (1 <= key && key <= t->sizearray) */
- if (l_castS2U(key - 1) < t->sizearray)
+ if (l_castS2U(key) - 1 < t->sizearray)
return &t->array[key - 1];
else {
Node *n = hashint(t, key);
@@ -513,7 +508,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
if (nx == 0) break;
n += nx;
}
- };
+ }
return luaO_nilobject;
}
}
@@ -522,7 +517,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
/*
** search function for short strings
*/
-const TValue *luaH_getstr (Table *t, TString *key) {
+const TValue *luaH_getshortstr (Table *t, TString *key) {
Node *n = hashstr(t, key);
lua_assert(key->tt == LUA_TSHRSTR);
for (;;) { /* check whether 'key' is somewhere in the chain */
@@ -531,11 +526,41 @@ const TValue *luaH_getstr (Table *t, TString *key) {
return gval(n); /* that's it */
else {
int nx = gnext(n);
- if (nx == 0) break;
+ if (nx == 0)
+ return luaO_nilobject; /* not found */
n += nx;
}
- };
- return luaO_nilobject;
+ }
+}
+
+
+/*
+** "Generic" get version. (Not that generic: not valid for integers,
+** which may be in array part, nor for floats with integral values.)
+*/
+static const TValue *getgeneric (Table *t, const TValue *key) {
+ Node *n = mainposition(t, key);
+ for (;;) { /* check whether 'key' is somewhere in the chain */
+ if (luaV_rawequalobj(gkey(n), key))
+ return gval(n); /* that's it */
+ else {
+ int nx = gnext(n);
+ if (nx == 0)
+ return luaO_nilobject; /* not found */
+ n += nx;
+ }
+ }
+}
+
+
+const TValue *luaH_getstr (Table *t, TString *key) {
+ if (key->tt == LUA_TSHRSTR)
+ return luaH_getshortstr(t, key);
+ else { /* for long strings, use generic case */
+ TValue ko;
+ setsvalue(cast(lua_State *, NULL), &ko, key);
+ return getgeneric(t, &ko);
+ }
}
@@ -544,7 +569,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
*/
const TValue *luaH_get (Table *t, const TValue *key) {
switch (ttype(key)) {
- case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key));
+ case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
case LUA_TNIL: return luaO_nilobject;
case LUA_TNUMFLT: {
@@ -553,19 +578,8 @@ const TValue *luaH_get (Table *t, const TValue *key) {
return luaH_getint(t, k); /* use specialized version */
/* else... */
} /* FALLTHROUGH */
- default: {
- Node *n = mainposition(t, key);
- for (;;) { /* check whether 'key' is somewhere in the chain */
- if (luaV_rawequalobj(gkey(n), key))
- return gval(n); /* that's it */
- else {
- int nx = gnext(n);
- if (nx == 0) break;
- n += nx;
- }
- };
- return luaO_nilobject;
- }
+ default:
+ return getgeneric(t, key);
}
}