Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #933 from IgorStepanov/newaa-aaLiteral-and-aaInit
Browse files Browse the repository at this point in the history
[new AA] implement aaLiteral
  • Loading branch information
MartinNowak committed Oct 4, 2014
2 parents e3c8df5 + a213c6b commit 7e2a075
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/internal/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,24 @@ template externDFunc(string fqn, T:FT*, FT) if(is(FT == function))
else
static assert(0);
}

template staticIota(int beg, int end)
{
static if (beg + 1 >= end)
{
static if (beg >= end)
{
alias staticIota = TypeTuple!();
}
else
{
alias staticIota = TypeTuple!(+beg);
}
}
else
{
enum mid = beg + (end - beg) / 2;
alias staticIota = TypeTuple!(staticIota!(beg, mid), staticIota!(mid, end));
}
}

40 changes: 40 additions & 0 deletions src/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,46 @@ extern (C)
void* _aaRangeFrontKey(AARange r) pure nothrow @nogc;
void* _aaRangeFrontValue(AARange r) pure nothrow @nogc;
void _aaRangePopFront(ref AARange r) pure nothrow @nogc;

/*
_d_assocarrayliteralTX marked as pure, because aaLiteral can be called from pure code.
This is a typesystem hole, however this is existing hole.
Early compiler didn't check purity of toHash or postblit functions, if key is a UDT thus
copiler allowed to create AA literal with keys, which have impure unsafe toHash methods.
*/
void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) pure;
}

auto aaLiteral(Key, Value, T...)(auto ref T args) if (T.length % 2 == 0)
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
import core.internal.traits;
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

foreach (i; staticIota!(0, args.length / 2))
{
keys ~= args[2*i];
values ~= args[2*i + 1];
}

void[] key_slice;
void[] value_slice;
void *ret;
() @trusted {
key_slice = *cast(void[]*)&keys;
value_slice = *cast(void[]*)&values;
ret = _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}();
return ret;
}
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down
40 changes: 40 additions & 0 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,46 @@ extern (C)

int _aaEqual(in TypeInfo tiRaw, in void* e1, in void* e2);
hash_t _aaGetHash(in void* aa, in TypeInfo tiRaw) nothrow;

/*
_d_assocarrayliteralTX marked as pure, because aaLiteral can be called from pure code.
This is a typesystem hole, however this is existing hole.
Early compiler didn't check purity of toHash or postblit functions, if key is a UDT thus
copiler allowed to create AA literal with keys, which have impure unsafe toHash methods.
*/
void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) pure;
}

auto aaLiteral(Key, Value, T...)(auto ref T args) if (T.length % 2 == 0)
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
import core.internal.traits;
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

foreach (i; staticIota!(0, args.length / 2))
{
keys ~= args[2*i];
values ~= args[2*i + 1];
}

void[] key_slice;
void[] value_slice;
void *ret;
() @trusted {
key_slice = *cast(void[]*)&keys;
value_slice = *cast(void[]*)&values;
ret = _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}();
return ret;
}
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down

0 comments on commit 7e2a075

Please sign in to comment.