Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 474

Show
Ignore:
Timestamp:
01/04/11 06:58:40 (14 years ago)
Author:
braddr
Message:

Fix aa.keys and aa.values to work on 64 bit.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/rt/aaA.d

    r456 r474  
    4545      100_663_319UL,    402_653_189UL, 
    4646    1_610_612_741UL,  4_294_967_291UL, 
    4747//  8_589_934_513UL, 17_179_869_143UL 
    4848]; 
    4949 
    5050/* This is the type of the return value for dynamic arrays. 
    5151 * It should be a type that is returned in registers. 
    5252 * Although DMD will return types of Array in registers, 
    5353 * gcc will not, so we instead use a 'long'. 
    5454 */ 
    55 alias long ArrayRet_t; 
     55alias void[] ArrayRet_t; 
    5656 
    5757struct Array 
    5858{ 
    5959    size_t length; 
    6060    void* ptr; 
    6161} 
    6262 
    6363struct aaA 
    6464{ 
    6565    aaA *next; 
     
    502502} 
    503503 
    504504/******************************************** 
    505505 * Produce array of N byte keys from aa. 
    506506 */ 
    507507 
    508508ArrayRet_t _aaKeys(AA aa, size_t keysize) 
    509509{ 
    510510    auto len = _aaLen(aa); 
    511511    if (!len) 
    512         return 0
     512        return null
    513513    auto res = (cast(byte*) gc_malloc(len * keysize, 
    514514                                 !(aa.a.keyti.flags() & 1) ? BlkAttr.NO_SCAN : 0))[0 .. len * keysize]; 
    515515    size_t resi = 0; 
    516516    foreach (e; aa.a.b) 
    517517    { 
    518518        while (e) 
    519519        { 
    520520            memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize); 
    521521            resi++; 
    522522            e = e.next; 
    523523        } 
    524524    } 
    525525    assert(resi == len); 
    526526 
    527527    Array a; 
    528528    a.length = len; 
    529529    a.ptr = res.ptr; 
    530530    return *cast(ArrayRet_t*)(&a); 
     531} 
     532 
     533unittest 
     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); 
    531555} 
    532556 
    533557 
    534558/********************************************** 
    535559 * 'apply' for associative arrays - to support foreach 
    536560 */ 
    537561 
    538562// dg is D, but _aaApply() is C 
    539563extern (D) typedef int delegate(void *) dg_t; 
    540564