Associative array and ranges

Nrgyzer nrgyzer at gmail.com
Thu Feb 3 07:41:16 PST 2011


== Auszug aus Steven Schveighoffer (schveiguy at yahoo.com)'s Artikel
> On Thu, 03 Feb 2011 09:35:44 -0500, Nrgyzer <nrgyzer at gmail.com>
wrote:
> > == Auszug aus bearophile (bearophileHUGS at 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.


More information about the Digitalmars-d-learn mailing list