Updated copyrights
[libdai.git] / src / varset.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
6 Radboud University Nijmegen, The Netherlands
7
8 This file is part of libDAI.
9
10 libDAI is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 libDAI is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with libDAI; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 #include <dai/varset.h>
27
28
29 namespace dai {
30
31
32 using namespace std;
33
34
35 /// Calculates the product of number of states of all variables in vars
36 size_t nrStates( const VarSet &vars ) {
37 size_t states = 1;
38 for( VarSet::const_iterator n = vars.begin(); n != vars.end(); n++ )
39 states *= n->states();
40 return states;
41 }
42
43
44 /// calcState calculates the linear index of vars that corresponds
45 /// to the states of the variables given in states, implicitly assuming
46 /// states[m] = 0 for all m in this VarSet which are not in states.
47 size_t calcState( const VarSet &vars, const std::map<Var, size_t> &states ) {
48 size_t prod = 1;
49 size_t state = 0;
50 for( VarSet::const_iterator n = vars.begin(); n != vars.end(); n++ ) {
51 map<Var, size_t>::const_iterator m = states.find( *n );
52 if( m != states.end() )
53 state += prod * m->second;
54 prod *= n->states();
55 }
56 return state;
57 }
58
59
60 /// Sends a VarSet to an output stream
61 std::ostream& operator<< (std::ostream &os, const VarSet& ns) {
62 for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
63 os << *n;
64 return( os );
65 }
66
67
68 } // end of namespace dai