std.container & ranges

Christophe travert at phare.normalesup.org
Wed Nov 2 01:54:14 PDT 2011


Kagamin , dans le message (digitalmars.D.learn:30362), a écrit :
> Steven Schveighoffer Wrote:
> 
>> ahem, using dcollections:
>> 
>> foreach(ref doRemove, cell; &organism.purge)
>>      doRemove = cell.x == x && cell.y == y;
>> 
>> complexity: O(n)
> 
> may be a generic iteration handler would be more useful?
> 
> foreach(ref handler, cell; &organism.each)
>    if(cell.x == x && cell.y == y) handler.removeCurrent();
> 
> it could provide a whole api, say, you may want to have lookahead
> 
> foreach(ref handler, cell; &organism.each)
>    if(cell.x == x && cell.y == y && handler.next.x==0)
>       handler.removeCurrent();


That's not easier than using Tobias' improved foreach:

foreach(cell, cellRange; organism[])
  if (cell.x == x && cell.y == y)
    organism.remove(cellRange);


If you want to use algorithm specialized and optimized for the 
container, I would prefer to use purge than each + handler. purge seems 
better to me, because it can be specialized for the container and for 
the action, and do not require to learn a new handler structure. "each" 
do not seem to add a lot  to foreach(cell, cellRange; range), since it 
must be able to cope with any operation provided by handler, and any 
operation combinaisons...



More information about the Digitalmars-d-learn mailing list