Removing an object from a range

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Dec 12 20:50:56 PST 2010


On 12/13/10, Matthias Walter <xammy at xammy.homelinux.net> wrote:
> On 12/12/2010 08:43 PM, Andrej M. wrote:
>> I can't seem to find an easy remove method in std.algorithm that takes an
>> object and a range (an array in this case) and removes any matches from
>> the range. I'm using this snippet for now:
>>
>> private DrawingElement[] elements;
>>
>> public override void Remove(DrawingElement d)
>> {
>>     foreach (i, child; elements)
>>     {
>>         if (child == d)
>>         {
>>             elements = remove(elements, i);
>>             break;
>>         }
>>     }
>> }
> Just want to mention that this code does not remove "any matches",  but
> only the first one! So if you want all removed, you have to improve it a
> bit.
>
> Matthias
>

Yeah that's what the break is for. But that shouldn't be a test for
equality either. It should rather be:

>>     foreach (i, child; elements)
>>     {
>>         if (child is d)
>>         {
>>             elements = remove(elements, i);
>>             break;
>>         }
>>     }

Because I would really need to be looking for a specific object, I
think. Anyway it's just some code from some design patterns,
demonstration code..


More information about the Digitalmars-d-learn mailing list