TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::MultiCounter< DataType > Class Template Reference

Helper class for flattening nested loops. More...

#include <MultiCounter.h>

Public Member Functions

 MultiCounter (const std::vector< DataType > &begin, const std::vector< DataType > &end, const std::vector< DataType > &increment)
 
MultiCounteroperator++ ()
 
const DataType operator[] (unsigned int n) const
 
 operator std::vector< DataType > () const
 
void reset ()
 
bool done () const
 
unsigned int getSize () const
 

Detailed Description

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"
#include "TBTK/Streams.h"
#include "TBTK/TBTK.h"
using namespace std;
using namespace TBTK;
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

Constructor & Destructor Documentation

◆ MultiCounter()

template<typename DataType >
TBTK::MultiCounter< DataType >::MultiCounter ( const std::vector< DataType > &  begin,
const std::vector< DataType > &  end,
const std::vector< DataType > &  increment 
)
inline

Constructor.

Parameters
beginThe start values for the counters.
endThe end of the counters. This value is not included in the range.
incrementThe increment size of the counter.

Member Function Documentation

◆ done()

template<typename DataType >
bool TBTK::MultiCounter< DataType >::done
inline

Check if the counter has reached the end.

Returns
True if the counter has reached the end, otherwise false.

◆ getSize()

template<typename DataType >
unsigned int TBTK::MultiCounter< DataType >::getSize
inline

Get the number of counters.

Returns
The number of counters.

◆ operator++()

template<typename DataType >
MultiCounter< DataType > & TBTK::MultiCounter< DataType >::operator++
inline

Increment operator.

Returns
A reference to the MultiCounter after the increment has occured.

◆ operator[]()

template<typename DataType >
const DataType TBTK::MultiCounter< DataType >::operator[] ( unsigned int  n) const
inline

Array subscript operator. Returns the current valu of the nth counter.

Parameters
nThe counter to get the value for.
Returns
The value of the nth counter.

◆ reset()

template<typename DataType >
void TBTK::MultiCounter< DataType >::reset
inline

Reset the counter.


The documentation for this class was generated from the following file:
Streams.h
Streams for TBTK output.
TBTK::MultiCounter
Helper class for flattening nested loops.
Definition: MultiCounter.h:42
TBTK::Initialize
void Initialize()
TBTK::Streams::out
static std::ostream out
Definition: Streams.h:70
TBTK.h
TBTK initialization.