dai::State Class Reference

Makes it easy to iterate over all possible joint states of variables within a VarSet. More...

#include <dai/index.h>

List of all members.

Public Types

typedef states_type::const_iterator const_iterator
 Constant iterator over the values.

Public Member Functions

 State ()
 Default constructor.
 State (const VarSet &vs, size_t linearState=0)
 Construct from VarSet vs and corresponding linear state linearState.
 State (const std::map< Var, size_t > &s)
 Construct from a std::map<Var, size_t>.
const_iterator begin () const
 Returns constant iterator that points to the first item.
const_iterator end () const
 Returns constant iterator that points beyond the last item.
 operator size_t () const
 Return current linear state.
template<typename InputIterator>
void insert (InputIterator b, InputIterator e)
 Inserts a range of variable-state pairs, changing the current state.
const std::map< Var, size_t > & get () const
 Return current state represented as a map.
 operator const std::map< Var, size_t > & () const
 Cast into std::map<Var, size_t>.
size_t operator() (const Var &v) const
 Return current state of variable v, or 0 if v is not in *this.
size_t operator() (const VarSet &vs) const
 Return linear state of variables in vs, assuming that variables that are not in *this are in state 0.
void operator++ ()
 Increments the current state (prefix).
void operator++ (int)
 Increments the current state (postfix).
bool valid () const
 Returns true if the current state is valid.
void reset ()
 Resets the current state (to the joint state represented by linear state 0).

Private Types

typedef std::map< Var, size_t > states_type
 Type for representing a joint state of some variables as a map, which maps each variable to its state.

Private Attributes

long state
 Current state (represented linearly).
states_type states
 Current state (represented as a map).


Detailed Description

Makes it easy to iterate over all possible joint states of variables within a VarSet.

A joint state of several variables can be represented in two different ways, by a map that maps each variable to its own state, or by an integer that gives the index of the joint state in the canonical enumeration.

Both representations are useful, and the main functionality provided by the State class is to simplify iterating over the various joint states of a VarSet and to provide access to the current state in both representations.

As an example, consider the following code snippet which iterates over all joint states of variables x0 and x1:

  VarSet vars( x0, x1 );
  for( State S(vars); S.valid(); S++ ) {
      cout << "Linear state: " << S.get() << ", x0 = " << S(x0) << ", x1 = " << S(x1) << endl;
  }

Note:
The same functionality could be achieved by simply iterating over the linear state and using dai::calcState(), but the State class offers a more efficient implementation.

A State is very similar to a dai::multifor, but tailored for Var 's and VarSet 's.

See also:
dai::calcLinearState(), dai::calcState()
Idea:
Make the State class a more prominent part of libDAI (and document it clearly, explaining the concept of state); add more optimized variants of the State class like IndexFor (e.g. for TFactor<>::slice()).
Examples:

example_permute.cpp, and example_varset.cpp.


Member Typedef Documentation

typedef std::map<Var, size_t> dai::State::states_type [private]

Type for representing a joint state of some variables as a map, which maps each variable to its state.

typedef states_type::const_iterator dai::State::const_iterator

Constant iterator over the values.


Constructor & Destructor Documentation

dai::State::State (  )  [inline]

Default constructor.

dai::State::State ( const VarSet vs,
size_t  linearState = 0 
) [inline]

Construct from VarSet vs and corresponding linear state linearState.

dai::State::State ( const std::map< Var, size_t > &  s  )  [inline]

Construct from a std::map<Var, size_t>.


Member Function Documentation

const_iterator dai::State::begin (  )  const [inline]

Returns constant iterator that points to the first item.

const_iterator dai::State::end (  )  const [inline]

Returns constant iterator that points beyond the last item.

dai::State::operator size_t (  )  const [inline]

Return current linear state.

template<typename InputIterator>
void dai::State::insert ( InputIterator  b,
InputIterator  e 
) [inline]

Inserts a range of variable-state pairs, changing the current state.

const std::map<Var,size_t>& dai::State::get (  )  const [inline]

Return current state represented as a map.

dai::State::operator const std::map< Var, size_t > & (  )  const [inline]

Cast into std::map<Var, size_t>.

size_t dai::State::operator() ( const Var v  )  const [inline]

Return current state of variable v, or 0 if v is not in *this.

size_t dai::State::operator() ( const VarSet vs  )  const [inline]

Return linear state of variables in vs, assuming that variables that are not in *this are in state 0.

void dai::State::operator++ (  )  [inline]

Increments the current state (prefix).

void dai::State::operator++ ( int   )  [inline]

Increments the current state (postfix).

bool dai::State::valid (  )  const [inline]

Returns true if the current state is valid.

void dai::State::reset (  )  [inline]

Resets the current state (to the joint state represented by linear state 0).


Member Data Documentation

long dai::State::state [private]

Current state (represented linearly).

Current state (represented as a map).


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