template<typename DataType>
class TBTK::MultiCounter< DataType >
Helper class for flattening nested loops.
The MultiCounter allows for multiple loop variables to be looped over using a single loop. It can be used to flatten deeply nested loops.
Example
#include "TBTK/MultiCounter.h"
using namespace std;
int main(){
Streams::out << "Three level nested loop:\n";
for(unsigned int x = 0; x < 3; x++){
for(unsigned int y = 1; y < 7; y += 2){
for(unsigned int z = 2; z < 8; z += 3){
Streams::out << x << "\t";
Streams::out << y << "\t";
Streams::out << z << "\n";
}
}
}
Streams::out << "\nEquivalent flattened loop:\n";
{0, 1, 2},
{3, 7, 8},
{1, 2, 3}
);
for(multiCounter.reset(); !multiCounter.done(); ++multiCounter){
unsigned int x = multiCounter[0];
unsigned int y = multiCounter[1];
unsigned int z = multiCounter[2];
Streams::out << x << "\t";
Streams::out << y << "\t";
Streams::out << z << "\n";
}
}
Output
Three level nested loop:
0 1 2
0 1 5
0 3 2
0 3 5
0 5 2
0 5 5
1 1 2
1 1 5
1 3 2
1 3 5
1 5 2
1 5 5
2 1 2
2 1 5
2 3 2
2 3 5
2 5 2
2 5 5
Equivalent flattened loop:
0 1 2
0 1 5
0 3 2
0 3 5
0 5 2
0 5 5
1 1 2
1 1 5
1 3 2
1 3 5
1 5 2
1 5 5
2 1 2
2 1 5
2 3 2
2 3 5
2 5 2
2 5 5