iterate over all items of array with const stride

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?

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


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

[quote]I’m not quite sure what you’re trying to do but is this it?


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
[/quote]
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:



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…

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

Would this do the trick:

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;
    }
}

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.

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 :frowning:

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 :slight_smile:

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

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.

the stride is anywhere from 1 to 2^16 :wink: