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.

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:

> 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_.
February 28, 2007
Reply to Thorsten,

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

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)


February 28, 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


February 28, 2007
Bill Baxter wrote:
> 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

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-Sauls
March 01, 2007
Chris Nicholson-Sauls wrote:
> 
> Bill Baxter wrote:
>> 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
> 
> 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-Sauls

And there is now a new version in SVN (v0.10.4) which has some little cleanups.

-- Chris Nicholson-Sauls
1 2
Next ›   Last »