Inserting contents of one array into another

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Inserting contents of one array into another

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:22 pm

by allistar >> Sun, 20 Apr 2003 23:53:49 GMT

Hi all,
I have an array of objects (which could be quite large) and a second array (holding the same types of objects as the first array). I would like to insert the contents of array 2 into array 1 at a particular position.

e.g.
array 1 contains 200,000 objects.
array 2 contains 10,000 objects.
I would like to insert those 10,000 objects from array 1 into position (say) 139,000 of array 1.

That is not hard to do in code, but because of the size of array 1
any solution I come up with is slow.

Does anyone know of a trick or hack to achieve this?

Thanks,
Allistar.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Inserting contents of one array into another

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:22 pm

by brendan1 >> Mon, 28 Apr 2003 11:05:51 GMT

Hi Allistar,
Haven't tried this but I think it could be due to the way that array elements are accessed in Jade. If your code is something like

// assume that startInx, the insertion point, is already set up
targetInx := startInx;
sourceInx := 1;
sourceMax := sourceArray.size;
while sourceInx <= sourceMax do
bigTargetArray.insert(targetInx, sourceArray[sourceInx]);
sourceInx := sourceInx + 1;
targetInx := targetInx + 1;
endwhile;

then there will be two linear searches on each iteration, one on bigTargetArray to position for the insert and one on sourceArray to position for the at. Remember that Arrays are more like linked lists (subclass of List) than arrays in C++ or VB, say, so Jade (I suspect) has to do a linear scan to position itself properly at an index.

You might be able to speed up the code by using an iterator. For example,

// assume that startInx, the insertion point, is already set up
create tempArray transient; // ObjectArray will do
iter := bigTargetArray.createIterator;
targetInx := 1;
while startInx > targetInx and iter.next(nextArrayObj) do
tempArray.add(nextArrayObj); // Not sure, but I am assuming that this is fast and doesn't do a linear scan !?
targetInx := targetInx + 1;
endwhile;
sourceArray.copy(tempArray); // should append source array to end of tempArray
while iter.next(nextArrayObj) do
tempArray.add(nextArrayObj);
endwhile;

bigTargetArray.clear;
tempArray.copy(bigTargetArray);
epilog
delete iter;
delete tempArray;

In spite of the extra copying (the two while loops and the copy just before the epilog) I would suspect that this will still be much faster than the first code fragment for large arrays.

I'd be interested to hear it goes.

Brendan

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Inserting contents of one array into another

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:22 pm

by allistar >> Mon, 28 Apr 2003 22:11:42 GMT
Hi Allistar,
Haven't tried this but I think it could be due to the way that array elements are accessed in Jade. If your code is something like

[snip]
In spite of the extra copying (the two while loops and the copy just before the epilog) I would suspect that this will still be much faster than the first code fragment for large arrays.

I'd be interested to hear it goes.

Brendan

Hi Brendan,
Thanks for that. I ended up creating a new array and using an interator to add items from the old arrays into the new one accordingly. That was the solution I didn't want to use as even though it is faster than indexing into the other two arrays, it still requires me to touch every single object in the two arrays to make the new combined array (which is bad if there are a million objects). I was hoping that there was some trick in Jade to take advantage of how the collection blocks work to combine two large arrays efficiently.

Thanks for the tips though, it works fast enough for the time being.

Regards,
Allistar.

------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz ------------------------------------------------------------------


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 14 guests