5 /* A HashRec lives in a single allocated block. The layout is the
6 * header struct, then a table of 2^lgsz hash entries (key/value
7 * pairs), then an index table of 2*2^lgsz integers storing index
8 * values into the entry table. There are two tokens needed for
9 * "unused" and "used but empty". */
12 #define ENT_DELETED -2
14 typedef struct { naRef key, val; } HashEnt;
16 typedef struct HashRec {
17 int size; /* number of active entries */
18 int lgsz; /* base-2 logarithm of the allocated (!) size */
19 int next; /* next entry to use */
22 #define REC(h) (PTR(h).hash->rec)
23 #define POW2(n) (1<<(n))
24 #define NCELLS(hr) (2*POW2((hr)->lgsz))
25 #define ROUNDUPOFF(n,m) ((((n)+(m-1))/m)*m)-(n)
26 #define ALIGN(p,sz) (((char*)p)+ROUNDUPOFF(((size_t)p)%sz,sz))
27 #define ENTS(h) ((HashEnt*)ALIGN(&((HashRec*)h)[1],sizeof(naRef)))
28 #define TAB(h) ((int*)&(ENTS(h)[1<<(h)->lgsz]))
29 #define HBITS(hr,code) ((hr)->lgsz ? ((code)>>(32-(hr)->lgsz)) : 0)
31 #define LROT(h,n) (((h)<<n)|((h)>>((8*sizeof(h))-n)))
32 static unsigned int mix32(unsigned int h)
34 h ^= 0x2e63823a; h += LROT(h, 15); h -= LROT(h, 9);
35 h += LROT(h, 4); h -= LROT(h, 1); h ^= LROT(h, 2);
38 static unsigned int hash32(const unsigned char* in, int len)
40 unsigned int h = len, val = 0;
42 for(i=0; i<len; i++) {
43 val = (val<<8) ^ in[i];
49 return mix32(h ^ val);
52 static unsigned int refhash(naRef key)
55 struct naStr* s = PTR(key).str;
56 if(s->hashcode) return s->hashcode;
57 return s->hashcode = hash32((void*)naStr_data(key), naStr_len(key));
58 } else { /* must be a number */
59 union { double d; unsigned int u[2]; } n;
60 n.d = key.num == -0.0 ? 0.0 : key.num; /* remember negative zero! */
61 return mix32(mix32(n.u[0]) ^ n.u[1]);
65 static int equal(naRef a, naRef b)
67 if(IS_NUM(a)) return a.num == b.num;
68 if(PTR(a).obj == PTR(b).obj) return 1;
69 if(naStr_len(a) != naStr_len(b)) return 0;
70 return memcmp(naStr_data(a), naStr_data(b), naStr_len(a)) == 0;
73 /* Returns the index of a cell that either contains a matching key, or
74 * is the empty slot to receive a new insertion. */
75 static int findcell(struct HashRec *hr, naRef key, unsigned int hash)
77 int i, mask = POW2(hr->lgsz+1)-1, step = (2*hash+1) & mask;
78 for(i=HBITS(hr,hash); TAB(hr)[i] != ENT_EMPTY; i=(i+step)&mask)
79 if(TAB(hr)[i] != ENT_DELETED && equal(key, ENTS(hr)[TAB(hr)[i]].key))
84 static void hashset(HashRec* hr, naRef key, naRef val)
86 int ent, cell = findcell(hr, key, refhash(key));
87 if((ent = TAB(hr)[cell]) == ENT_EMPTY) {
89 if(ent >= NCELLS(hr)) return; /* race protection, don't overrun */
92 ENTS(hr)[ent].key = key;
94 ENTS(hr)[ent].val = val;
97 static int recsize(int lgsz)
101 return (int)((char*)&TAB(&hr)[POW2(lgsz+1)] - (char*)&hr) + sizeof(naRef);
104 static HashRec* resize(struct naHash* hash)
106 HashRec *hr = hash->rec, *hr2;
109 int oldsz = hr->size;
110 while(oldsz) { oldsz >>= 1; lgsz++; }
112 hr2 = naAlloc(recsize(lgsz));
113 hr2->size = hr2->next = 0;
115 for(i=0; i<(2*(1<<lgsz)); i++)
116 TAB(hr2)[i] = ENT_EMPTY;
117 for(i=0; hr && i < POW2(hr->lgsz+1); i++)
119 hashset(hr2, ENTS(hr)[TAB(hr)[i]].key, ENTS(hr)[TAB(hr)[i]].val);
120 naGC_swapfree((void*)&hash->rec, hr2);
124 int naHash_size(naRef h) { return REC(h) ? REC(h)->size : 0; }
126 int naHash_get(naRef hash, naRef key, naRef* out)
128 HashRec* hr = REC(hash);
130 int ent, cell = findcell(hr, key, refhash(key));
131 if((ent = TAB(hr)[cell]) < 0) return 0;
132 *out = ENTS(hr)[ent].val;
138 void naHash_set(naRef hash, naRef key, naRef val)
140 HashRec* hr = REC(hash);
141 if(!hr || hr->next >= POW2(hr->lgsz))
142 hr = resize(PTR(hash).hash);
143 hashset(hr, key, val);
146 void naHash_delete(naRef hash, naRef key)
148 HashRec* hr = REC(hash);
150 int cell = findcell(hr, key, refhash(key));
151 if(TAB(hr)[cell] >= 0) {
152 TAB(hr)[cell] = ENT_DELETED;
153 if(--hr->size < POW2(hr->lgsz-1))
154 resize(PTR(hash).hash);
159 void naHash_keys(naRef dst, naRef hash)
162 HashRec* hr = REC(hash);
163 for(i=0; hr && i < NCELLS(hr); i++)
165 naVec_append(dst, ENTS(hr)[TAB(hr)[i]].key);
168 void naiGCMarkHash(naRef hash)
171 HashRec* hr = REC(hash);
172 for(i=0; hr && i < NCELLS(hr); i++)
173 if(TAB(hr)[i] >= 0) {
174 naiGCMark(ENTS(hr)[TAB(hr)[i]].key);
175 naiGCMark(ENTS(hr)[TAB(hr)[i]].val);
179 static void tmpStr(naRef* out, struct naStr* str, const char* key)
184 str->data.ref.ptr = (unsigned char*)key;
185 str->data.ref.len = strlen(key);
189 int naMember_cget(naContext c, naRef obj, const char* field, naRef* out)
191 naRef key; struct naStr str;
192 tmpStr(&key, &str, field);
193 return naMember_get(c, obj, key, out);
196 naRef naHash_cget(naRef hash, char* key)
200 tmpStr(&key2, &str, key);
201 return naHash_get(hash, key2, &result) ? result : naNil();
204 void naHash_cset(naRef hash, char* key, naRef val)
206 naRef key2; struct naStr str;
207 tmpStr(&key2, &str, key);
208 naiHash_tryset(hash, key2, val);
211 int naiHash_tryset(naRef hash, naRef key, naRef val)
213 HashRec* hr = REC(hash);
215 int ent, cell = findcell(hr, key, refhash(key));
216 if((ent = TAB(hr)[cell]) >= 0) { ENTS(hr)[ent].val = val; return 1; }
221 void naiGCHashClean(struct naHash* h)
227 /* Optimized naHash_get for looking up local variables (OP_LOCAL is by
228 * far the most common opcode and deserves some special case
229 * optimization). Assumes that the key is an interned symbol
230 * (i.e. the hash code is precomputed, and we only need to test for
231 * pointer identity). */
232 int naiHash_sym(struct naHash* hash, struct naStr* sym, naRef* out)
234 HashRec* hr = hash->rec;
237 HashEnt* ents = ENTS(hr);
238 unsigned int hc = sym->hashcode;
239 int cell, mask = POW2(hr->lgsz+1) - 1, step = (2*hc+1) & mask;
240 for(cell=HBITS(hr,hc); tab[cell] != ENT_EMPTY; cell=(cell+step)&mask)
241 if(tab[cell]!=ENT_DELETED && sym==PTR(ents[tab[cell]].key).str) {
242 *out = ents[tab[cell]].val;
250 /* As above, a special naHash_set for setting local variables.
251 * Assumes that the key is interned, and also that it isn't already
252 * present in the hash. */
253 void naiHash_newsym(struct naHash* hash, naRef* sym, naRef* val)
255 HashRec* hr = hash->rec;
256 int mask, step, cell, ent;
257 struct naStr *s = PTR(*sym).str;
258 if(!hr || hr->next >= POW2(hr->lgsz))
260 mask = POW2(hr->lgsz+1) - 1;
261 step = (2*s->hashcode+1) & mask;
262 cell = HBITS(hr, s->hashcode);
263 while(TAB(hr)[cell] != ENT_EMPTY)
264 cell = (cell + step) & mask;
266 if(ent >= NCELLS(hr)) return; /* race protection, don't overrun */
269 ENTS(hr)[TAB(hr)[cell]].key = *sym;
270 ENTS(hr)[TAB(hr)[cell]].val = *val;