Associative array and ranges

spir denis.spir at gmail.com
Thu Feb 3 09:36:33 PST 2011


On 02/03/2011 04:41 PM, Nrgyzer wrote:
> == 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.

Ruby hashes are like that, but I don't think it would be easy to do in D. You'd 
better maintain the insertion order yourself in a // array of keys, or make 
elements link list nodes.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list