Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 1614 times)
0 Members and 1 Guest are viewing this topic.
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Posted 2004-11-22 04: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?
Offline kevglass

JGO Kernel


Medals: 123
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-11-22 06: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<length;i+=stride) {
    // deal with array element here
   System.out.println(targetArray[i]);
}


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

Kev

Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #2 - Posted 2004-11-22 21: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<length;i+=stride) {
    // deal with array element here
   System.out.println(targetArray[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=3

Step 1
input array: 1 2 3 4 5 6 7
temporary container: 1 2 3 4 5 6 7
output array:

Step 2
input array: 1 2 3 4 5 6 7
temporary container: 1 2 4 5 6 7
output array: 3

Step 3
input array: 1 2 3 4 5 6 7
temporary container: 1 2 4 5 7
output array: 3 6

Step 4
input array: 1 2 3 4 5 6 7
temporary container: 1 4 5 7
output array: 3 6 2

Step 5
input array: 1 2 3 4 5 6 7
temporary container: 1 4 5
output array: 3 6 2 7

Step 6
input array: 1 2 3 4 5 6 7
temporary container: 1 4
output array: 3 6 2 7 5

Step 7
input array: 1 2 3 4 5 6 7
temporary container: 4
output array: 3 6 2 7 5 1

Step 8
input array: 1 2 3 4 5 6 7
temporary 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...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 123
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


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

Ah ha! I thought it must be more complex than that Smiley 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

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #4 - Posted 2004-11-23 11: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<out.length;i++) {
            System.out.println(out[i]);
        }
    }
    public int[] go(int in[], int stride) {
        int length = in.length;
        int out[] = new int[length];
        boolean used[] = new boolean[length];
        int inPos = -1;
        int outPos = 0;
        int count = 0;
        int strideCount = 0;

        do {
            do {
                inPos++;
                if(inPos >= 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;
    }
}



Offline Starfarer

Innocent Bystander




Java games rock!


« Reply #5 - Posted 2004-11-23 20: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.
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #6 - Posted 2004-11-23 23: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 Sad

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 Smiley

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

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #7 - Posted 2004-11-24 01: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.

Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


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

the stride is anywhere from 1 to 2^16 Wink



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.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!