digitalmars.D - array operation request
- clayasaurus (19/19) Jun 25 2004 hello, i would like to request a simple array operation.
- Andy Friesen (14/25) Jun 25 2004 In the case of non-object types, you can do this:
- Regan Heath (8/44) Jun 25 2004 Nice! my only qualm is that this resizes on every addition. It'd be more...
- Andy Friesen (3/6) Jun 25 2004 That should work just fine.
- Arcane Jill (2/4) Jun 26 2004 delete array[2];
- clayasaurus (2/6) Jun 26 2004 thanks, i didn't realize you could do that. :)
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.
Jun 25 2004
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
Jun 25 2004
On Fri, 25 Jun 2004 21:00:12 -0700, Andy Friesen <andy ikagames.com> wrote:clayasaurus wrote: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..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; } } -- andyimport 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/
Jun 25 2004
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
Jun 25 2004
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];
Jun 26 2004
In article <cbjgv7$27r7$1 digitaldaemon.com>, Arcane Jill says...In article <cbiq4a$150g$1 digitaldaemon.com>, clayasaurus says...thanks, i didn't realize you could do that. :)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];
Jun 26 2004