rlm@1: /* rlm@1: ** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ rlm@1: ** Lua tables (hash) rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: /* rlm@1: ** Implementation of tables (aka arrays, objects, or hash tables). rlm@1: ** Tables keep its elements in two parts: an array part and a hash part. rlm@1: ** Non-negative integer keys are all candidates to be kept in the array rlm@1: ** part. The actual size of the array is the largest `n' such that at rlm@1: ** least half the slots between 0 and n are in use. rlm@1: ** Hash uses a mix of chained scatter table with Brent's variation. rlm@1: ** A main invariant of these tables is that, if an element is not rlm@1: ** in its main position (i.e. the `original' position that its hash gives rlm@1: ** to it), then the colliding element is in its own main position. rlm@1: ** Hence even when the load factor reaches 100%, performance remains good. rlm@1: */ rlm@1: rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define ltable_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "ldebug.h" rlm@1: #include "ldo.h" rlm@1: #include "lgc.h" rlm@1: #include "lmem.h" rlm@1: #include "lobject.h" rlm@1: #include "lstate.h" rlm@1: #include "ltable.h" rlm@1: rlm@1: rlm@1: /* rlm@1: ** max size of array part is 2^MAXBITS rlm@1: */ rlm@1: #if LUAI_BITSINT > 26 rlm@1: #define MAXBITS 26 rlm@1: #else rlm@1: #define MAXBITS (LUAI_BITSINT-2) rlm@1: #endif rlm@1: rlm@1: #define MAXASIZE (1 << MAXBITS) rlm@1: rlm@1: rlm@1: #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) rlm@1: rlm@1: #define hashstr(t,str) hashpow2(t, (str)->tsv.hash) rlm@1: #define hashboolean(t,p) hashpow2(t, p) rlm@1: rlm@1: rlm@1: /* rlm@1: ** for some types, it is better to avoid modulus by power of 2, as rlm@1: ** they tend to have many 2 factors. rlm@1: */ rlm@1: #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) rlm@1: rlm@1: rlm@1: #define hashpointer(t,p) hashmod(t, IntPoint(p)) rlm@1: rlm@1: rlm@1: /* rlm@1: ** number of ints inside a lua_Number rlm@1: */ rlm@1: #define numints cast_int(sizeof(lua_Number)/sizeof(int)) rlm@1: rlm@1: rlm@1: rlm@1: #define dummynode (&dummynode_) rlm@1: rlm@1: static const Node dummynode_ = { rlm@1: {{NULL}, LUA_TNIL}, /* value */ rlm@1: {{{NULL}, LUA_TNIL, NULL}} /* key */ rlm@1: }; rlm@1: rlm@1: rlm@1: /* rlm@1: ** hash for lua_Numbers rlm@1: */ rlm@1: static Node *hashnum (const Table *t, lua_Number n) { rlm@1: unsigned int a[numints]; rlm@1: int i; rlm@1: if (luai_numeq(n, 0)) /* avoid problems with -0 */ rlm@1: return gnode(t, 0); rlm@1: memcpy(a, &n, sizeof(a)); rlm@1: for (i = 1; i < numints; i++) a[0] += a[i]; rlm@1: return hashmod(t, a[0]); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** returns the `main' position of an element in a table (that is, the index rlm@1: ** of its hash value) rlm@1: */ rlm@1: static Node *mainposition (const Table *t, const TValue *key) { rlm@1: switch (ttype(key)) { rlm@1: case LUA_TNUMBER: rlm@1: return hashnum(t, nvalue(key)); rlm@1: case LUA_TSTRING: rlm@1: return hashstr(t, rawtsvalue(key)); rlm@1: case LUA_TBOOLEAN: rlm@1: return hashboolean(t, bvalue(key)); rlm@1: case LUA_TLIGHTUSERDATA: rlm@1: return hashpointer(t, pvalue(key)); rlm@1: default: rlm@1: return hashpointer(t, gcvalue(key)); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** returns the index for `key' if `key' is an appropriate key to live in rlm@1: ** the array part of the table, -1 otherwise. rlm@1: */ rlm@1: static int arrayindex (const TValue *key) { rlm@1: if (ttisnumber(key)) { rlm@1: lua_Number n = nvalue(key); rlm@1: int k; rlm@1: lua_number2int(k, n); rlm@1: if (luai_numeq(cast_num(k), n)) rlm@1: return k; rlm@1: } rlm@1: return -1; /* `key' did not match some condition */ rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** returns the index of a `key' for table traversals. First goes all rlm@1: ** elements in the array part, then elements in the hash part. The rlm@1: ** beginning of a traversal is signalled by -1. rlm@1: */ rlm@1: static int findindex (lua_State *L, Table *t, StkId key) { rlm@1: int i; rlm@1: if (ttisnil(key)) return -1; /* first iteration */ rlm@1: i = arrayindex(key); rlm@1: if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ rlm@1: return i-1; /* yes; that's the index (corrected to C) */ rlm@1: else { rlm@1: Node *n = mainposition(t, key); rlm@1: do { /* check whether `key' is somewhere in the chain */ rlm@1: /* key may be dead already, but it is ok to use it in `next' */ rlm@1: if (luaO_rawequalObj(key2tval(n), key) || rlm@1: (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && rlm@1: gcvalue(gkey(n)) == gcvalue(key))) { rlm@1: i = cast_int(n - gnode(t, 0)); /* key index in hash table */ rlm@1: /* hash elements are numbered after array ones */ rlm@1: return i + t->sizearray; rlm@1: } rlm@1: else n = gnext(n); rlm@1: } while (n); rlm@1: luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ rlm@1: return 0; /* to avoid warnings */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: int luaH_next (lua_State *L, Table *t, StkId key) { rlm@1: int i = findindex(L, t, key); /* find original element */ rlm@1: for (i++; i < t->sizearray; i++) { /* try first array part */ rlm@1: if (!ttisnil(&t->array[i])) { /* a non-nil value? */ rlm@1: setnvalue(key, cast_num(i+1)); rlm@1: setobj2s(L, key+1, &t->array[i]); rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ rlm@1: if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ rlm@1: setobj2s(L, key, key2tval(gnode(t, i))); rlm@1: setobj2s(L, key+1, gval(gnode(t, i))); rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: return 0; /* no more elements */ rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** {============================================================= rlm@1: ** Rehash rlm@1: ** ============================================================== rlm@1: */ rlm@1: rlm@1: rlm@1: static int computesizes (int nums[], int *narray) { rlm@1: int i; rlm@1: int twotoi; /* 2^i */ rlm@1: int a = 0; /* number of elements smaller than 2^i */ rlm@1: int na = 0; /* number of elements to go to array part */ rlm@1: int n = 0; /* optimal size for array part */ rlm@1: for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { rlm@1: if (nums[i] > 0) { rlm@1: a += nums[i]; rlm@1: if (a > twotoi/2) { /* more than half elements present? */ rlm@1: n = twotoi; /* optimal size (till now) */ rlm@1: na = a; /* all elements smaller than n will go to array part */ rlm@1: } rlm@1: } rlm@1: if (a == *narray) break; /* all elements already counted */ rlm@1: } rlm@1: *narray = n; rlm@1: lua_assert(*narray/2 <= na && na <= *narray); rlm@1: return na; rlm@1: } rlm@1: rlm@1: rlm@1: static int countint (const TValue *key, int *nums) { rlm@1: int k = arrayindex(key); rlm@1: if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ rlm@1: nums[ceillog2(k)]++; /* count as such */ rlm@1: return 1; rlm@1: } rlm@1: else rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static int numusearray (const Table *t, int *nums) { rlm@1: int lg; rlm@1: int ttlg; /* 2^lg */ rlm@1: int ause = 0; /* summation of `nums' */ rlm@1: int i = 1; /* count to traverse all array keys */ rlm@1: for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ rlm@1: int lc = 0; /* counter */ rlm@1: int lim = ttlg; rlm@1: if (lim > t->sizearray) { rlm@1: lim = t->sizearray; /* adjust upper limit */ rlm@1: if (i > lim) rlm@1: break; /* no more elements to count */ rlm@1: } rlm@1: /* count elements in range (2^(lg-1), 2^lg] */ rlm@1: for (; i <= lim; i++) { rlm@1: if (!ttisnil(&t->array[i-1])) rlm@1: lc++; rlm@1: } rlm@1: nums[lg] += lc; rlm@1: ause += lc; rlm@1: } rlm@1: return ause; rlm@1: } rlm@1: rlm@1: rlm@1: static int numusehash (const Table *t, int *nums, int *pnasize) { rlm@1: int totaluse = 0; /* total number of elements */ rlm@1: int ause = 0; /* summation of `nums' */ rlm@1: int i = sizenode(t); rlm@1: while (i--) { rlm@1: Node *n = &t->node[i]; rlm@1: if (!ttisnil(gval(n))) { rlm@1: ause += countint(key2tval(n), nums); rlm@1: totaluse++; rlm@1: } rlm@1: } rlm@1: *pnasize += ause; rlm@1: return totaluse; rlm@1: } rlm@1: rlm@1: rlm@1: static void setarrayvector (lua_State *L, Table *t, int size) { rlm@1: int i; rlm@1: luaM_reallocvector(L, t->array, t->sizearray, size, TValue); rlm@1: for (i=t->sizearray; iarray[i]); rlm@1: t->sizearray = size; rlm@1: } rlm@1: rlm@1: rlm@1: static void setnodevector (lua_State *L, Table *t, int size) { rlm@1: int lsize; rlm@1: if (size == 0) { /* no elements to hash part? */ rlm@1: t->node = cast(Node *, dummynode); /* use common `dummynode' */ rlm@1: lsize = 0; rlm@1: } rlm@1: else { rlm@1: int i; rlm@1: lsize = ceillog2(size); rlm@1: if (lsize > MAXBITS) rlm@1: luaG_runerror(L, "table overflow"); rlm@1: size = twoto(lsize); rlm@1: t->node = luaM_newvector(L, size, Node); rlm@1: for (i=0; ilsizenode = cast_byte(lsize); rlm@1: t->lastfree = gnode(t, size); /* all positions are free */ rlm@1: } rlm@1: rlm@1: rlm@1: static void resize (lua_State *L, Table *t, int nasize, int nhsize) { rlm@1: int i; rlm@1: int oldasize = t->sizearray; rlm@1: int oldhsize = t->lsizenode; rlm@1: Node *nold = t->node; /* save old hash ... */ rlm@1: if (nasize > oldasize) /* array part must grow? */ rlm@1: setarrayvector(L, t, nasize); rlm@1: /* create new hash part with appropriate size */ rlm@1: setnodevector(L, t, nhsize); rlm@1: if (nasize < oldasize) { /* array part must shrink? */ rlm@1: t->sizearray = nasize; rlm@1: /* re-insert elements from vanishing slice */ rlm@1: for (i=nasize; iarray[i])) rlm@1: setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); rlm@1: } rlm@1: /* shrink array */ rlm@1: luaM_reallocvector(L, t->array, oldasize, nasize, TValue); rlm@1: } rlm@1: /* re-insert elements from hash part */ rlm@1: for (i = twoto(oldhsize) - 1; i >= 0; i--) { rlm@1: Node *old = nold+i; rlm@1: if (!ttisnil(gval(old))) rlm@1: setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); rlm@1: } rlm@1: if (nold != dummynode) rlm@1: luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ rlm@1: } rlm@1: rlm@1: rlm@1: void luaH_resizearray (lua_State *L, Table *t, int nasize) { rlm@1: int nsize = (t->node == dummynode) ? 0 : sizenode(t); rlm@1: resize(L, t, nasize, nsize); rlm@1: } rlm@1: rlm@1: rlm@1: static void rehash (lua_State *L, Table *t, const TValue *ek) { rlm@1: int nasize, na; rlm@1: int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ rlm@1: int i; rlm@1: int totaluse; rlm@1: for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ rlm@1: nasize = numusearray(t, nums); /* count keys in array part */ rlm@1: totaluse = nasize; /* all those keys are integer keys */ rlm@1: totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ rlm@1: /* count extra key */ rlm@1: nasize += countint(ek, nums); rlm@1: totaluse++; rlm@1: /* compute new size for array part */ rlm@1: na = computesizes(nums, &nasize); rlm@1: /* resize the table to new computed sizes */ rlm@1: resize(L, t, nasize, totaluse - na); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** }============================================================= rlm@1: */ rlm@1: rlm@1: rlm@1: Table *luaH_new (lua_State *L, int narray, int nhash) { rlm@1: Table *t = luaM_new(L, Table); rlm@1: luaC_link(L, obj2gco(t), LUA_TTABLE); rlm@1: t->metatable = NULL; rlm@1: t->flags = cast_byte(~0); rlm@1: /* temporary values (kept only if some malloc fails) */ rlm@1: t->array = NULL; rlm@1: t->sizearray = 0; rlm@1: t->lsizenode = 0; rlm@1: t->node = cast(Node *, dummynode); rlm@1: setarrayvector(L, t, narray); rlm@1: setnodevector(L, t, nhash); rlm@1: return t; rlm@1: } rlm@1: rlm@1: rlm@1: void luaH_free (lua_State *L, Table *t) { rlm@1: if (t->node != dummynode) rlm@1: luaM_freearray(L, t->node, sizenode(t), Node); rlm@1: luaM_freearray(L, t->array, t->sizearray, TValue); rlm@1: luaM_free(L, t); rlm@1: } rlm@1: rlm@1: rlm@1: static Node *getfreepos (Table *t) { rlm@1: while (t->lastfree-- > t->node) { rlm@1: if (ttisnil(gkey(t->lastfree))) rlm@1: return t->lastfree; rlm@1: } rlm@1: return NULL; /* could not find a free place */ rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** inserts a new key into a hash table; first, check whether key's main rlm@1: ** position is free. If not, check whether colliding node is in its main rlm@1: ** position or not: if it is not, move colliding node to an empty place and rlm@1: ** put new key in its main position; otherwise (colliding node is in its main rlm@1: ** position), new key goes to an empty position. rlm@1: */ rlm@1: static TValue *newkey (lua_State *L, Table *t, const TValue *key) { rlm@1: Node *mp = mainposition(t, key); rlm@1: if (!ttisnil(gval(mp)) || mp == dummynode) { rlm@1: Node *othern; rlm@1: Node *n = getfreepos(t); /* get a free place */ rlm@1: if (n == NULL) { /* cannot find a free place? */ rlm@1: rehash(L, t, key); /* grow table */ rlm@1: return luaH_set(L, t, key); /* re-insert key into grown table */ rlm@1: } rlm@1: lua_assert(n != dummynode); rlm@1: othern = mainposition(t, key2tval(mp)); rlm@1: if (othern != mp) { /* is colliding node out of its main position? */ rlm@1: /* yes; move colliding node into free position */ rlm@1: while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ rlm@1: gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ rlm@1: *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ rlm@1: gnext(mp) = NULL; /* now `mp' is free */ rlm@1: setnilvalue(gval(mp)); rlm@1: } rlm@1: else { /* colliding node is in its own main position */ rlm@1: /* new node will go into free position */ rlm@1: gnext(n) = gnext(mp); /* chain new position */ rlm@1: gnext(mp) = n; rlm@1: mp = n; rlm@1: } rlm@1: } rlm@1: gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; rlm@1: luaC_barriert(L, t, key); rlm@1: lua_assert(ttisnil(gval(mp))); rlm@1: return gval(mp); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** search function for integers rlm@1: */ rlm@1: const TValue *luaH_getnum (Table *t, int key) { rlm@1: /* (1 <= key && key <= t->sizearray) */ rlm@1: if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) rlm@1: return &t->array[key-1]; rlm@1: else { rlm@1: lua_Number nk = cast_num(key); rlm@1: Node *n = hashnum(t, nk); rlm@1: do { /* check whether `key' is somewhere in the chain */ rlm@1: if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) rlm@1: return gval(n); /* that's it */ rlm@1: else n = gnext(n); rlm@1: } while (n); rlm@1: return luaO_nilobject; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** search function for strings rlm@1: */ rlm@1: const TValue *luaH_getstr (Table *t, TString *key) { rlm@1: Node *n = hashstr(t, key); rlm@1: do { /* check whether `key' is somewhere in the chain */ rlm@1: if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) rlm@1: return gval(n); /* that's it */ rlm@1: else n = gnext(n); rlm@1: } while (n); rlm@1: return luaO_nilobject; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** main search function rlm@1: */ rlm@1: const TValue *luaH_get (Table *t, const TValue *key) { rlm@1: switch (ttype(key)) { rlm@1: case LUA_TNIL: return luaO_nilobject; rlm@1: case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); rlm@1: case LUA_TNUMBER: { rlm@1: int k; rlm@1: lua_Number n = nvalue(key); rlm@1: lua_number2int(k, n); rlm@1: if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ rlm@1: return luaH_getnum(t, k); /* use specialized version */ rlm@1: /* else go through */ rlm@1: } rlm@1: default: { rlm@1: Node *n = mainposition(t, key); rlm@1: do { /* check whether `key' is somewhere in the chain */ rlm@1: if (luaO_rawequalObj(key2tval(n), key)) rlm@1: return gval(n); /* that's it */ rlm@1: else n = gnext(n); rlm@1: } while (n); rlm@1: return luaO_nilobject; rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { rlm@1: const TValue *p = luaH_get(t, key); rlm@1: t->flags = 0; rlm@1: if (p != luaO_nilobject) rlm@1: return cast(TValue *, p); rlm@1: else { rlm@1: if (ttisnil(key)) luaG_runerror(L, "table index is nil"); rlm@1: else if (ttisnumber(key) && luai_numisnan(nvalue(key))) rlm@1: luaG_runerror(L, "table index is NaN"); rlm@1: return newkey(L, t, key); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: TValue *luaH_setnum (lua_State *L, Table *t, int key) { rlm@1: const TValue *p = luaH_getnum(t, key); rlm@1: if (p != luaO_nilobject) rlm@1: return cast(TValue *, p); rlm@1: else { rlm@1: TValue k; rlm@1: setnvalue(&k, cast_num(key)); rlm@1: return newkey(L, t, &k); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { rlm@1: const TValue *p = luaH_getstr(t, key); rlm@1: if (p != luaO_nilobject) rlm@1: return cast(TValue *, p); rlm@1: else { rlm@1: TValue k; rlm@1: setsvalue(L, &k, key); rlm@1: return newkey(L, t, &k); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int unbound_search (Table *t, unsigned int j) { rlm@1: unsigned int i = j; /* i is zero or a present index */ rlm@1: j++; rlm@1: /* find `i' and `j' such that i is present and j is not */ rlm@1: while (!ttisnil(luaH_getnum(t, j))) { rlm@1: i = j; rlm@1: j *= 2; rlm@1: if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ rlm@1: /* table was built with bad purposes: resort to linear search */ rlm@1: i = 1; rlm@1: while (!ttisnil(luaH_getnum(t, i))) i++; rlm@1: return i - 1; rlm@1: } rlm@1: } rlm@1: /* now do a binary search between them */ rlm@1: while (j - i > 1) { rlm@1: unsigned int m = (i+j)/2; rlm@1: if (ttisnil(luaH_getnum(t, m))) j = m; rlm@1: else i = m; rlm@1: } rlm@1: return i; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** Try to find a boundary in table `t'. A `boundary' is an integer index rlm@1: ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). rlm@1: */ rlm@1: int luaH_getn (Table *t) { rlm@1: unsigned int j = t->sizearray; rlm@1: if (j > 0 && ttisnil(&t->array[j - 1])) { rlm@1: /* there is a boundary in the array part: (binary) search for it */ rlm@1: unsigned int i = 0; rlm@1: while (j - i > 1) { rlm@1: unsigned int m = (i+j)/2; rlm@1: if (ttisnil(&t->array[m - 1])) j = m; rlm@1: else i = m; rlm@1: } rlm@1: return i; rlm@1: } rlm@1: /* else must find a boundary in hash part */ rlm@1: else if (t->node == dummynode) /* hash part is empty? */ rlm@1: return j; /* that is easy... */ rlm@1: else return unbound_search(t, j); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: #if defined(LUA_DEBUG) rlm@1: rlm@1: Node *luaH_mainposition (const Table *t, const TValue *key) { rlm@1: return mainposition(t, key); rlm@1: } rlm@1: rlm@1: int luaH_isdummy (Node *n) { return n == dummynode; } rlm@1: rlm@1: #endif