Arrays are sufficient for ArrayLists? Really??

Steven Schveighoffer schveiguy at yahoo.com
Mon May 16 13:27:26 PDT 2011


On Mon, 16 May 2011 16:17:45 -0400, Mehrdad <wfunction at hotmail.com> wrote:

> On 5/16/2011 1:15 PM, Vladimir Panteleev wrote:
>> On Mon, 16 May 2011 23:11:50 +0300, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>
>>> I tested the function above.
>>
>> OP's intention was to create a version of removeAt which modifies the
>> array in-place only when there are no other aliases towards the data.
>> The initial approach was to attempt to check if the passed array is a
>> slice of a larger array, but as it was discussed, it's flawed.
>>
>
> +1

Wait, I don't think the approach is flawed, just not correctly implemented  
(see my suggestion with capacity).

If you have an array:

[1, 2, 3, 4]

and you want to remove the 2nd element, another alias to the original  
array would be:

[1, 3, 4, 4]

Are you trying to argue that the last 4 in that array is still valid  
data?  If so, then yes, it's impossible to tell if there are other aliases  
to the same data.  All you can tell is if the data ends at the valid data  
for that block (via the capacity property).

I can't see any use case for continuing to use the original alias, because  
the data is obviously corrupt.  Any alias that still references that last  
element would be corrupted.

If you passed in the slice [0..3], you get an original aliased array of:

[1, 3, 3, 4]

Now, anything that references that third element is corrupt.  But that  
fourth element is not corrupted, it wasn't part of the arguments to  
removeAt.  So you can continue to refer to that element, and a properly  
implemented removeAt should avoid stomping on that element with a later  
append.

-Steve


More information about the Digitalmars-d mailing list