digitalmars.D - array.remove(int), similar to aa.remove(key)
- Lionello Lunesu (6/6) May 24 2006 array.remove(int) could simply replace [i] with [$-1] and decrement the
- Oskar Linde (19/25) May 24 2006 Since arrays are ordered, it is not clear that array.remove(int) should ...
- Oskar Linde (9/41) May 24 2006 $i is a typo, should be i.
- Lionello Lunesu (5/30) May 24 2006 That's what I did, and it's the code I end up coding/copy-pasting
- Chris Nicholson-Sauls (9/17) May 25 2006 I ended up moving all my personal "always copy-pasted template functions...
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
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
Ough, some mistakes. Oskar Linde skrev:Lionello Lunesu skrev:$i is a typo, should be i.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");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
Oskar Linde wrote:Lionello Lunesu skrev:OK, so there could be two removes, ordered and unordered.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; } }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
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