/* ** returns the `main' position of an element in a table (that is, the index ** of its hash value) ** ** Floating point numbers with integer value give the hash position of the ** integer (so they use the same table position). */ static Node *mainposition (const Table *t, const TValue *key) { switch (ttype(key)) { #ifdef LUA_TINT case LUA_TINT: return hashint(t,ivalue(key)); #endif case LUA_TNUMBER: { #ifdef LUA_TINT lua_Integer i; if (tt_integer_valued(key,&i)) return hashint(t, i); # ifdef LNUM_COMPLEX /* Complex numbers are hashed by their scalar part. Pure imaginary values * with scalar 0 or -0 should give same hash. */ if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0)) return gnode(t, 0); /* 0 and -0 to give same hash */ # endif #else if (luai_numeq(nvalue(key),0)) return gnode(t, 0); /* 0 and -0 to give same hash */ #endif return hashnum(t,nvalue_fast(key)); } case LUA_TSTRING: return hashstr(t, rawtsvalue(key)); case LUA_TBOOLEAN: return hashboolean(t, bvalue(key)); case LUA_TLIGHTUSERDATA: return hashpointer(t, pvalue(key)); default: return hashpointer(t, gcvalue(key)); } }
static Node *mainposition (const Table *t, const Tvalue *key) { switch (ttype(key)) { case KTAP_TNUMBER: return hashnum(t, nvalue(key)); case KTAP_TLNGSTR: { Tstring *s = rawtsvalue(key); if (s->tsv.extra == 0) { /* no hash? */ s->tsv.hash = kp_string_hash(getstr(s), s->tsv.len, s->tsv.hash); s->tsv.extra = 1; /* now it has its hash */ } return hashstr(t, rawtsvalue(key)); } case KTAP_TSHRSTR: return hashstr(t, rawtsvalue(key)); case KTAP_TBOOLEAN: return hashboolean(t, bvalue(key)); case KTAP_TLIGHTUSERDATA: return hashpointer(t, pvalue(key)); case KTAP_TLCF: return hashpointer(t, fvalue(key)); default: return hashpointer(t, gcvalue(key)); } }
static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ], unsigned int idx) { struct genericstrtable *h; for (h = t[hashnum(idx)]; h; h = h->next) if (h->num == idx) return h->name; return NULL; }
const char *names_videoterminal(u_int16_t termt) { struct videoterminal *vt; vt = videoterminals_hash[hashnum(termt)]; for (; vt; vt = vt->next) if (vt->termt == termt) return vt->name; return NULL; }
const char *names_audioterminal(u_int16_t termt) { struct audioterminal *at; at = audioterminals_hash[hashnum(termt)]; for (; at; at = at->next) if (at->termt == termt) return at->name; return NULL; }
/* Hash an arbitrary key and return its anchor position in the hash table. */ static Node *hashkey(const GCtab *t, cTValue *key) { if (tvisstr(key)) return hashstr(t, strV(key)); else if (tvisnum(key)) return hashnum(t, key); else if (tvisbool(key)) return hashmask(t, boolV(key)); else return hashgcref(t, key->gcr); /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */ }
static int hash_videoterminal(struct videoterminal *vt) { struct videoterminal *vt_old; unsigned int h = hashnum(vt->termt); for (vt_old = videoterminals_hash[h]; vt_old; vt_old = vt_old->next) if (vt_old->termt == vt->termt) return -1; vt->next = videoterminals_hash[h]; videoterminals_hash[h] = vt; return 0; }
static int hash_audioterminal(struct audioterminal *at) { struct audioterminal *at_old; unsigned int h = hashnum(at->termt); for (at_old = audioterminals_hash[h]; at_old; at_old = at_old->next) if (at_old->termt == at->termt) return -1; at->next = audioterminals_hash[h]; audioterminals_hash[h] = at; return 0; }
static int hash_genericstrtable(struct genericstrtable *t[HASHSZ], struct genericstrtable *g) { struct genericstrtable *g_old; unsigned int h = hashnum(g->num); for (g_old = t[h]; g_old; g_old = g_old->next) if (g_old->num == g->num) return -1; g->next = t[h]; t[h] = g; return 0; }
/* ** returns the `main' position of an element in a table (that is, the index ** of its hash value) */ static Node *mainposition (const Table *t, const TValue *key) { switch (ttype(key)) { case LV_TNUMBER: return hashnum(t, nvalue(key)); case LV_TSTRING: return hashstr(t, rawtsvalue(key)); case LV_TBOOLEAN: return hashboolean(t, bvalue(key)); case LV_TLIGHTUSERDATA: return hashpointer(t, pvalue(key)); default: return hashpointer(t, gcvalue(key)); } }
const Tvalue *kp_table_getint(Table *t, int key) { Node *n; if ((unsigned int)(key - 1) < (unsigned int)t->sizearray) return &t->array[key - 1]; n = hashnum(t, key); do { if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == key) return gval(n); else n = gnext(n); } while (n); return ktap_nilobject; }