digitalmars.D - remove from array
- Thorsten (1/1) Feb 27 2007 How can I remove values from an array ?
-
BCS
(6/8)
Feb 27 2007
- Bill Baxter (5/18) Feb 27 2007 This has the function you're looking for and a number of others you'll
- Thorsten (3/9) Feb 27 2007 Yeah thanks, it's the drop-function.
- Bill Baxter (10/22) Feb 27 2007 Maybe in theory, but it's doubtful. There is only one person able to
- Chris Nicholson-Sauls (5/26) Feb 28 2007 Hmm, wow, I really need to revisit Cashew... think I'll go do that now....
- Chris Nicholson-Sauls (3/32) Feb 28 2007 And there is now a new version in SVN (v0.10.4) which has some little cl...
- Bill Baxter (3/16) Feb 27 2007 Using ~ works, but it's slower than using memmorve (what's in Cashew uti...
- BCS (7/10) Feb 27 2007 Good point. OTOH if you want to keep the source untouched, you need to a...
- Thorsten (10/16) Feb 27 2007 Sorry, I tried that, and it is too expensive !!
- torhu (14/33) Feb 27 2007 I see Bill already told you about cashew, but here's an answer anyway.
- Frits van Bommel (37/56) Feb 27 2007 Nonsense.
- BCS (4/21) Feb 27 2007 It would be kida slow wouldn't it.
- Kyle Furlong (3/4) Feb 27 2007 Queries of this kind belong in D.learn or Google. :D
- David L. Davis (14/15) Feb 27 2007 Have a look at the "del" function in the XArrayT.d code...but I thin...
Reply to Thorsten,How can I remove values from an array ?<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }
Feb 27 2007
BCS wrote:Reply to Thorsten,This has the function you're looking for and a number of others you'll probably be looking for soon after. :-) http://www.dsource.org/projects/cashew/browser/trunk/cashew/utils/array.d --bbHow can I remove values from an array ?<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }
Feb 27 2007
Bill Baxter Wrote:This has the function you're looking for and a number of others you'll probably be looking for soon after. :-) http://www.dsource.org/projects/cashew/browser/trunk/cashew/utils/array.d --bbYeah thanks, it's the drop-function. Will this be added to phobos once ?
Feb 27 2007
Thorsten wrote:Bill Baxter Wrote:Maybe in theory, but it's doubtful. There is only one person able to make commits to Phobos, Walter, and his time is taken up by making changes to the compiler that /cannot/ be worked around by library code. That plus now there's Tango, which *is* an open and collaborative standard library effort. There's probably something like drop in Tango already. If not they'd probably be willing to add it. I see they have a remove() function that is like a combination of find()+drop(), and they have find() but no drop. --bbThis has the function you're looking for and a number of others you'll probably be looking for soon after. :-) http://www.dsource.org/projects/cashew/browser/trunk/cashew/utils/array.d --bbYeah thanks, it's the drop-function. Will this be added to phobos once ?
Feb 27 2007
Bill Baxter wrote:BCS wrote:Hmm, wow, I really need to revisit Cashew... think I'll go do that now. :) So much left unfinished and unwritten on there. Bad me. But thanks for the promotion. Good to know I'm not the only one who uses it. -- Chris Nicholson-SaulsReply to Thorsten,This has the function you're looking for and a number of others you'll probably be looking for soon after. :-) http://www.dsource.org/projects/cashew/browser/trunk/cashew/utils/array.d --bbHow can I remove values from an array ?<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }
Feb 28 2007
Chris Nicholson-Sauls wrote:Bill Baxter wrote:And there is now a new version in SVN (v0.10.4) which has some little cleanups. -- Chris Nicholson-SaulsBCS wrote:Hmm, wow, I really need to revisit Cashew... think I'll go do that now. :) So much left unfinished and unwritten on there. Bad me. But thanks for the promotion. Good to know I'm not the only one who uses it. -- Chris Nicholson-SaulsReply to Thorsten,This has the function you're looking for and a number of others you'll probably be looking for soon after. :-) http://www.dsource.org/projects/cashew/browser/trunk/cashew/utils/array.d --bbHow can I remove values from an array ?<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }
Feb 28 2007
BCS wrote:Reply to Thorsten,Using ~ works, but it's slower than using memmorve (what's in Cashew utils). --bbHow can I remove values from an array ?<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }
Feb 27 2007
Reply to Bill,Using ~ works, but it's slower than using memmorve (what's in Cashew utils).Good point. OTOH if you want to keep the source untouched, you need to allocate anyway. Thinking of that, a dup is needed to keep things consistent T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1].dup : arr[0..i] ~ arr[i+1 .. $]; }
Feb 27 2007
<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }Sorry, I tried that, and it is too expensive !! Because it creates about 3 temporary instances. I would say : void Remove(T)(inout T[] arr, int i) { for(uint j = i;j < arr.length-1;++j) arr[j] = arr[j+1]; arr.length = arr.length - 1; } can someone accelerate this ???
Feb 27 2007
Thorsten wrote:I see Bill already told you about cashew, but here's an answer anyway. You can use memmove to do the copying. Using memmove also seems to be the fastest way to insert elements in the middle of an array, if you need that too. void Remove(T)(inout T[] arr, int i) { assert(arr && i < arr.length); memmove(arr.ptr + i, arr.ptr + i + 1, (arr.length - i - 1) * T.sizeof); arr.length = arr.length - 1; } If you don't care about the order, you can of course just do this: arr[i] = arr[$-1]; arr.length = arr.length - 1;<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }Sorry, I tried that, and it is too expensive !! Because it creates about 3 temporary instances. I would say : void Remove(T)(inout T[] arr, int i) { for(uint j = i;j < arr.length-1;++j) arr[j] = arr[j+1]; arr.length = arr.length - 1; } can someone accelerate this ???
Feb 27 2007
Thorsten wrote:Nonsense. First of all, only one of the last two clauses is even evaluated. And second, only the second clause allocates, and even then only once. Slicing an array doesn't create a copy, it just creates a new (length, pointer) pair. Only the '~' allocates, and this is not a temporary but the return value. That function might be improved a bit by also returning a plain slice for the case i == 0, but with that change it's perfectly fine as long as copy-on-write is acceptable. If you are certain no copies of the original array (including overlapping arrays) will be kept, you can use something like your suggestion:<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }Sorry, I tried that, and it is too expensive !! Because it creates about 3 temporary instances.I would say : void Remove(T)(inout T[] arr, int i) { for(uint j = i;j < arr.length-1;++j) arr[j] = arr[j+1]; arr.length = arr.length - 1; } can someone accelerate this ???Using memmove to replace the loop has already been suggested, that should usually be fast. Though consider checking if the removed element is closer to the beginning of the array, and if so moving the first part instead of the second part followed by "arr = arr[1 .. $]": --- // warning: untested code void Remove(T)(inout T[] arr, int i) { if (i < (arr.length / 2)) { for(uint j = i; j > 0; --j) arr[j] = arr[j-1]; arr = arr[1 .. $]; } else { for(uint j = i; j < arr.length-1; ++j) arr[j] = arr[j+1]; arr.length = arr.length - 1; } } --- where the for loops may also be replaced by memmove. This should perform less memory operations for first-half removals. However, this has the nasty side effect that using ~= on the resulting array will _reallocate_.
Feb 27 2007
Reply to Thorsten,It would be kida slow wouldn't it. I only see one allocate and it's not a temp, where are the others? (BTW slicing doesn't allocate)<totaly untested code> T[] Remove(T)(T[] arr, int i) { return i==arr.length-1 ? arr[0..$-1] : arr[0..i] ~ arr[i+1 .. $]; }Sorry, I tried that, and it is too expensive !! Because it creates about 3 temporary instances. I would say : void Remove(T)(inout T[] arr, int i) { for(uint j = i;j < arr.length-1;++j) arr[j] = arr[j+1]; arr.length = arr.length - 1; } can someone accelerate this ???
Feb 27 2007
Thorsten wrote:How can I remove values from an array ?Queries of this kind belong in D.learn or Google. :D http://www.google.com/search?hl=en&safe=off&q=remove+values+from+array+d+programming&btnG=Search
Feb 27 2007
"Thorsten" <toki78 usenet.cnntp.org> wrote in message news:es2e19$1a6m$1 digitalmars.com...How can I remove values from an array ?Have a look at the "del" function in the XArrayT.d code...but I think you'll find that all the functions do be very useful. I've just tested this code against dmd v1.007 on WinXP SP2 and it worked fine. * XArrayT.d - D array extenders. A set of template functions to handle array overlapping slices by Andrew Fedoniouk, with the indexr() function added by Chris, and some minor bug fixes along with a unittest done by me. http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Feb 27 2007