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

Commit

Permalink
implement aaLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Stepanov committed Aug 26, 2014
1 parent 1685c90 commit 2c03e7a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/object.di
Expand Up @@ -383,6 +383,51 @@ 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...)(T args) @trusted
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

template _Tuple(T...)
{
alias _Tuple = T;
}

template Step2Tuple(size_t len, size_t idx = 0)
{
static if (idx >= len)
{
alias Step2Tuple = _Tuple!();
}
else
{
alias Step2Tuple = _Tuple!(idx, Step2Tuple!(len, idx + 2));
}
}

foreach (i; Step2Tuple!(T.length))
{
keys ~= cast(Key)args[i];
values ~= cast(Value)args[i+1];
}

void[] key_slice = *cast(void[]*)&keys;
void[] value_slice = *cast(void[]*)&values;

return _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down
45 changes: 45 additions & 0 deletions src/object_.d
Expand Up @@ -1979,6 +1979,51 @@ 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...)(T args) @trusted
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

template _Tuple(T...)
{
alias _Tuple = T;
}

template Step2Tuple(size_t len, size_t idx = 0)
{
static if (idx >= len)
{
alias Step2Tuple = _Tuple!();
}
else
{
alias Step2Tuple = _Tuple!(idx, Step2Tuple!(len, idx + 2));
}
}

foreach (i; Step2Tuple!(T.length))
{
keys ~= cast(Key)args[i];
values ~= cast(Value)args[i+1];
}

void[] key_slice = *cast(void[]*)&keys;
void[] value_slice = *cast(void[]*)&values;

return _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}
}

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

0 comments on commit 2c03e7a

Please sign in to comment.