On Thu, 03 Feb 2011 10:41:16 -0500, Nrgyzer <nrgy...@gmail.com> wrote:

== Auszug aus Steven Schveighoffer (schvei...@yahoo.com)'s Artikel
On Thu, 03 Feb 2011 09:35:44 -0500, Nrgyzer <nrgy...@gmail.com>
wrote:
> == Auszug aus bearophile (bearophileh...@lycos.com)'s Artikel
>> Nrgyzer:
>> > 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.
>> Show a hypothetical code example of what you desire to do,
please.
>> Bye,
>> bearophile
>
> Example:
>
> ...
> class Example(T : Drawable) : Drawable {
>
>    T[hash_t] pObjectsToDraw;
>
>    uint pFrom, pTo;
>
>    void setLimit(from, to) {
>            pFrom = from;
>            pTo = to;
>    }
>
>    void remove(T objToRemove) {
>            pObjectsToDraw.remove(objToRemove.toHash());
>    }
>
>    override void draw() {
>            for (uint i = pFrom; i < pTo; i++) {
>                    pOjectsToDraw[i].draw(); // cannot call
> because pObjectsToDraw is an associative and no static or dynamic
> array
>            }
>    }
>
> }
First, hashes are not stored in any particular order, so I'm not
sure what
you expect to accomplish except "give me (pTo - pFrom) random
elements
 from the array"
Second, you can use a foreach loop to get data out of an AA, and
then
break when you've retrieved enough elements.
Again, I'm not sure what the point is of starting in the middle of
the
array.  Are you expecting something different from a hashtable?
-Steve

I know that hashes aren't stored in any order... but lets take the
LinkedHashSet in Java. The LinkedHashSet/Map stores the hashes in
order of inserting. With the toArray()-method I can loop over
specific elements/indices... but I can also remove the elements by
their hash. I'm looking for a similar technique in D - you can find
an Java example on http://www.java2s.com/Code/JavaAPI/java.util/
LinkedHashSettoArray.htm.

The AA does not do that. Note that there is no builtin 'set' type in D (there is one in dcollections).

Note that your worry about performance when iterating the AA is completely negated by the toArray call -- this allocates space for your temporary array, and necessarily iterates all elements, even if you only want 5.

You are better off not to do things this way.

Incidentally, I do plan on adding a Link*Set/Map to dcollections, because I really like the array type in php (same thing). But this will not provide constant access to the nth element, so it still will fail your requirements.

The only thing I can think of is to store the elements in an array alongside the hash map (do you need to look up elements by hash_t? If not, why not just use an array directly?) to store the order of insertion. This only works if you rarely remove elements (removal in an array is an O(n) operation).

-Steve

Reply via email to