<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 13, 2014 at 5:50 PM, Steven Schveighoffer <span dir="ltr"><<a href="mailto:schveiguy@yahoo.com" target="_blank">schveiguy@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On Thu, 13 Feb 2014 19:06:45 -0500, timotheecour <<a href="mailto:timothee.cour2@gmail.com" target="_blank">timothee.cour2@gmail.com</a>> wrote:<br>

<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
On Thursday, 13 February 2014 at 23:56:35 UTC, Timothee Cour<br>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
how to iterate over an AA by key-value pair (tuple)?<br>
use case:<br>
avoid interrupting UFCS chains, eg:<br>
foo.generate_aa.byKeyValue.<u></u>filter!(a=>a[0].isLower).map!(<u></u>a=>a[1]) ...<br>
of course I could roll my own function but I was wondering if there's<br>
already a way.<br>
</blockquote>
<br>
<br>
is there anything more efficient than this?<br>
<br>
auto byKeyValue(T)(T a)if(isAssociativeArray!T){<br>
    return a.byKey.map!(b=>tuple(b, a[b]));<br>
}<br>
</blockquote>
<br></div></div>
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.<br>

<br>
-Steve<br></blockquote><div><br></div><div>That's what I was suspecting,<br></div><div>I guess the code to modify would be here:</div><div><br></div><div>druntime/import/object.di:455:5<br></div><div><br></div><div><div>
auto byKeyValue(){...}</div><div><br></div><div>auto byKey()</div><div>    {</div><div>        static struct Result</div><div>        {</div><div>            AARange r;</div><div><br></div><div>            @property bool empty() { return _aaRangeEmpty(r); }</div>
<div>            @property ref Key front() { return *cast(Key*)_aaRangeFrontKey(r); }</div><div>            void popFront() { _aaRangePopFront(r); }</div><div>            Result save() { return this; }</div><div>        }</div>
<div><br></div><div>        return Result(_aaRange(p));</div><div>    }</div></div><div><br></div><div>That seems like a worthy enhancement.</div><div>If I/someone does it, will it be merged in?</div><div><br></div></div>
</div></div>