Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 iterate over all items of array with const stride  (Read 1892 times) 0 Members and 1 Guest are viewing this topic.
moogie

JGO Ninja

Medals: 15
Projects: 6
Exp: 10 years

Java games rock!

 « Posted 2004-11-22 03:16:37 »

Is there a simple algorithm to iterate over all the elements in an array given a constant stride?

I am currently using a circularly linked list to popluate a temporary array with indecies to of the actual array by traversing through the linked list (stride) and then removing the node from the linked list and placing the node's value into the temporary array. this repeats until there is no more links left.

This temporary array of indecies is then used to popluate an new array with the values from the original array given the temporary array's values as indecies.

This method works, however is it very slow... is there a faster way? i.e. is there a mathematical formula i can use to generate the next index in the sequence?

Java4k RIP 2014
kevglass

« JGO Spiffy Duke »

Medals: 254
Projects: 25
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

 « Reply #1 - Posted 2004-11-22 05:14:23 »

I'm not quite sure what you're trying to do but is this it?

 1  2  3  4  5  6  7  8 `int[] targetArray = new int[1000];int stride = 7;int length = targetArray.length;for (int i=0;i

I assume its more complicated than this but hopefully this will give a started place to explain it more.

Kev

moogie

JGO Ninja

Medals: 15
Projects: 6
Exp: 10 years

Java games rock!

 « Reply #2 - Posted 2004-11-22 20:20:29 »

Quote
I'm not quite sure what you're trying to do but is this it?

 1  2  3  4  5  6  7  8 `int[] targetArray = new int[1000];int stride = 7;int length = targetArray.length;for (int i=0;i

I assume its more complicated than this but hopefully this will give a started place to explain it more.

Kev

hehe, i wish it was that simple...

I want to iterate over all the elements at most once. i.e. once an element has been processed it is effectively no longer part of the array and needs to be ignored.

my current algorithm does the following:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41 `stride=3Step 1input array: 1 2 3 4 5 6 7temporary container: 1 2 3 4 5 6 7output array:Step 2input array: 1 2 3 4 5 6 7temporary container: 1 2 4 5 6 7output array: 3Step 3input array: 1 2 3 4 5 6 7temporary container: 1 2 4 5 7output array: 3 6Step 4input array: 1 2 3 4 5 6 7temporary container: 1 4 5 7output array: 3 6 2Step 5input array: 1 2 3 4 5 6 7temporary container: 1 4 5output array: 3 6 2 7Step 6input array: 1 2 3 4 5 6 7temporary container: 1 4 output array: 3 6 2 7 5Step 7input array: 1 2 3 4 5 6 7temporary container: 4 output array: 3 6 2 7 5 1Step 8input array: 1 2 3 4 5 6 7temporary container:  output array: 3 6 2 7 5 1 4`

I want to be able to get rid of the temporary containter and directly be able to determine the next index to be placed in the output array...

Java4k RIP 2014
 Games published by our own members! Check 'em out!
kevglass

« JGO Spiffy Duke »

Medals: 254
Projects: 25
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

 « Reply #3 - Posted 2004-11-23 05:05:59 »

Ah ha! I thought it must be more complex than that Now thats a real humdinger of a problem. Have you considered storing a wrapping object in the array instead of the actual object with a indicator as to whether it's been used this time (obviously it wouldn't be thread safe).

Then once you've used an item mark it as used (like taking it out of temporary list) and then cycle through every element in your array counting up unused items until you reach your stride.

I suspect this would be faster but more ugly.

Kev

CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

 « Reply #4 - Posted 2004-11-23 10:13:15 »

Would this do the trick:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43 `public class TestApp {    public static void main(String args[]) {        TestApp t = new TestApp();        int in[] = {1,2,3,4,5,6,7};        int out[] = t.go(in, 3);        System.out.println("Results:");        for(int i=0;i= length) {                    inPos -= length;                }                while(used[inPos]) {                    inPos++;                    if(inPos >= length) {                        inPos -= length;                    }                }                strideCount++;            } while(strideCount < stride);            strideCount = 0;            out[count] = in[inPos];            used[inPos] = true;            count++;        } while(count < length);        return out;    }}`

Starfarer

Innocent Bystander

Java games rock!

 « Reply #5 - Posted 2004-11-23 19:30:51 »

Just to point out the obvious, but if you could arrange for the stride and array length to be co-prime (that is, have no factors in common), then simple modulo arithmetic would work perfectly.
moogie

JGO Ninja

Medals: 15
Projects: 6
Exp: 10 years

Java games rock!

 « Reply #6 - Posted 2004-11-23 22:48:22 »

starfarer: the array length will user defined and infact most likely to be a power of 2... which rules out co-prime and modulo aritmetric

kevglass: I am not sure that using an indicator will help that much... as it would mean more link traversal... but i will implement it to see

CaptianJester: that is almost what i am currently doing now... except that i use a circular linked list instead of your used array.

Java4k RIP 2014
CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

 « Reply #7 - Posted 2004-11-24 00:36:15 »

Ah, I see.  An array will be faster than a linked list for checking however.

I just ran a test on my machine (1.0GHz) and it took 451 milliseconds(depending on the stride) to iterate over 1,000,000 entries with this method.  Is that not fast enough?

How variable is the stride?  The larger the stride, the slower.

moogie

JGO Ninja

Medals: 15
Projects: 6
Exp: 10 years

Java games rock!

 « Reply #8 - Posted 2004-11-24 04:31:47 »

the stride is anywhere from 1 to 2^16

Java4k RIP 2014
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (38 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (57 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (48 views) 2015-03-05 14:36:23
 BurntPizza 21x LiquidNitrogen 21x basil_ 20x KevinWorkman 18x EgonOlsen 17x theagentd 16x Roquen 16x 65K 11x Varkas 10x Riven 10x wessles 10x princec 9x SHC 8x Ashedragon 8x phu004 8x lcass 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org