dai::multifor Class Reference

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

#include <dai/index.h>

List of all members.

Public Member Functions

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

Private Attributes

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


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.


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.

Stores the current linear index.


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

Generated on Thu Aug 5 19:04:06 2010 for libDAI by  doxygen 1.5.5