Arrays are sufficient for ArrayLists? Really??

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 16 12:59:46 PDT 2011


On 5/16/11 2:56 PM, Timon Gehr wrote:
> Timon Gehr wrote:
>> void removeAt(T)(ref T[] arr, size_t index)
>> {
>>     foreach (i, ref item; retro(arr[1 .. index+1]))
>>          item = arr[i - 1];
>>      arr = arr[1 .. $];
>> }
>
> Sorry, still wrong:
>
> void removeAt(T)(ref T[] arr, size_t index)
> {
>     foreach (i, ref item; retro(arr[1 .. index+1]))
>           item = arr[index - i - 1];
>     arr = arr[1 .. $];
> }

I think it would take less time to actually paste the function in a file 
and try it. A cute possibility:

void removeAt(T)(ref T[] arr, size_t index)
{
     copy(retro(arr[0 .. index]), retro(arr[1 .. index + 1]));
     arr = arr[1 .. $];
}


Andrei


More information about the Digitalmars-d mailing list