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
5 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
6 Radboud University Nijmegen, The Netherlands
8 This file is part of libDAI.
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.
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.
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
27 /// \brief Defines VarSet class
28 /// \todo Improve documentation
31 #ifndef __defined_libdai_varset_h
32 #define __defined_libdai_varset_h
40 #include <dai/smallset.h>
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.
50 class VarSet
: public smallSet
<Var
> {
52 /// Default constructor
53 VarSet() : smallSet
<Var
>() {}
56 VarSet( const VarSet
&x
) : smallSet
<Var
>(x
) {}
58 /// Assignment operator
59 VarSet
& operator=( const VarSet
&x
) {
61 smallSet
<Var
>::operator=( x
);
66 /// Construct from smallSet<Var>
67 VarSet( const smallSet
<Var
> &x
) : smallSet
<Var
>(x
) {}
69 /// Calculates the product of the number of states of all variables in this VarSet.
72 for( VarSet::const_iterator n
= begin(); n
!= end(); n
++ )
73 states
*= n
->states();
77 /// Construct a VarSet with one element
78 VarSet( const Var
&n
) : smallSet
<Var
>(n
) {}
80 /// Construct a VarSet with two elements
81 VarSet( const Var
&n1
, const Var
&n2
) : smallSet
<Var
>(n1
,n2
) {}
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.
89 template <typename VarIterator
>
90 VarSet( VarIterator begin
, VarIterator end
, size_t sizeHint
=0 ) : smallSet
<Var
>(begin
,end
,sizeHint
) {}
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).
98 size_t calcState( const std::map
<Var
, size_t> &states
) {
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
;
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
++ )
119 } // end of namespace dai