Changeset 474
- Timestamp:
- 01/04/11 06:58:40 (14 years ago)
- Files:
-
- trunk/src/rt/aaA.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/rt/aaA.d
r456 r474 45 45 100_663_319UL, 402_653_189UL, 46 46 1_610_612_741UL, 4_294_967_291UL, 47 47 // 8_589_934_513UL, 17_179_869_143UL 48 48 ]; 49 49 50 50 /* This is the type of the return value for dynamic arrays. 51 51 * It should be a type that is returned in registers. 52 52 * Although DMD will return types of Array in registers, 53 53 * gcc will not, so we instead use a 'long'. 54 54 */ 55 alias longArrayRet_t;55 alias void[] ArrayRet_t; 56 56 57 57 struct Array 58 58 { 59 59 size_t length; 60 60 void* ptr; 61 61 } 62 62 63 63 struct aaA 64 64 { 65 65 aaA *next; … … 502 502 } 503 503 504 504 /******************************************** 505 505 * Produce array of N byte keys from aa. 506 506 */ 507 507 508 508 ArrayRet_t _aaKeys(AA aa, size_t keysize) 509 509 { 510 510 auto len = _aaLen(aa); 511 511 if (!len) 512 return 0;512 return null; 513 513 auto res = (cast(byte*) gc_malloc(len * keysize, 514 514 !(aa.a.keyti.flags() & 1) ? BlkAttr.NO_SCAN : 0))[0 .. len * keysize]; 515 515 size_t resi = 0; 516 516 foreach (e; aa.a.b) 517 517 { 518 518 while (e) 519 519 { 520 520 memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize); 521 521 resi++; 522 522 e = e.next; 523 523 } 524 524 } 525 525 assert(resi == len); 526 526 527 527 Array a; 528 528 a.length = len; 529 529 a.ptr = res.ptr; 530 530 return *cast(ArrayRet_t*)(&a); 531 } 532 533 unittest 534 { 535 int[string] aa; 536 537 aa["hello"] = 3; 538 assert(aa["hello"] == 3); 539 aa["hello"]++; 540 assert(aa["hello"] == 4); 541 542 assert(aa.length == 1); 543 544 string[] keys = aa.keys; 545 assert(keys.length == 1); 546 assert(memcmp(keys[0].ptr, cast(char*)"hello", 5) == 0); 547 548 int[] values = aa.values; 549 assert(values.length == 1); 550 assert(values[0] == 4); 551 552 aa.rehash; 553 assert(aa.length == 1); 554 assert(aa["hello"] == 4); 531 555 } 532 556 533 557 534 558 /********************************************** 535 559 * 'apply' for associative arrays - to support foreach 536 560 */ 537 561 538 562 // dg is D, but _aaApply() is C 539 563 extern (D) typedef int delegate(void *) dg_t; 540 564
