libDAI
Public Member Functions | Private Attributes | List of all members
dai::multifor Class Reference

multifor makes it easy to perform a dynamic number of nested for loops. More...

#include <dai/index.h>

Public Member Functions

 multifor ()
 Default constructor. More...
 
 multifor (const std::vector< size_t > &d)
 Initialize from vector of index ranges. More...
 
 operator size_t () const
 Returns linear index (i.e., the index in the Cartesian product space) More...
 
size_t operator[] (size_t k) const
 Returns k 'th index. More...
 
multiforoperator++ ()
 Increments the current indices (prefix) More...
 
void operator++ (int)
 Increments the current indices (postfix) More...
 
multiforreset ()
 Resets the state. More...
 
bool valid () const
 Returns true if the current indices are valid. More...
 

Private Attributes

std::vector< size_t > _ranges
 Stores the number of possible values of all indices. More...
 
std::vector< size_t > _indices
 Stores the current values of all indices. More...
 
long _linear_index
 Stores the current linear index. More...
 

Detailed Description

multifor makes it easy to perform a dynamic number of nested for loops.

An example of the usage is as follows:

std::vector<size_t> ranges;
ranges.push_back( 3 );
ranges.push_back( 4 );
ranges.push_back( 5 );
for( multifor s(ranges); s.valid(); ++s )
cout << "linear index: " << (size_t)s << " corresponds to indices " << s[2] << ", " << s[1] << ", " << s[0] << endl;

which would be equivalent to:

size_t s = 0;
for( size_t s2 = 0; s2 < 5; s2++ )
for( size_t s1 = 0; s1 < 4; s1++ )
for( size_t s0 = 0; s0 < 3; s++, s0++ )
cout << "linear index: " << (size_t)s << " corresponds to indices " << s2 << ", " << s1 << ", " << s0 << endl;
Examples:
example_permute.cpp.

Constructor & Destructor Documentation

dai::multifor::multifor ( )
inline

Default constructor.

dai::multifor::multifor ( const std::vector< size_t > &  d)
inline

Initialize from vector of index ranges.

Member Function Documentation

dai::multifor::operator size_t ( ) const
inline

Returns linear index (i.e., the index in the Cartesian product space)

size_t dai::multifor::operator[] ( size_t  k) const
inline

Returns k 'th index.

multifor& dai::multifor::operator++ ( )
inline

Increments the current indices (prefix)

void dai::multifor::operator++ ( int  )
inline

Increments the current indices (postfix)

multifor& dai::multifor::reset ( )
inline

Resets the state.

bool dai::multifor::valid ( ) const
inline

Returns true if the current indices are valid.

Examples:
example_permute.cpp.

Member Data Documentation

std::vector<size_t> dai::multifor::_ranges
private

Stores the number of possible values of all indices.

std::vector<size_t> dai::multifor::_indices
private

Stores the current values of all indices.

long dai::multifor::_linear_index
private

Stores the current linear index.


The documentation for this class was generated from the following file: