Oops, correct previous partial commit.
[libdai.git] / include / dai / varset.h
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 /// \file
27 /// \brief Defines VarSet class
28 /// \todo Improve documentation
29
30
31 #ifndef __defined_libdai_varset_h
32 #define __defined_libdai_varset_h
33
34
35 #include <vector>
36 #include <map>
37 #include <ostream>
38 #include <dai/var.h>
39 #include <dai/util.h>
40 #include <dai/smallset.h>
41
42
43 namespace dai {
44
45
46 /// Represents a set of variables.
47 /** \note A VarSet is implemented using a std::vector<Var> instead
48 * of the more natural std::set<Var> because of efficiency reasons.
49 */
50 class VarSet : public smallSet<Var> {
51 public:
52 /// Default constructor
53 VarSet() : smallSet<Var>() {}
54
55 /// Copy constructor
56 VarSet( const VarSet &x ) : smallSet<Var>(x) {}
57
58 /// Assignment operator
59 VarSet& operator=( const VarSet &x ) {
60 if( this != &x ) {
61 smallSet<Var>::operator=( x );
62 }
63 return *this;
64 }
65
66 /// Construct from smallSet<Var>
67 VarSet( const smallSet<Var> &x ) : smallSet<Var>(x) {}
68
69 /// Calculates the product of the number of states of all variables in this VarSet.
70 size_t nrStates() {
71 size_t states = 1;
72 for( VarSet::const_iterator n = begin(); n != end(); n++ )
73 states *= n->states();
74 return states;
75 }
76
77 /// Construct a VarSet with one element
78 VarSet( const Var &n ) : smallSet<Var>(n) {}
79
80 /// Construct a VarSet with two elements
81 VarSet( const Var &n1, const Var &n2 ) : smallSet<Var>(n1,n2) {}
82
83 /// Construct a VarSet from a range of iterators.
84 /** \tparam VarIterator Iterator with value_type Var.
85 * \param begin Points to first Var to be added.
86 * \param end Points just beyond last Var to be added.
87 * \param sizeHint For efficiency, the number of elements can be speficied by sizeHint.
88 */
89 template <typename VarIterator>
90 VarSet( VarIterator begin, VarIterator end, size_t sizeHint=0 ) : smallSet<Var>(begin,end,sizeHint) {}
91
92 /// Calculates the linear index in the cartesian product of the variables in *this, which corresponds to a particular joint assignment of the variables.
93 /** \param states Specifies the states of some variables.
94 * \return The linear index in the cartesian product of the variables in *this
95 * corresponding with the joint assignment specified by \c states (where it is
96 * assumed that states[m] == 0 for all m in vars which are not in states).
97 */
98 size_t calcState( const std::map<Var, size_t> &states ) {
99 size_t prod = 1;
100 size_t state = 0;
101 for( VarSet::const_iterator n = begin(); n != end(); n++ ) {
102 std::map<Var, size_t>::const_iterator m = states.find( *n );
103 if( m != states.end() )
104 state += prod * m->second;
105 prod *= n->states();
106 }
107 return state;
108 }
109
110 /// Writes a VarSet to an output stream
111 friend std::ostream& operator<< (std::ostream &os, const VarSet& ns) {
112 for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
113 os << *n;
114 return( os );
115 }
116 };
117
118
119 } // end of namespace dai
120
121
122 #endif