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

Changeset 488

Show
Ignore:
Timestamp:
01/08/11 19:59:15 (14 years ago)
Author:
andrei
Message:

.dup property for associative arrays

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/import/object.di

    r476 r488  
    398398    return &foo; 
    399399    } 
    400400 
    401401    int delegate(int delegate(ref Value) dg) byValue() 
    402402    { 
    403403    return &opApply; 
    404404    } 
    405405 
    406406    Value get(Key key, lazy Value defaultValue) 
    407407    { 
    408     auto p = key in *cast(Value[Key]*)(&p); 
    409     return p ? *p : defaultValue; 
    410     } 
     408        auto r = key in *cast(Value[Key]*)(&p); 
     409        return r ? *r : defaultValue; 
     410    } 
     411 
     412    @property Value[Key] dup() 
     413    { 
     414        Value[Key] result; 
     415        foreach (k, v; this) 
     416        { 
     417            result[k] = v; 
     418        } 
     419        return result; 
     420    } 
     421
     422 
     423unittest 
     424
     425    auto a = [ 1:"one", 2:"two", 3:"three" ]; 
     426    auto b = a.dup; 
     427    assert(b == [ 1:"one", 2:"two", 3:"three" ]); 
    411428} 
    412429 
    413430void clear(T)(T obj) if (is(T == class)) 
    414431{ 
    415432    if (!obj) return; 
    416433    auto ci = obj.classinfo; 
    417434    auto defaultCtor = 
    418435        cast(void function(Object)) ci.defaultConstructor; 
    419436    version(none) // enforce isn't available in druntime 
    420437        _enforce(defaultCtor || (ci.flags & 8) == 0); 
  • trunk/src/object_.d

    r476 r488  
    24442444    return &foo; 
    24452445    } 
    24462446 
    24472447    int delegate(int delegate(ref Value) dg) byValue() 
    24482448    { 
    24492449    return &opApply; 
    24502450    } 
    24512451 
    24522452    Value get(Key key, lazy Value defaultValue) 
    24532453    { 
    2454     auto p = key in *cast(Value[Key]*)(&p); 
    2455     return p ? *p : defaultValue; 
    2456     } 
     2454        auto p = key in *cast(Value[Key]*)(&p); 
     2455        return p ? *p : defaultValue; 
     2456    } 
     2457 
     2458    @property Value[Key] dup() 
     2459    { 
     2460        Value[Key] result; 
     2461        foreach (k, v; this) 
     2462        { 
     2463            result[k] = v; 
     2464        } 
     2465        return result; 
     2466    } 
     2467
     2468 
     2469unittest 
     2470
     2471    auto a = [ 1:"one", 2:"two", 3:"three" ]; 
     2472    auto b = a.dup; 
     2473    assert(b == [ 1:"one", 2:"two", 3:"three" ]); 
    24572474} 
    24582475 
    24592476void clear(T)(T obj) if (is(T == class)) 
    24602477{ 
    24612478    if (!obj) return; 
    24622479    auto ci = obj.classinfo; 
    24632480    auto defaultCtor = 
    24642481        cast(void function(Object)) ci.defaultConstructor; 
    24652482    version(none) // enforce isn't available in druntime 
    24662483        _enforce(defaultCtor || (ci.flags & 8) == 0); 
     
    27122729/*************************************** 
    27132730 * Helper function used to see if two containers of different 
    27142731 * types have the same contents in the same sequence. 
    27152732 */ 
    27162733 
    27172734bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2) 
    27182735{ 
    27192736    if (a1.length != a2.length) 
    27202737    return false; 
    27212738    foreach(i, a; a1) 
    2722     {   if (a != a2[i]) 
    2723         return false; 
     2739    { 
     2740        if (a != a2[i]) 
     2741            return false; 
    27242742    } 
    27252743    return true; 
    27262744} 
    27272745 
    27282746