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

Commit

Permalink
implement aaLiteral and aaNew
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Stepanov committed Oct 4, 2014
1 parent 05c9db3 commit d236d1c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/internal/traits.d
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));
}
}

38 changes: 38 additions & 0 deletions src/object.di
Expand Up @@ -383,6 +383,44 @@ extern (C)
void* _aaRangeFrontKey(AARange r) pure nothrow @nogc;
void* _aaRangeFrontValue(AARange r) pure nothrow @nogc;
void _aaRangePopFront(ref AARange r) pure nothrow @nogc;

void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) @trusted 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;

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

void* aaNew(Key, Value)()
{
return null;
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down
38 changes: 38 additions & 0 deletions src/object_.d
Expand Up @@ -1979,6 +1979,44 @@ extern (C)

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

void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) @trusted 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;

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

void* aaNew(Key, Value)()
{
return null;
}

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

0 comments on commit d236d1c

Please sign in to comment.