We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Appending arrays, now dynamic!
Page Index Toggle Pages: 1
Appending arrays, now dynamic! (Read 608 times)
Appending arrays, now dynamic!
May 11th, 2006, 10:18pm
 
This is a code to append an object to an array of objects of your own class. ( Ben, maybe implement in processing, after you've added some error-messages and such ;) )

Code:

import java.lang.reflect.Array;

void setup()
{
noLoop();
// initializing temp variables part one
T[] a = new T[0];
T[] tmp = new T[3];
tmp[0] = new T(1,5);
tmp[1] = new T(2,6);
tmp[2] = new T(3,7);

// initializing temp variables part two
T[] b = new T[0];
T[] tmp2 = new T[3];
tmp2[0] = new T(10,50);
tmp2[1] = new T(20,60);
tmp2[2] = new T(30,70);

// appending each object
for(int i = 0; i < tmp.length; i++)
a = (T[])appendClass(a, tmp[i]);

// appending an entire array
b = (T[])appendClassArray(b,tmp2);

// printing the results, just to check
for(int i = 0; i < a.length; i++)
println(a[i].x+","+a[i].y+" - "+a[i].txt);
for(int i = 0; i < b.length; i++)
println(b[i].x+","+b[i].y+" - "+b[i].txt);
}

// Appending an object of a certain class to an array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClass(Object array, Object o)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+1);
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
Array.set(newArray,Array.getLength(newArray)-1,o);
return newArray;
}

// Appending an array of a certain class to another array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClassArray(Object array, Object oarray)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+Array.getLength(oarray));
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
System.arraycopy(oarray, 0, newArray, Array.getLength(array), Array.getLength(oarray));
return newArray;
}


// A temporary class, just for testing.
class T
{
float x, y;
String txt;
T(float X, float Y)
{
x = X;
y = Y;
txt = "";
for(int i = 0; i < 5; i++)
txt = txt+char((int)random(60,120));
}
}


-seltar
Re: Appending arrays, now dynamic!
Reply #1 - May 12th, 2006, 11:05am
 
Having had this argument out before about Vectors and using arraycopy to expand object arrays I went for a quick speed check on this new method of yours.

I may have got the code wrong, can someone please check?
Code:

import java.lang.reflect.Array;
int timer = 0;
void setup()
{
noLoop();
T a[] = new T[1];
T b[] = new T[1];
timer = millis();
for(int i = 0; i < 10000; i++){
a = (T[])appendClass(a, new T());
}
println(millis() - timer);
Vector c = new Vector();
timer = millis();
for(int i = 0; i < 10000; i++){
c.add(new T());
}
println(millis() - timer);
}

// Appending an object of a certain class to an array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClass(Object array, Object o)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+1);
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
Array.set(newArray,Array.getLength(newArray)-1,o);
return newArray;
}

// Appending an array of a certain class to another array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClassArray(Object array, Object oarray)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+Array.getLength(oarray));
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
System.arraycopy(oarray, 0, newArray, Array.getLength(array), Array.getLength(oarray));
return newArray;
}

// A temporary class, just for testing.
class T
{
float x, y;
String txt;
T()
{
txt = "";
for(int i = 0; i < 5; i++)
txt = txt+char((int)random(60,120));
}
}

My test suggests that using Vector() is still the fast route to expansion. I may have gotten it wrong though.
Re: Appending arrays, now dynamic!
Reply #2 - May 12th, 2006, 12:48pm
 
it's true that adding single objects is slower. when i remember correctly that's because Vector is internally not growing by adding 1, but by a factor. to speed up appendClass you'd want it to grow by factor 2 or something smarter. but in the end you'll be close to the Vector class ...

but:
appending / concatenating whole arrays is indeed faster, see test:
Code:

import java.lang.reflect.Array;

int timer = 0;

void setup()
{
// max number of elements
//
int MAX = 10000;

// set up the arrays
//
T a[] = new T[0];
T b[] = new T[0];
for(int i = 0; i < MAX; i++){
a = (T[])appendClass(a, new T());
}
for(int i = 0; i < MAX; i++){
b = (T[])appendClass(b, new T());
}

// set up a vector
//
Vector c = new Vector();
for (int i = 0; i < MAX; i++) {
c.add(new T());
}

// test and time appendClassArray()
//
timer = millis();
a = (T[])appendClassArray(a, b);
println( millis() - timer );
println( a.length );

// test and time Vector.addAll( Collection c )
//
timer = millis();
c.addAll(Arrays.asList(b));
println( millis() - timer );
println( c.size() );

noLoop();
}



// Appending an object of a certain class to an array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClass(Object array, Object o)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+1);
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
Array.set(newArray,Array.getLength(newArray)-1,o);
return newArray;
}

// Appending an array of a certain class to another array of the same class
// remember to cast (ClassName[]) in front of the function
Object appendClassArray(Object array, Object oarray)
{
Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)+Array.getLength(oarray));
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
System.arraycopy(oarray, 0, newArray, Array.getLength(array), Array.getLength(oarray));
return newArray;
}

// A temporary class, just for testing.
class T
{
float x, y;
String txt;
T()
{
txt = "";
for(int i = 0; i < 5; i++)
txt = txt+char((int)random(60,120));
}
}


and .. arraycopy() is now (as of rev. 115) part of the processing-api:
fry wrote on May 11th, 2006, 1:28am:
+ added arraycopy(from[], to[], count)


best,
F
Re: Appending arrays, now dynamic!
Reply #3 - May 12th, 2006, 1:00pm
 
bummer.. i just finished implementing all this stuff for 0116 yesterday as you were putting it all together.

it'll use the built-in functions expand(), concat(), append(), subset() etc and will starting with 0116 work on arbitrary arrays.
Re: Appending arrays, now dynamic!
Reply #4 - May 12th, 2006, 4:39pm
 
hehe, oh well.. at least i learned something, instead of just getting it for free Wink

and hurray for fry, for implementing all those pretty functions Smiley

-seltar
Page Index Toggle Pages: 1