copy and array length vs capacity. (Doc suggestion?)

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 21 16:10:06 PST 2015


Hi Jon! :)

On 11/21/2015 03:34 PM, Jon D wrote:

 >      Preconditions:
 >      target shall have enough room to accomodate the entirety of source.
 >
 > Clarifying that "enough room" means 'length' rather than 'capacity'
 > might be beneficial.

May I suggest that you improve that page. ;) If you don't already have a 
clone o the repo, you can do it easily by clicking the "Improve this 
page" button on that page.

Regarding why copy() cannot use the capacity of the slice, it is because 
slices don't know about each other, so, copy could not let other slices 
know that the capacity has just been used by this particular slice.

However, copy() could first append an element, in which case the 
capacity would be owned by this slice. copy() then safely use the 
capacity, knowing very well that the act of appending that one element 
has dropped the capacities of all other slices to zero.

In pseudo code:

if
   there is enough capacity
   and if copying will spill into capacity
then
    append an element
    copy by spilling into capacity
    set .length appropriately

Others, please review, implement, prove that it is efficient, and post a 
pull request. :)

Ali



More information about the Digitalmars-d-learn mailing list