Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 26, 2004 array operation request | ||||
---|---|---|---|---|
| ||||
hello, i would like to request a simple array operation. first i will give a simple problem and some source that might be used to solve that problem. Lets say you have a dynamic array with names "bob", "jon", "doe". You want to remove "jon", but you don't want the array to resize to 2 and not have a null value for the array. right now i use code like this so solve the problem int[] bob; setvalues(bob,1,2,3); // bob's length is 3 // oh yes, and in the future i hope for ... int[] bob = [1,2,3]; ;) bob = removeValueInArray(bob, 1); // bob should now be length 2 with values 1 and 3 int[] removeValueInArray(int array[], int remove) {// creates a new array with out the specified value int newarray[]; newarray = array[0 .. (remove)]; newarray ~= array[(remove+1) .. array.length]; return newarray; } maybe there can be something like array.remove(2) which would remove a certain value in the array and resize it appropriately. |
June 26, 2004 Re: array operation request | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote:
> hello, i would like to request a simple array operation.
>
> first i will give a simple problem and some source that might be used to solve
> that problem. Lets say you have a dynamic array with names "bob", "jon", "doe".
> You want to remove "jon", but you don't want the array to resize to 2 and not
> have a null value for the array.
>
> right now i use code like this so solve the problem
>
> int[] bob; setvalues(bob,1,2,3); // bob's length is 3
> // oh yes, and in the future i hope for ... int[] bob = [1,2,3]; ;)
In the case of non-object types, you can do this:
import std.stdarg;
template createArray(T) {
T[] createArray(...) {
T[] result;
foreach (TypeInfo ti; _arguments) {
assert(typeid(T) == ti);
result ~= va_arg!(T)(_argptr);
}
return result;
}
}
-- andy
|
June 26, 2004 Re: array operation request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | On Fri, 25 Jun 2004 21:00:12 -0700, Andy Friesen <andy@ikagames.com> wrote: > clayasaurus wrote: >> hello, i would like to request a simple array operation. first i will give a simple problem and some source that might be used to solve >> that problem. Lets say you have a dynamic array with names "bob", "jon", "doe". >> You want to remove "jon", but you don't want the array to resize to 2 and not >> have a null value for the array. right now i use code like this so solve the problem >> >> int[] bob; setvalues(bob,1,2,3); // bob's length is 3 >> // oh yes, and in the future i hope for ... int[] bob = [1,2,3]; ;) > > In the case of non-object types, you can do this: > > import std.stdarg; > template createArray(T) { > T[] createArray(...) { > T[] result; > foreach (TypeInfo ti; _arguments) { > assert(typeid(T) == ti); > result ~= va_arg!(T)(_argptr); > } > return result; > } > } > > -- andy Nice! my only qualm is that this resizes on every addition. It'd be more efficient if it knew the size before hand and set the length. Does _arguments have a length property? if so.. > import std.stdarg; > template createArray(T) { > T[] createArray(...) { > T[] result; result.length = _arguments.length; > foreach (uint i, TypeInfo ti; _arguments) { > assert(typeid(T) == ti); > result[i] = va_arg!(T)(_argptr); > } > return result; > } > } Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
June 26, 2004 Re: array operation request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote:
> my only qualm is that this resizes on every addition. It'd be more efficient if it knew the size before hand and set the length. Does _arguments have a length property? if so..
That should work just fine.
-- andy
|
June 26, 2004 Re: array operation request | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | In article <cbiq4a$150g$1@digitaldaemon.com>, clayasaurus says... >maybe there can be something like array.remove(2) which would remove a certain value in the array and resize it appropriately. delete array[2]; |
June 26, 2004 Re: array operation request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | In article <cbjgv7$27r7$1@digitaldaemon.com>, Arcane Jill says... > >In article <cbiq4a$150g$1@digitaldaemon.com>, clayasaurus says... > >>maybe there can be something like array.remove(2) which would remove a certain value in the array and resize it appropriately. > >delete array[2]; > thanks, i didn't realize you could do that. :) |
Copyright © 1999-2021 by the D Language Foundation