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