Thread overview | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 30, 2006 Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
How do I remove an element from a dynamic array? int[] a = [1,2,3,4,5]; I tried every syntax I could think of: a[3] = void; a[3..4] = void; a[3..4] = a[3..3]; a[3] = []; a[3..4] = []; delete a[3]; delete a[3..4]; The last one compiles, but fills a[0] with garbage. I hope the answer isn't: a = a[0..3] ~ a[4..length]; Thanks! --bb |
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> How do I remove an element from a dynamic array?
>
> int[] a = [1,2,3,4,5];
>
> I tried every syntax I could think of:
>
> a[3] = void;
> a[3..4] = void;
> a[3..4] = a[3..3];
> a[3] = [];
> a[3..4] = [];
> delete a[3];
> delete a[3..4];
>
> The last one compiles, but fills a[0] with garbage.
>
> I hope the answer isn't:
>
> a = a[0..3] ~ a[4..length];
>
> Thanks!
> --bb
From a COW (copy on write) standpoint, that last one is the only safe one, i.e: a = a[0..x] ~ a[x+1..$]; with 'x' the index of the element you want to remove.
If you don't need COW and don't care for the order of the elements, you can do this: a[x] = a[$-1]; a.length = a.length - 1; //a.length-- won't work
L.
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> How do I remove an element from a dynamic array?
To me it is not clear what you mean by "remove".
Are you talking about associative arrays?
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> How do I remove an element from a dynamic array?
>
> int[] a = [1,2,3,4,5];
>
> I tried every syntax I could think of:
>
> a[3] = void;
> a[3..4] = void;
> a[3..4] = a[3..3];
> a[3] = [];
> a[3..4] = [];
> delete a[3];
> delete a[3..4];
>
> The last one compiles, but fills a[0] with garbage.
>
> I hope the answer isn't:
>
> a = a[0..3] ~ a[4..length];
>
> Thanks!
> --bb
From my personal tools library...
// remove an item from an array
template drop(T)
{
T drop( inout T[] arr, int which )
{
debug if ( which>=arr.length)
throw new Exception(str.format("Attempt to drop position %s from size %s",which,arr.length));
T result = arr[which];
int max = arr.length-1;
for (; which < max; which++ ) arr[which]=arr[which+1];
arr.length= max;
return result;
}
}
Even though it returns the array, it modifies it in place.
-DavidM
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> How do I remove an element from a dynamic array?
>
> int[] a = [1,2,3,4,5];
>
> I tried every syntax I could think of:
>
> a[3] = void;
> a[3..4] = void;
> a[3..4] = a[3..3];
> a[3] = [];
> a[3..4] = [];
> delete a[3];
> delete a[3..4];
>
> The last one compiles, but fills a[0] with garbage.
>
> I hope the answer isn't:
>
> a = a[0..3] ~ a[4..length];
>
> Thanks!
> --bb
Assuming you mean remove as in extract and throw away the item at a given index, then yes that last one is the only way. In Cashew this is abstracted to array.removeIndex(size_t). I really can't think of any other way it could be done, though...
Except for maybe:
# a[3 .. a.length - 1] = a[4 .. a.length];
# a.length = a.length - 1;
Hrm, not really any better. Or maybe, using Cashew (but avoiding .removeIndex):
# a[3 .. a.length].rotl;
# a.pop;
Essentially, the same thing anyway.
-- Chris Nicholson-Sauls
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | Chris Nicholson-Sauls wrote:
> Bill Baxter wrote:
>> How do I remove an element from a dynamic array?
>>
>> int[] a = [1,2,3,4,5];
>>
>> I tried every syntax I could think of:
>>
>> a[3] = void;
>> a[3..4] = void;
>> a[3..4] = a[3..3];
>> a[3] = [];
>> a[3..4] = [];
>> delete a[3];
>> delete a[3..4];
>>
>> The last one compiles, but fills a[0] with garbage.
>>
>> I hope the answer isn't:
>>
>> a = a[0..3] ~ a[4..length];
>>
>> Thanks!
>> --bb
>
> Assuming you mean remove as in extract and throw away the item at a given index, then yes that last one is the only way. In Cashew this is abstracted to array.removeIndex(size_t). I really can't think of any other way it could be done, though...
>
> Except for maybe:
> # a[3 .. a.length - 1] = a[4 .. a.length];
> # a.length = a.length - 1;
Unfortunately, I think this is illegal. See my thread in this group entitled "Removing an array element in order."
Sean
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> Chris Nicholson-Sauls wrote:
>
>> Bill Baxter wrote:
>>
>>> How do I remove an element from a dynamic array?
>>>
>>> int[] a = [1,2,3,4,5];
>>>
>>> I tried every syntax I could think of:
>>>
>>> a[3] = void;
>>> a[3..4] = void;
>>> a[3..4] = a[3..3];
>>> a[3] = [];
>>> a[3..4] = [];
>>> delete a[3];
>>> delete a[3..4];
>>>
>>> The last one compiles, but fills a[0] with garbage.
>>>
>>> I hope the answer isn't:
>>>
>>> a = a[0..3] ~ a[4..length];
>>>
>>> Thanks!
>>> --bb
>>
>>
>> Assuming you mean remove as in extract and throw away the item at a given index, then yes that last one is the only way. In Cashew this is abstracted to array.removeIndex(size_t). I really can't think of any other way it could be done, though...
>>
>> Except for maybe:
>> # a[3 .. a.length - 1] = a[4 .. a.length];
>> # a.length = a.length - 1;
>
>
> Unfortunately, I think this is illegal. See my thread in this group entitled "Removing an array element in order."
>
>
> Sean
Ah. Most likely because of pointer corruption from assigning a slice of an array into that same array. Should still be doable with a little magic, but probably more trouble than its worth. Yet another reason I think Phobos needs an array utils module like that in my Cashew or like Oskar's work... or any of the others that have popped up now and then. Even some of std.string could be abstracted into "std.array" for cases where Unicode compatability is not neccessary (closed systems using strictly char[] or dchar[], for example).
-- Chris Nicholson-Sauls
|
October 30, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > How do I remove an element from a dynamic array? [...] > I hope the answer isn't: > > a = a[0..3] ~ a[4..length]; That's what I use... --anders |
October 31, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | David Medlock wrote: > Bill Baxter wrote: >> How do I remove an element from a dynamic array? >> >> int[] a = [1,2,3,4,5]; >> >> I tried every syntax I could think of: >> >> a[3] = void; >> a[3..4] = void; >> a[3..4] = a[3..3]; >> a[3] = []; >> a[3..4] = []; >> delete a[3]; >> delete a[3..4]; >> >> The last one compiles, but fills a[0] with garbage. >> >> I hope the answer isn't: >> >> a = a[0..3] ~ a[4..length]; >> >> Thanks! >> --bb > > > From my personal tools library... > > // remove an item from an array > template drop(T) > { > T drop( inout T[] arr, int which ) > { > debug if ( which>=arr.length) > throw new Exception(str.format("Attempt to drop position %s from size %s",which,arr.length)); > T result = arr[which]; > int max = arr.length-1; > for (; which < max; which++ ) arr[which]=arr[which+1]; > arr.length= max; > return result; > } > } > > > Even though it returns the array, it modifies it in place. > > -DavidM Thanks David, this seems like the best answer in terms of efficiency, although I suppose the a = a[0..3] ~ a[4..length]; could theoretically be pretty similar depending how the compiler implements it. There really should be syntax in the language or at least functions like the above in the standard library for this (and for removing slices). Otherwise std::vector starts to look handy compared to D arrays, and you don't want that! ;-) Built-in arrays are supposed to be easier to use than library code, after all. // Erases the element at position pos. iterator std::vector::erase(iterator pos) //Erases the range [first, last) iterator std::vector::erase(iterator first, iterator last) Other than that, it looks like arrays support a sane syntax for everything std::vector has, except maybe std::vector::reserve()/std::vector::capacity() which has also been discussed recently. --bb |
October 31, 2006 Re: Removing elements from dynamic arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> best answer in terms of efficiency
For me removing an element does not affect other elements.
|
Copyright © 1999-2021 by the D Language Foundation