comparison runtime/internal/aaA.d @ 1418:f5f8c21ce6ef

Make "`aa[key]`" use the same runtime call as "`key in aa`". The runtime calls these were using were different, but with equivalent definitions. With `ldc -O3`, the following functions now all compile to the exact same code: {{{ int[int] y; void foo(int x) { if (x in y) { auto z = x in y; sink(*z); } } void bar(int x) { if (x in y) { sink(y[x]); } } void baz(int x) { if (auto p = x in y) { sink(*p); } } }}}
author Frits van Bommel <fvbommel wxs.nl>
date Mon, 25 May 2009 12:50:40 +0200
parents a26b0c5d5942
children 09734fb929c0
comparison
equal deleted inserted replaced
1417:c3c23d2c5407 1418:f5f8c21ce6ef
290 290
291 291
292 /************************************************* 292 /*************************************************
293 * Get pointer to value in associative array indexed by key. 293 * Get pointer to value in associative array indexed by key.
294 * Returns null if it is not already there. 294 * Returns null if it is not already there.
295 */ 295 * Used for both "aa[key]" and "key in aa"
296
297 void* _aaGetRvalue(AA aa, TypeInfo keyti, size_t valuesize, void *pkey)
298 {
299 //printf("_aaGetRvalue(valuesize = %u)\n", valuesize);
300 if (!aa)
301 return null;
302
303 //auto pkey = cast(void *)(&valuesize + 1);
304 auto keysize = aligntsize(keyti.tsize());
305 auto len = aa.b.length;
306
307 if (len)
308 {
309 auto key_hash = keyti.getHash(pkey);
310 //printf("hash = %d\n", key_hash);
311 size_t i = key_hash % len;
312 auto e = aa.b[i];
313 while (e !is null)
314 {
315 if (key_hash == e.hash)
316 {
317 auto c = keyti.compare(pkey, e + 1);
318 if (c == 0)
319 return cast(void *)(e + 1) + keysize;
320 e = (c < 0) ? e.left : e.right;
321 }
322 else
323 e = (key_hash < e.hash) ? e.left : e.right;
324 }
325 }
326 return null; // not found, caller will throw exception
327 }
328
329
330 /*************************************************
331 * Determine if key is in aa.
332 * Returns: 296 * Returns:
333 * null not in aa 297 * null not in aa
334 * !=null in aa, return pointer to value 298 * !=null in aa, return pointer to value
335 */ 299 */
336 300