Thread overview
[phobos] array.shift for deletion/insertion
Feb 25, 2011
spir
Feb 26, 2011
spir
Feb 26, 2011
Daniel Murphy
Feb 26, 2011
Jonathan M Davis
February 26, 2011
Hello,

There is no builtin func to delete or insert one or more elements from or into
a dynamic array. The reason is probably that providing a dedicated idiom may
let such an operation look cheap (same reason as for refusing 'in' for dyn arrays).
Still, there is a need for this functionality, to avoid
* either repetedly coding it when needed, possibly with bugs,
* or using concat, which copies the whole content instead of the necessary part.

For both operations, the actual process is a shift operation on the part of the
array ranging from a lower bound to the upper end, for an offset equal to the
deleted/inserted slice's length. We could, I guess, provide this shift process
builtin. This would allow writing deletion/insertion easily, while making it
obvious that the operation is costly.
See example below. What do you think?


Denis

void shift (T) (ref T[] array, size_t low, sizediff_t offset) {
     if (offset == 0) return;
     auto len = array.length;
     size_t i;

     // move up for insertion: proceed backwards
     if (offset > 0) {
         array.length = len + offset;
         for (i=len-1 ; i>=low ; i--)
             array[i+offset] = array[i];
     }
     // move down for deletion: note offset is negative!
     else {
         for (i=low ; i<array.length ; i++)
             array[i+offset] = array[i];
         array.length = len + offset;
     }
}

unittest {
     auto a = [0,1,2,3,4,5,6,7,8,9];
     writeln(a);
     // delete [4..6]
     a.shift(7, -3);
     writeln(a);	        // [0,1,2,3,7,8,9]
     // insert [4..6]
     a.shift(4, +3);
     a[4..7] = [4,5,6];
     writeln(a);         // [0,1,2,3,4,5,6,7,8,9]
}

-- 
_________________
vita es estrany
spir.wikidot.com

February 25, 2011
std.array.insert?

Andrei

On 2/25/11 5:08 PM, spir wrote:
> Hello,
>
> There is no builtin func to delete or insert one or more elements from
> or into a dynamic array. The reason is probably that providing a
> dedicated idiom may let such an operation look cheap (same reason as for
> refusing 'in' for dyn arrays).
> Still, there is a need for this functionality, to avoid
> * either repetedly coding it when needed, possibly with bugs,
> * or using concat, which copies the whole content instead of the
> necessary part.
>
> For both operations, the actual process is a shift operation on the part
> of the array ranging from a lower bound to the upper end, for an offset
> equal to the deleted/inserted slice's length. We could, I guess, provide
> this shift process builtin. This would allow writing deletion/insertion
> easily, while making it obvious that the operation is costly.
> See example below. What do you think?
>
>
> Denis
>
> void shift (T) (ref T[] array, size_t low, sizediff_t offset) {
> if (offset == 0) return;
> auto len = array.length;
> size_t i;
>
> // move up for insertion: proceed backwards
> if (offset > 0) {
> array.length = len + offset;
> for (i=len-1 ; i>=low ; i--)
> array[i+offset] = array[i];
> }
> // move down for deletion: note offset is negative!
> else {
> for (i=low ; i<array.length ; i++)
> array[i+offset] = array[i];
> array.length = len + offset;
> }
> }
>
> unittest {
> auto a = [0,1,2,3,4,5,6,7,8,9];
> writeln(a);
> // delete [4..6]
> a.shift(7, -3);
> writeln(a); // [0,1,2,3,7,8,9]
> // insert [4..6]
> a.shift(4, +3);
> a[4..7] = [4,5,6];
> writeln(a); // [0,1,2,3,4,5,6,7,8,9]
> }
>
February 26, 2011
On 02/26/2011 12:33 AM, Andrei Alexandrescu wrote:
> std.array.insert?

Oh! for some reason I didn't have it (temp doc bug?), just found it online.
Then, shouldn't we also have the opposite operation delete(i,j) or delete(i,l)?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com

February 26, 2011
std.algorithm.remove?
February 25, 2011
On Friday, February 25, 2011 16:27:55 Daniel Murphy wrote:
> std.algorithm.remove?

Except that it doesn't work in cases where you want to remove by value. So, it does part of the job but not all of it.

- Jonathan M Davis