www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array.remove(int), similar to aa.remove(key)

reply Lionello Lunesu <lio lunesu.remove.com> writes:
array.remove(int) could simply replace [i] with [$-1] and decrement the 
length.

I need this quite a lot and it gets tiresome writing the same 'for' over 
and over. Furthermore, like I've mentioned in the title: since AA's have 
it, it makes sense to add it to normal arrays as well.

L.
May 24 2006
next sibling parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Lionello Lunesu skrev:
 array.remove(int) could simply replace [i] with [$-1] and decrement the 
 length.
 
 I need this quite a lot and it gets tiresome writing the same 'for' over 
 and over. Furthermore, like I've mentioned in the title: since AA's have 
 it, it makes sense to add it to normal arrays as well.
Since arrays are ordered, it is not clear that array.remove(int) should be array[i] = array[$-1], array.length = array.length -1; rather than: array = array[0..$i-1] ~ array[i+1..$]; Note: if you want to implement array.remove(int) by yourself, it is trivial (untested code): template remove(ArrTy, IntTy) { static assert(is(IntTy : int)); void remove(ArrTy arr, IntTy ix) { arr[ix] = arr[$-1]; arr.length = arr.length - 1; } } and use it such as: char[] t = "abc".dup; assert(t.remove(1) == "ac"); Regards, Oskar
May 24 2006
next sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Ough, some mistakes.

Oskar Linde skrev:
 Lionello Lunesu skrev:
 array.remove(int) could simply replace [i] with [$-1] and decrement 
 the length.

 I need this quite a lot and it gets tiresome writing the same 'for' 
 over and over. Furthermore, like I've mentioned in the title: since 
 AA's have it, it makes sense to add it to normal arrays as well.
Since arrays are ordered, it is not clear that array.remove(int) should be array[i] = array[$-1], array.length = array.length -1; rather than: array = array[0..$i-1] ~ array[i+1..$];
$i is a typo, should be i.
 
 
 Note: if you want to implement array.remove(int) by yourself, it is 
 trivial (untested code):
 
 template remove(ArrTy, IntTy) {
     static assert(is(IntTy : int));
     void remove(ArrTy arr, IntTy ix) {
         arr[ix] = arr[$-1];
         arr.length = arr.length - 1;
     }
 }
 
 and use it such as:
 
 char[] t = "abc".dup;
 assert(t.remove(1) == "ac");
The template function above doesn't return anything, so this should of course be: char[] t = "abc".dup; t.remove(1); assert(t == "ac"); /Oskar
May 24 2006
prev sibling parent Lionello Lunesu <lio lunesu.remove.com> writes:
Oskar Linde wrote:
 Lionello Lunesu skrev:
 array.remove(int) could simply replace [i] with [$-1] and decrement 
 the length.

 I need this quite a lot and it gets tiresome writing the same 'for' 
 over and over. Furthermore, like I've mentioned in the title: since 
 AA's have it, it makes sense to add it to normal arrays as well.
Since arrays are ordered, it is not clear that array.remove(int) should be array[i] = array[$-1], array.length = array.length -1; rather than: array = array[0..$i-1] ~ array[i+1..$];
OK, so there could be two removes, ordered and unordered.
 Note: if you want to implement array.remove(int) by yourself, it is 
 trivial (untested code):
 
 template remove(ArrTy, IntTy) {
     static assert(is(IntTy : int));
     void remove(ArrTy arr, IntTy ix) {
         arr[ix] = arr[$-1];
         arr.length = arr.length - 1;
     }
 }
That's what I did, and it's the code I end up coding/copy-pasting whenever I need it. Nice touch though, that static assert. L.
May 24 2006
prev sibling parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Lionello Lunesu wrote:
 array.remove(int) could simply replace [i] with [$-1] and decrement the 
 length.
 
 I need this quite a lot and it gets tiresome writing the same 'for' over 
 and over. Furthermore, like I've mentioned in the title: since AA's have 
 it, it makes sense to add it to normal arrays as well.
 
 L.
I ended up moving all my personal "always copy-pasted template functions" into my personal library (Cashew) so I didn't have to paste them into anything anymore. ;) Might consider doing the same. If Cashew's ArrayUtils module suits you, I can just post it for you. Interface doc'd at: http://www.codemeu.com:81/~pontiff/projects/cashew/docs/ArrayUtils.html Code at: http://www.codemeu.com:81/~pontiff/projects/cashew/cashew.zip -- Chris Nicholson-Sauls
May 25 2006