Arrays, overlapping, etc

Regan Heath regan at netmail.co.nz
Mon Jul 9 01:31:25 PDT 2007


I did a lot of coding over the weekend.  (I wrote a lexer for C and the 
preprocessor - don't ask ;)) and had a number of issues which I wanted 
to bring up.

The first of which is the "overlapping array copy" exception.  It has no 
file:line numbers and this can make it a real pain to track down where 
it has occurred.

The next point is that surely an overlapping copy can be 
implemented/handled?  I had a quick look in Bugzilla and found a couple 
of of items the topic and related topics.  One even suggests documenting 
the various methods you can use to get around the deficiency.

A related post talks about adding a remove() method for normal arrays to 
efficiently remove an item from the array.  Adding overlapping array 
copies will make this a relatively trivial operation, eg.

To remove item at index 'n' from 'array':
   array[n..$-1] = array[n+1..$];

So, some questions about overlapping copies;

1. Why is it illegal (mentioned in the docs)?

2. Is it because Walter wants to keep the language simple to implement?

(surely not)

3. Is it because W hasn't gotten round to implementing an overlapping copy?

(perhaps)

4. Is there some reason I am not seeing?

(more than likely)

It seems an overlapping copy could be handled by the internal array copy 
function using a combination of memmove and memcpy.

Take a memory block X:
X:  [abcde0123456789]

With overlapping slices S and D:
S:       [0123456789]
D:  [abcde01234]

Perform a move on the overlapping part:
m:     <-[01234]
=:  [01234xxxxx]

Then a copy on the rest:
c:        |---[56789]
=:  [0123456789]

Resulting memory block:
X:  [012345678956789]

memmove and memcpy don't care if the blocks in question below to one 
array, or 2, 3, or more.  Nor does it matter (so far as I can see) 
provided the resulting block in memory looks like S copied to the 
location of D.

Thoughts?

Regan



More information about the Digitalmars-d mailing list