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

Tool for looping over the states of several variables. More...

#include <dai/index.h>

Public Member Functions

 IndexFor ()
 Default constructor. More...
 
 IndexFor (const VarSet &indexVars, const VarSet &forVars)
 Construct IndexFor object from indexVars and forVars. More...
 
IndexForreset ()
 Resets the state. More...
 
 operator size_t () const
 Conversion to size_t: returns linear index of the current state of indexVars. More...
 
IndexForoperator++ ()
 Increments the current state of forVars (prefix) More...
 
void operator++ (int)
 Increments the current state of forVars (postfix) More...
 
bool valid () const
 Returns true if the current state is valid. More...
 

Private Attributes

long _index
 The current linear index corresponding to the state of indexVars. More...
 
std::vector< long > _sum
 For each variable in forVars, the amount of change in _index. More...
 
std::vector< size_t > _state
 For each variable in forVars, the current state. More...
 
std::vector< size_t > _ranges
 For each variable in forVars, its number of possible values. More...
 

Detailed Description

Tool for looping over the states of several variables.

The class IndexFor is an important tool for indexing Factor entries. Its usage can best be explained by an example. Assume indexVars, forVars are both VarSet 's. Then the following code:

IndexFor i( indexVars, forVars );
size_t iter = 0;
for( ; i.valid(); i++, iter++ ) {
cout << "State of forVars: " << calcState( forVars, iter ) << "; ";
cout << "state of indexVars: " << calcState( indexVars, size_t(i) ) << endl;
}

loops over all joint states of the variables in forVars, and (size_t)i equals the linear index of the corresponding state of indexVars, where the variables in indexVars that are not in forVars assume their zero'th value.

Idea:
Optimize all indices as follows: keep a cache of all (or only relatively small) indices that have been computed (use a hash). Then, instead of computing on the fly, use the precomputed ones. Here the labels of the variables don't matter, but the ranges of the variables do.

Constructor & Destructor Documentation

dai::IndexFor::IndexFor ( )
inline

Default constructor.

dai::IndexFor::IndexFor ( const VarSet indexVars,
const VarSet forVars 
)
inline

Construct IndexFor object from indexVars and forVars.

Member Function Documentation

IndexFor& dai::IndexFor::reset ( )
inline

Resets the state.

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

Conversion to size_t: returns linear index of the current state of indexVars.

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

Increments the current state of forVars (prefix)

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

Increments the current state of forVars (postfix)

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

Returns true if the current state is valid.

Member Data Documentation

long dai::IndexFor::_index
private

The current linear index corresponding to the state of indexVars.

std::vector<long> dai::IndexFor::_sum
private

For each variable in forVars, the amount of change in _index.

std::vector<size_t> dai::IndexFor::_state
private

For each variable in forVars, the current state.

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

For each variable in forVars, its number of possible values.


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