how to iterate over an AA by key-value pair (tuple)?
Timothee Cour
thelastmammoth at gmail.com
Thu Feb 13 23:29:01 PST 2014
On Thu, Feb 13, 2014 at 5:50 PM, Steven Schveighoffer
<schveiguy at yahoo.com>wrote:
> On Thu, 13 Feb 2014 19:06:45 -0500, timotheecour <timothee.cour2 at gmail.com>
> wrote:
>
> On Thursday, 13 February 2014 at 23:56:35 UTC, Timothee Cour
>> wrote:
>>
>>> how to iterate over an AA by key-value pair (tuple)?
>>> use case:
>>> avoid interrupting UFCS chains, eg:
>>> foo.generate_aa.byKeyValue.filter!(a=>a[0].isLower).map!(a=>a[1]) ...
>>> of course I could roll my own function but I was wondering if there's
>>> already a way.
>>>
>>
>>
>> is there anything more efficient than this?
>>
>> auto byKeyValue(T)(T a)if(isAssociativeArray!T){
>> return a.byKey.map!(b=>tuple(b, a[b]));
>> }
>>
>
> Of course. The a[b] lookup is significant in the complexity, it's
> amortized constant, and the constant is not always small. Consider that all
> keys and values are already stored in structs inside the AA. With low-level
> access, it would be trivial to make an efficient tuple generator that did
> not need to lookup values by keys.
>
> -Steve
>
That's what I was suspecting,
I guess the code to modify would be here:
druntime/import/object.di:455:5
auto byKeyValue(){...}
auto byKey()
{
static struct Result
{
AARange r;
@property bool empty() { return _aaRangeEmpty(r); }
@property ref Key front() { return
*cast(Key*)_aaRangeFrontKey(r); }
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
}
return Result(_aaRange(p));
}
That seems like a worthy enhancement.
If I/someone does it, will it be merged in?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140213/02369fde/attachment-0001.html>
More information about the Digitalmars-d-learn
mailing list