Arrays are sufficient for ArrayLists? Really??

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 16 12:41:51 PDT 2011


On 5/16/11 2:38 PM, Andrei Alexandrescu wrote:
> On 5/16/11 2:31 PM, Mehrdad wrote:
>> On 5/16/2011 12:25 PM, Andrei Alexandrescu wrote:
>>> On 5/16/11 2:25 PM, Timon Gehr wrote:
>>>> Mehrdad wrote:
>>>>> Timon:
>>>>>> What about:
>>>>> void removeAt(T)(ref T[] arr, size_t index)
>>>>> {
>>>>> foreach (i, ref item; arr[1 .. index+1])
>>>>> item = arr[i - 1];
>>>>> arr = arr[1 .. $]; //note how no valid data is beyond the end of the
>>>>> array
>>>>> }
>>>>>
>>>>> Clever, but if you do this with a big enough number of items, you'll
>>>>> exhaust all memory. Be careful. :P
>>>>
>>>> Total memory usage is at most 2 times the maximum size the array
>>>> reaches during
>>>> its lifetime. Test it.
>>>
>>> Testing would also reveal that this is a buggy replica of
>>> std.algorithm.remove. At best we should suggest reusing library code
>>> over attempting to reinvent it.
>>>
>>>
>>> Thanks,
>>>
>>> Andrei
>>>
>>
>> Oh, I see.
>> Wait, what bug are you referring to, though?
>
> I was mistaken and removed my post. The code ingeniously redefines the
> problem - instead of removing from the array by shifting its tail
> downwards, it shifts elements upwards from the head of the array. A nice
> hack, but I don't think it does a lot in helping your situation.

In fact I even need to take that back. In order to work correctly, the 
function would have to iterate downwards. It _is_ indeed buggy, and I 
should stop emitting opinions when I'm short on time...

Andrei



More information about the Digitalmars-d mailing list