Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (540) Games in Android Showcase (133) games submitted by our members Games in WIP (603) 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 1780 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: 219
Projects: 24
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: 219
Projects: 24
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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 Mr.CodeIt (24 views) 2014-12-23 03:34:11 rwatson462 (53 views) 2014-12-15 09:26:44 Mr.CodeIt (45 views) 2014-12-14 19:50:38 BurntPizza (85 views) 2014-12-09 22:41:13 BurntPizza (110 views) 2014-12-08 04:46:31 JscottyBieshaar (79 views) 2014-12-05 12:39:02 SHC (89 views) 2014-12-03 16:27:13 CopyableCougar4 (96 views) 2014-11-29 21:32:03 toopeicgaming1999 (155 views) 2014-11-26 15:22:04 toopeicgaming1999 (152 views) 2014-11-26 15:20:36
 Rayvolution 47x basil_ 29x HeroesGraveDev 26x BurntPizza 24x LiquidNitrogen 24x appel 18x kpars 18x kevglass 17x princec 15x NegativeZero 15x KevinWorkman 15x Riven 15x gouessej 13x pitbuller 11x Ecumene 10x Gibbo3771 10x
 Resources 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:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50
 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