Associative array and ranges

Simen kjaeraas simen.kjaras at gmail.com
Wed Feb 2 06:21:06 PST 2011


Nrgyzer <nrgyzer at gmail.com> wrote:

> Hey guys,
>
> I have an associative array like this: T[hash_t] myArray; (T means
> the template type).
>
> Is there any chance to cast/convert this array to an indexed array or
> is it possible to iterate over specific indices? I know that there is
> something like next() for the foreach-statement but when the array
> contains some thousand instances and I only want iterate over (for
> example) 5 elements I think that's the wrong way. It's for a game and
> I think every next()-call influences the fps.
>
> I hope there is any solution for my problem :) - Thanks!

You should probably try aa.byValue:

auto o = ["a": 1, "b": 2];

foreach ( e; o.byValue ) {
     writeln( e );
}

It seems from the (somewhat unreadable) source, that the values are
stored in an array of pointers to structs holding these values.

If this is not fast enough for your needs, I'm afraid you will have
to write an AA implementation yourself. (You can replace the code in
druntime/src/rt/aaA.d with your own if you still want the nice
syntax sugar)

-- 
Simen


More information about the Digitalmars-d-learn mailing list