WDUK
WDUK

Reputation: 1524

Storing an array of references to arrays

I want to create an array of references to my arrays. The reason for this is because i want to optimise my fast Fourier transform algorithm to be branchless - or rather, less branchy.

The idea behind it is i have two arrays:

Array1 and Array2

I need to ping pong between the two in a for loop so i want to store the reference to the arrays like this:

[0] = Array1Ref
[1] = Array2Ref
[2] = Array1Ref
. . .

Is it possible to do this in C#? If so how would you define such an array - would i need to use unsafe ?

Upvotes: 0

Views: 66

Answers (1)

Sweeper
Sweeper

Reputation: 275075

If you just want to access a different array in each iteration of the for loop without using a conditional, you can keep swapping two variables and use one of them.

var arrayRef = Array1;
var theOtherArrayRef = Array2;
for (...) {
    // use arrayRef in places where you would have accessed the array of array references
    
    ...

    // C# 7 tuple syntax
    (arrayRef, theOtherArrayRef) = (theOtherArrayRef, arrayRef);

    // pre-C# 7:
    /*
    var temp = arrayRef;
    arrayRef = theOtherArrayRef;
    theOtherArrayRef = arrayRef;
    */
}

Upvotes: 1

Related Questions