13 #ifndef __defined_libdai_index_h
14 #define __defined_libdai_index_h
70 _ranges.reserve( forVars.
size() );
71 _sum.reserve( forVars.
size() );
75 for( ; j != forVars.
end() && *j <= *i; ++j ) {
76 _ranges.push_back( j->states() );
77 _sum.push_back( (*i == *j) ? sum : 0 );
81 for( ; j != forVars.
end(); ++j ) {
82 _ranges.push_back( j->states() );
90 fill( _state.begin(), _state.end(), 0 );
96 operator size_t()
const {
106 while( i < _state.size() ) {
108 if( ++_state[i] < _ranges[i] )
110 _index -= _sum[i] * _ranges[i];
115 if( i == _state.size() )
128 return( _index >= 0 );
149 Permute(
const std::vector<size_t> &rs,
const std::vector<size_t> &
sigma ) : _ranges(rs), _sigma(sigma) {
150 DAI_ASSERT( _ranges.size() == _sigma.size() );
158 Permute(
const std::vector<Var> &vars,
bool reverse=
false ) : _ranges(), _sigma() {
159 size_t N = vars.size();
162 _ranges.reserve( N );
163 for(
size_t i = 0; i < N; ++i )
165 _ranges.push_back( vars[N - 1 - i].states() );
167 _ranges.push_back( vars[i].states() );
170 VarSet vs( vars.begin(), vars.end(), N );
176 size_t ind = find( vars.begin(), vars.end(), *vs_i ) - vars.begin();
178 _sigma.push_back( N - 1 - ind );
180 _sigma.push_back( ind );
189 size_t N = _ranges.size();
192 std::vector<size_t> vi;
195 for(
size_t k = 0; k < N; k++ ) {
196 vi.push_back( li % _ranges[k] );
204 for(
size_t k = 0; k < N; k++ ) {
205 sigma_li += vi[_sigma[k]] * prod;
206 prod *= _ranges[_sigma[k]];
232 size_t N = _ranges.size();
233 std::vector<size_t> invRanges( N, 0 );
234 std::vector<size_t> invSigma( N, 0 );
235 for(
size_t i = 0; i < N; i++ ) {
236 invSigma[_sigma[i]] = i;
237 invRanges[i] = _ranges[_sigma[i]];
239 return Permute( invRanges, invSigma );
274 multifor() : _ranges(), _indices(), _linear_index(0) {}
277 multifor(
const std::vector<size_t> &d ) : _ranges(d), _indices(d.size(),0), _linear_index(0) {}
280 operator size_t()
const {
282 return( _linear_index );
297 for( i = 0; i != _indices.size(); i++ ) {
298 if( ++(_indices[i]) < _ranges[i] )
302 if( i == _indices.size() )
315 fill( _indices.begin(), _indices.end(), 0 );
322 return( _linear_index >= 0 );
369 if( linearState == 0 )
375 linearState /= (
BigInt)v->states();
382 State(
const std::map<Var, size_t> &s ) : state(0), states() {
383 insert( s.begin(), s.end() );
390 const_iterator
begin()
const {
return states.begin(); }
393 const_iterator
end()
const {
return states.end(); }
396 operator size_t()
const {
402 template<
typename InputIterator>
403 void insert( InputIterator b, InputIterator e ) {
404 states.insert( b, e );
406 for( const_iterator it =
begin(); it !=
end(); it++ )
409 state = this->operator()( vars );
413 const std::map<Var,size_t>&
get()
const {
return states; }
416 operator const std::map<Var,size_t>& ()
const {
return states; }
420 states_type::const_iterator entry = states.find( v );
421 if( entry == states.end() )
424 return entry->second;
432 states_type::const_iterator entry = states.find( *v );
433 if( entry != states.end() )
434 vs_state += (
BigInt)entry->second * prod;
435 prod *= (
BigInt)v->states();
444 states_type::iterator entry = states.begin();
445 while( entry != states.end() ) {
446 if( ++(entry->second) < entry->first.states() )
451 if( entry == states.end() )
463 return( state >= 0 );
469 for( states_type::iterator s = states.begin(); s != states.end(); s++ )
multifor()
Default constructor.
Definition: index.h:274
size_t BigInt_size_t(const BigInt &N)
Safe down-cast of big integer to size_t.
Definition: util.h:104
BigInt state
Current state (represented linearly)
Definition: index.h:358
void insert(InputIterator b, InputIterator e)
Inserts a range of variable-state pairs, changing the current state.
Definition: index.h:403
const std::vector< size_t > & sigma() const
Returns constant reference to the permutation.
Definition: index.h:213
iterator end()
Returns iterator that points beyond the last element.
Definition: smallset.h:198
size_t operator[](size_t i) const
Returns the result of applying the permutation on i.
Definition: index.h:222
Permute()
Default constructor.
Definition: index.h:146
std::vector< size_t > _state
For each variable in forVars, the current state.
Definition: index.h:57
std::vector< size_t > & sigma()
Returns reference to the permutation.
Definition: index.h:216
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...
Definition: index.h:355
Permute(const std::vector< size_t > &rs, const std::vector< size_t > &sigma)
Construct from vector of index ranges and permutation.
Definition: index.h:149
std::vector< T >::size_type size() const
Returns number of elements.
Definition: smallset.h:169
iterator begin()
Returns iterator that points to the first element.
Definition: smallset.h:193
Defines the VarSet class, which represents a set of random variables.
State(const std::map< Var, size_t > &s)
Construct from a std::map
Definition: index.h:382
bool valid() const
Returns true if the current state is valid.
Definition: index.h:127
void operator++(int)
Increments the current indices (postfix)
Definition: index.h:309
multifor makes it easy to perform a dynamic number of nested for loops.
Definition: index.h:263
size_t convertLinearIndex(size_t li) const
Calculates a permuted linear index.
Definition: index.h:188
Makes it easy to iterate over all possible joint states of variables within a VarSet.
Definition: index.h:352
long _linear_index
Stores the current linear index.
Definition: index.h:270
size_t operator[](size_t k) const
Returns k 'th index.
Definition: index.h:286
IndexFor & reset()
Resets the state.
Definition: index.h:89
Tool for looping over the states of several variables.
Definition: index.h:48
Permute(const std::vector< Var > &vars, bool reverse=false)
Construct from vector of variables.
Definition: index.h:158
std::vector< size_t > _indices
Stores the current values of all indices.
Definition: index.h:268
const_iterator end() const
Returns constant iterator that points beyond the last item.
Definition: index.h:393
std::vector< size_t > _sigma
Stores the permutation.
Definition: index.h:142
mpz_class BigInt
Arbitrary precision integer number.
Definition: util.h:101
states_type::const_iterator const_iterator
Constant iterator over the values.
Definition: index.h:387
multifor(const std::vector< size_t > &d)
Initialize from vector of index ranges.
Definition: index.h:277
size_t operator()(const Var &v) const
Return current state of variable v, or 0 if v is not in *this.
Definition: index.h:419
states_type states
Current state (represented as a map)
Definition: index.h:361
IndexFor & operator++()
Increments the current state of forVars (prefix)
Definition: index.h:102
void operator++(int)
Increments the current state of forVars (postfix)
Definition: index.h:122
long _index
The current linear index corresponding to the state of indexVars.
Definition: index.h:51
bool valid() const
Returns true if the current state is valid.
Definition: index.h:462
std::vector< Var >::const_iterator const_iterator
Constant iterator over the elements.
Definition: smallset.h:182
State()
Default constructor.
Definition: index.h:365
std::vector< size_t > _ranges
For each variable in forVars, its number of possible values.
Definition: index.h:60
Represents a set of variables.
Definition: varset.h:94
IndexFor(const VarSet &indexVars, const VarSet &forVars)
Construct IndexFor object from indexVars and forVars.
Definition: index.h:67
multifor & operator++()
Increments the current indices (prefix)
Definition: index.h:293
State(const VarSet &vs, BigInt linearState=0)
Construct from VarSet vs and corresponding linear state linearState.
Definition: index.h:368
Permute inverse() const
Returns the inverse permutation.
Definition: index.h:231
std::vector< size_t > _ranges
Stores the number of possible values of all indices.
Definition: index.h:140
void operator++(int)
Increments the current state (postfix)
Definition: index.h:457
multifor & reset()
Resets the state.
Definition: index.h:314
void reset()
Resets the current state (to the joint state represented by linear state 0)
Definition: index.h:467
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
Tool for calculating permutations of linear indices of multi-dimensional arrays.
Definition: index.h:137
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
std::vector< long > _sum
For each variable in forVars, the amount of change in _index.
Definition: index.h:54
const_iterator begin() const
Returns constant iterator that points to the first item.
Definition: index.h:390
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
const std::vector< size_t > & ranges()
Returns constant reference to the dimensionality vector.
Definition: index.h:219
std::vector< size_t > _ranges
Stores the number of possible values of all indices.
Definition: index.h:266
bool valid() const
Returns true if the current indices are valid.
Definition: index.h:321
void operator++()
Increments the current state (prefix)
Definition: index.h:441
IndexFor()
Default constructor.
Definition: index.h:64