Thread overview
Removing an array element in order?
Jul 02, 2006
Sean Kelly
Jul 03, 2006
Oskar Linde
Jul 03, 2006
Sean Kelly
July 02, 2006
Assuming I want to remove an element in an array and preserve order, does the D spec consider it legal to do this:

    char[] str = "abc def";
    int    pos = 3;
    str[pos .. $-1] = str[pos+1 .. $];
    str.length = str.length - 1;
    assert( memcmp( &str[0], "abcdef", str.length ) );

Or are slice copy semantics equivalent to memcpy where overlapping regions are not allowed?


Sean
July 03, 2006
Sean Kelly wrote:
> Assuming I want to remove an element in an array and preserve order, does the D spec consider it legal to do this:
> 
>     char[] str = "abc def";
>     int    pos = 3;
>     str[pos .. $-1] = str[pos+1 .. $];
>     str.length = str.length - 1;
>     assert( memcmp( &str[0], "abcdef", str.length ) );
> 
> Or are slice copy semantics equivalent to memcpy where overlapping regions are not allowed?

As far as I understand, slice copying is equivalent to memcopy and overlapping copies are illegal. According to the spec:

(http://www.digitalmars.com/d/arrays.html)
Under Array Copying:

""
Overlapping copies are an error:

s[0..2] = s[1..3];	// error, overlapping copy
s[1..3] = s[0..2];	// error, overlapping copy

Disallowing overlapping makes it possible for more aggressive parallel code optimizations than possible with the serial semantics of C.
""

/Oskar
July 03, 2006
Oskar Linde wrote:
> Sean Kelly wrote:
>> Assuming I want to remove an element in an array and preserve order, does the D spec consider it legal to do this:
>>
>>     char[] str = "abc def";
>>     int    pos = 3;
>>     str[pos .. $-1] = str[pos+1 .. $];
>>     str.length = str.length - 1;
>>     assert( memcmp( &str[0], "abcdef", str.length ) );
>>
>> Or are slice copy semantics equivalent to memcpy where overlapping regions are not allowed?
> 
> As far as I understand, slice copying is equivalent to memcopy and overlapping copies are illegal. According to the spec:
> 
> (http://www.digitalmars.com/d/arrays.html)
> Under Array Copying:
> 
> ""
> Overlapping copies are an error:
> 
> s[0..2] = s[1..3];    // error, overlapping copy
> s[1..3] = s[0..2];    // error, overlapping copy
> 
> Disallowing overlapping makes it possible for more aggressive parallel code optimizations than possible with the serial semantics of C.

I should have just checked the spec :-)  Thanks!

So I suppose memmove is probably optimal in this case?


Sean