www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array operation request

reply clayasaurus <clayasaurus_member pathlink.com> writes:
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
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
parent reply Regan Heath <regan netwin.co.nz> writes:
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;

              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
parent Andy Friesen <andy ikagames.com> writes:
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
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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
parent clayasaurus <clayasaurus_member pathlink.com> writes:
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. :)
Jun 26 2004