New git HEAD version
[libdai.git] / src / varset.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/varset.h>
10
11
12 namespace dai {
13
14
15 using namespace std;
16
17
18 size_t calcLinearState( const VarSet &vs, const std::map<Var, size_t> &state ) {
19 size_t prod = 1;
20 size_t st = 0;
21 for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ ) {
22 std::map<Var, size_t>::const_iterator m = state.find( *v );
23 if( m != state.end() )
24 st += prod * m->second;
25 prod *= v->states();
26 }
27 return st;
28 }
29
30
31 std::map<Var, size_t> calcState( const VarSet &vs, size_t linearState ) {
32 std::map<Var, size_t> state;
33 for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ ) {
34 state[*v] = linearState % v->states();
35 linearState /= v->states();
36 }
37 DAI_ASSERT( linearState == 0 );
38 return state;
39 }
40
41
42 } // end of namespace dai