4 * Part of the D programming language runtime library.
5 * Implementation of associative arrays.
9 * Copyright (C) 2000-2008 by Digital Mars, http://www.digitalmars.com
10 * Written by Walter Bright
12 * This software is provided 'as-is', without any express or implied
13 * warranty. In no event will the authors be held liable for any damages
14 * arising from the use of this software.
16 * Permission is granted to anyone to use this software for any purpose,
17 * including commercial applications, and to alter it and redistribute it
18 * freely, subject to the following restrictions:
20 * o The origin of this software must not be misrepresented; you must not
21 * claim that you wrote the original software. If you use this software
22 * in a product, an acknowledgment in the product documentation would be
23 * appreciated but is not required.
24 * o Altered source versions must be plainly marked as such, and must not
25 * be misrepresented as being the original software.
26 * o This notice may not be removed or altered from any source
31 * Modified by Sean Kelly for use with the D Runtime Project
43 FINALIZE = 0b0000_0001,
44 NO_SCAN = 0b0000_0010,
45 NO_MOVE = 0b0000_0100,
46 ALL_BITS = 0b1111_1111
49 extern (C) void* gc_malloc( size_t sz, uint ba = 0 );
50 extern (C) void* gc_calloc( size_t sz, uint ba = 0 );
51 extern (C) void gc_free( void* p );
54 // Auto-rehash and pre-allocate - Dave Fladebo
56 static size_t[] prime_list = [
60 393_241UL, 1_572_869UL,
61 6_291_469UL, 25_165_843UL,
62 100_663_319UL, 402_653_189UL,
63 1_610_612_741UL, 4_294_967_291UL,
64 // 8_589_934_513UL, 17_179_869_143UL
67 /* This is the type of the return value for dynamic arrays.
68 * It should be a type that is returned in registers.
69 * Although DMD will return types of Array in registers,
70 * gcc will not, so we instead use a 'long'.
72 alias long ArrayRet_t;
92 size_t nodes; // total number of aaA nodes
93 TypeInfo keyti; // TODO: replace this with TypeInfo_AssociativeArray when available in _aaGet()
96 /* This is the type actually seen by the programmer, although
97 * it is completely opaque.
105 /**********************************
106 * Align to next pointer boundary, so that
107 * GC won't be faced with misaligned pointers
111 size_t aligntsize(size_t tsize)
113 // Is pointer alignment on the x64 4 bytes or 8?
114 return (tsize + size_t.sizeof - 1) & ~(size_t.sizeof - 1);
119 /*************************************************
124 void _aaInvAh(aaA*[] aa)
126 for (size_t i = 0; i < aa.length; i++)
133 private int _aaCmpAh_x(aaA *e1, aaA *e2)
136 c = e1.hash - e2.hash;
139 c = e1.key.length - e2.key.length;
141 c = memcmp((char *)e1.key, (char *)e2.key, e1.key.length);
146 private void _aaInvAh_x(aaA *e)
152 key_hash = getHash(e.key);
153 assert(key_hash == e.hash);
161 _aaInvAh_x(e1); // ordinary recursion
164 c = _aaCmpAh_x(e1, e);
167 } while (e1 != null);
175 c = _aaCmpAh_x(e, e2);
178 } while (e2 != null);
179 e = e.right; // tail recursion
187 /****************************************************
188 * Determine number of entries in associative array.
194 //printf("_aaLen()+\n");
201 void _aaLen_x(aaA* ex)
225 assert(len == result);
227 //printf("_aaLen()-\n");
231 return aa.a ? aa.a.nodes : 0;
235 /*************************************************
236 * Get pointer to value in associative array indexed by key.
237 * Add entry for key if it is not already there.
240 void* _aaGet(AA* aa, TypeInfo keyti, size_t valuesize, ...)
249 assert(aa.a.b.length);
250 //assert(_aaInAh(*aa.a, key));
254 auto pkey = cast(void *)(&valuesize + 1);
257 auto keysize = aligntsize(keyti.tsize());
266 auto len = prime_list[0];
268 aa.a.b = new pa[len];
271 auto key_hash = keyti.getHash(pkey);
272 //printf("hash = %d\n", key_hash);
273 i = key_hash % aa.a.b.length;
274 auto pe = &aa.a.b[i];
275 while ((e = *pe) !is null)
277 if (key_hash == e.hash)
279 auto c = keyti.compare(pkey, e + 1);
282 pe = (c < 0) ? &e.left : &e.right;
285 pe = (key_hash < e.hash) ? &e.left : &e.right;
288 // Not found, create new elem
289 //printf("create new one\n");
290 size_t size = aaA.sizeof + keysize + valuesize;
291 e = cast(aaA *) gc_calloc(size);
292 memcpy(e + 1, pkey, keysize);
296 auto nodes = ++aa.a.nodes;
297 //printf("length = %d, nodes = %d\n", (*aa.a).length, nodes);
298 if (nodes > aa.a.b.length * 4)
304 return cast(void *)(e + 1) + keysize;
308 /*************************************************
309 * Get pointer to value in associative array indexed by key.
310 * Returns null if it is not already there.
313 void* _aaGetRvalue(AA aa, TypeInfo keyti, size_t valuesize, ...)
315 //printf("_aaGetRvalue(valuesize = %u)\n", valuesize);
319 auto pkey = cast(void *)(&valuesize + 1);
320 auto keysize = aligntsize(keyti.tsize());
321 auto len = aa.a.b.length;
325 auto key_hash = keyti.getHash(pkey);
326 //printf("hash = %d\n", key_hash);
327 size_t i = key_hash % len;
331 if (key_hash == e.hash)
333 auto c = keyti.compare(pkey, e + 1);
335 return cast(void *)(e + 1) + keysize;
336 e = (c < 0) ? e.left : e.right;
339 e = (key_hash < e.hash) ? e.left : e.right;
342 return null; // not found, caller will throw exception
346 /*************************************************
347 * Determine if key is in aa.
350 * !=null in aa, return pointer to value
353 void* _aaIn(AA aa, TypeInfo keyti, ...)
359 //assert(result == 0 || result == 1);
365 auto pkey = cast(void *)(&keyti + 1);
367 //printf("_aaIn(), .length = %d, .ptr = %x\n", aa.a.length, cast(uint)aa.a.ptr);
368 auto len = aa.a.b.length;
372 auto key_hash = keyti.getHash(pkey);
373 //printf("hash = %d\n", key_hash);
374 size_t i = key_hash % len;
378 if (key_hash == e.hash)
380 auto c = keyti.compare(pkey, e + 1);
382 return cast(void *)(e + 1) + aligntsize(keyti.tsize());
383 e = (c < 0) ? e.left : e.right;
386 e = (key_hash < e.hash) ? e.left : e.right;
395 /*************************************************
396 * Delete key entry in aa[].
397 * If key is not in aa[], do nothing.
400 void _aaDel(AA aa, TypeInfo keyti, ...)
402 auto pkey = cast(void *)(&keyti + 1);
405 if (aa.a && aa.a.b.length)
407 auto key_hash = keyti.getHash(pkey);
408 //printf("hash = %d\n", key_hash);
409 size_t i = key_hash % aa.a.b.length;
410 auto pe = &aa.a.b[i];
411 while ((e = *pe) !is null) // null means not found
413 if (key_hash == e.hash)
415 auto c = keyti.compare(pkey, e + 1);
418 if (!e.left && !e.right)
422 else if (e.left && !e.right)
427 else if (!e.left && e.right)
447 pe = (c < 0) ? &e.left : &e.right;
450 pe = (key_hash < e.hash) ? &e.left : &e.right;
456 /********************************************
457 * Produce array of values from aa.
460 ArrayRet_t _aaValues(AA aa, size_t keysize, size_t valuesize)
463 assert(keysize == aligntsize(keysize));
470 void _aaValues_x(aaA* e)
474 memcpy(a.ptr + resi * valuesize,
475 cast(byte*)e + aaA.sizeof + keysize,
486 } while (e !is null);
491 a.length = _aaLen(aa);
492 a.ptr = cast(byte*) gc_malloc(a.length * valuesize,
493 valuesize < (void*).sizeof ? BlkAttr.NO_SCAN : 0);
500 assert(resi == a.length);
502 return *cast(ArrayRet_t*)(&a);
506 /********************************************
510 void* _aaRehash(AA* paa, TypeInfo keyti)
523 void _aaRehash_x(aaA* olde)
527 auto left = olde.left;
528 auto right = olde.right;
534 //printf("rehash %p\n", olde);
535 auto key_hash = olde.hash;
536 size_t i = key_hash % newb.b.length;
537 auto pe = &newb.b[i];
538 while ((e = *pe) !is null)
540 //printf("\te = %p, e.left = %p, e.right = %p\n", e, e.left, e.right);
542 assert(e.right != e);
543 if (key_hash == e.hash)
545 auto c = keyti.compare(olde + 1, e + 1);
547 pe = (c < 0) ? &e.left : &e.right;
550 pe = (key_hash < e.hash) ? &e.left : &e.right;
568 //printf("Rehash\n");
572 auto len = _aaLen(*paa);
576 for (i = 0; i < prime_list.length - 1; i++)
578 if (len <= prime_list[i])
582 newb.b = new aaA*[len];
590 newb.nodes = aa.nodes;
591 newb.keyti = aa.keyti;
600 /********************************************
604 void _aaBalance(AA* paa)
606 //printf("_aaBalance()\n");
614 /* Temporarily store contents of bucket in array[]
617 void addToArray(aaA* e)
620 { addToArray(e.left);
621 if (k == array.length)
622 array.length = array.length * 2;
628 /* The contents of the bucket are now sorted into array[].
631 void buildTree(aaA** p, size_t x1, size_t x2)
636 { auto mid = (x1 + x2) >> 1;
638 buildTree(&(*p).left, x1, mid);
639 buildTree(&(*p).right, mid + 1, x2);
647 /********************************************
648 * Produce array of N byte keys from aa.
651 ArrayRet_t _aaKeys(AA aa, size_t keysize)
656 void _aaKeys_x(aaA* e)
660 memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize);
670 } while (e !is null);
673 auto len = _aaLen(aa);
676 res = (cast(byte*) gc_malloc(len * keysize,
677 !(aa.a.keyti.flags() & 1) ? BlkAttr.NO_SCAN : 0))[0 .. len * keysize];
689 return *cast(ArrayRet_t*)(&a);
693 /**********************************************
694 * 'apply' for associative arrays - to support foreach
697 // dg is D, but _aaApply() is C
698 extern (D) typedef int delegate(void *) dg_t;
700 int _aaApply(AA aa, size_t keysize, dg_t dg)
703 assert(aligntsize(keysize) == keysize);
708 //printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.a, keysize, dg);
710 int treewalker(aaA* e)
715 //printf("treewalker(e = %p, dg = x%llx)\n", e, dg);
716 result = dg(cast(void *)(e + 1) + keysize);
725 result = treewalker(e.right);
741 result = treewalker(e);
750 // dg is D, but _aaApply2() is C
751 extern (D) typedef int delegate(void *, void *) dg2_t;
753 int _aaApply2(AA aa, size_t keysize, dg2_t dg)
756 assert(aligntsize(keysize) == keysize);
761 //printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.a, keysize, dg);
763 int treewalker(aaA* e)
768 //printf("treewalker(e = %p, dg = x%llx)\n", e, dg);
769 result = dg(cast(void *)(e + 1), cast(void *)(e + 1) + keysize);
778 result = treewalker(e.right);
794 result = treewalker(e);
804 /***********************************
805 * Construct an associative array of type ti from
806 * length pairs of key/value pairs.
810 BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...)
812 auto valuesize = ti.next.tsize(); // value size
814 auto keysize = keyti.tsize(); // key size
817 //printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
818 //printf("tivalue = %.*s\n", ti.next.classinfo.name);
819 if (length == 0 || valuesize == 0 || keysize == 0)
826 va_start!(size_t)(q, length);
829 result.keyti = keyti;
832 for (i = 0; i < prime_list.length - 1; i++)
834 if (length <= prime_list[i])
837 auto len = prime_list[i];
838 result.b = new aaA*[len];
840 size_t keystacksize = (keysize + int.sizeof - 1) & ~(int.sizeof - 1);
841 size_t valuestacksize = (valuesize + int.sizeof - 1) & ~(int.sizeof - 1);
843 size_t keytsize = aligntsize(keysize);
845 for (size_t j = 0; j < length; j++)
852 auto key_hash = keyti.getHash(pkey);
853 //printf("hash = %d\n", key_hash);
855 auto pe = &result.b[i];
861 // Not found, create new elem
862 //printf("create new one\n");
863 e = cast(aaA *) cast(void*) new void[aaA.sizeof + keytsize + valuesize];
864 memcpy(e + 1, pkey, keysize);
870 if (key_hash == e.hash)
872 auto c = keyti.compare(pkey, e + 1);
875 pe = (c < 0) ? &e.left : &e.right;
878 pe = (key_hash < e.hash) ? &e.left : &e.right;
880 memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);