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