Miscellaneous smaller improvements
[libdai.git] / src / varset.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2002-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <dai/varset.h>
13
14
15 namespace dai {
16
17
18 using namespace std;
19
20
21 size_t calcLinearState( const VarSet &vs, const std::map<Var, size_t> &state ) {
22 size_t prod = 1;
23 size_t st = 0;
24 for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ ) {
25 std::map<Var, size_t>::const_iterator m = state.find( *v );
26 if( m != state.end() )
27 st += prod * m->second;
28 prod *= v->states();
29 }
30 return st;
31 }
32
33
34 std::map<Var, size_t> calcState( const VarSet &vs, size_t linearState ) {
35 std::map<Var, size_t> state;
36 for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ ) {
37 state[*v] = linearState % v->states();
38 linearState /= v->states();
39 }
40 DAI_ASSERT( linearState == 0 );
41 return state;
42 }
43
44
45 } // end of namespace dai