Jump to page: 1 2
Thread overview
remove from array
Feb 27, 2007
Thorsten
Feb 27, 2007
BCS
Feb 27, 2007
Bill Baxter
Feb 27, 2007
Thorsten
Feb 28, 2007
Bill Baxter
Feb 27, 2007
Bill Baxter
Feb 28, 2007
BCS
Feb 27, 2007
Thorsten
Feb 28, 2007
torhu
Feb 28, 2007
Frits van Bommel
Feb 28, 2007
BCS
Feb 27, 2007
Kyle Furlong
Feb 28, 2007
David L. Davis
February 27, 2007
How can I remove values from an array ?
February 27, 2007
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 .. $];
}


February 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
February 27, 2007
BCS wrote:
> 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 .. $];
> }
> 
> 

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

--bb
February 27, 2007
BCS wrote:
> 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 .. $];
> }
> 
> 

Using ~ works, but it's slower than using memmorve (what's in Cashew utils).

--bb
February 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 ???
February 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
> 
> --bb

Yeah thanks, it's the drop-function.
Will this be added to phobos once ?

February 28, 2007
Thorsten wrote:
> 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
>>
>> --bb
> 
> Yeah thanks, it's the drop-function.
> Will this be added to phobos once ?
> 

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.

--bb
February 28, 2007
Thorsten wrote:
>> <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 ???

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;
February 28, 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 .. $];
}


« First   ‹ Prev
1 2