libDAI
varset.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_varset_h
14 #define __defined_libdai_varset_h
15 
16 
17 #include <vector>
18 #include <map>
19 #include <ostream>
20 #include <dai/var.h>
21 #include <dai/util.h>
22 #include <dai/smallset.h>
23 
24 
25 namespace dai {
26 
27 
28 // Predefine for definitions of calcLinearState() and calcState()
29 class VarSet;
30 
31 
33 
60 size_t calcLinearState( const VarSet &vs, const std::map<Var, size_t> &state );
61 
62 
64 
85 std::map<Var, size_t> calcState( const VarSet &vs, size_t linearState );
86 
87 
89 
94 class VarSet : public SmallSet<Var> {
95  public:
97 
98  VarSet() : SmallSet<Var>() {}
100 
102  VarSet( const SmallSet<Var> &x ) : SmallSet<Var>(x) {}
103 
105  VarSet( const Var &v ) : SmallSet<Var>(v) {}
106 
108  VarSet( const Var &v1, const Var &v2 ) : SmallSet<Var>(v1,v2) {}
109 
111 
116  template <typename VarIterator>
117  VarSet( VarIterator begin, VarIterator end, size_t sizeHint=0 ) : SmallSet<Var>(begin,end,sizeHint) {}
119 
121 
122 
130  BigInt nrStates() const {
131  BigInt states = 1;
132  for( VarSet::const_iterator n = begin(); n != end(); n++ )
133  states *= (BigInt)n->states();
134  return states;
135  }
137 
139 
140  friend std::ostream& operator<<( std::ostream &os, const VarSet &vs ) {
142  os << "{";
143  for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ )
144  os << (v != vs.begin() ? ", " : "") << *v;
145  os << "}";
146  return( os );
147  }
148 
150  std::string toString() const {
151  std::stringstream ss;
152  ss << *this;
153  return ss.str();
154  }
156 };
157 
158 
159 } // end of namespace dai
160 
161 
172 #endif
std::string toString() const
Formats a VarSet as a string.
Definition: varset.h:150
VarSet()
Default constructor (constructs an empty set)
Definition: varset.h:99
size_t calcLinearState(const VarSet &vs, const std::map< Var, size_t > &state)
Calculates the linear index in the Cartesian product of the variables in vs that corresponds to a par...
Definition: varset.cpp:18
iterator end()
Returns iterator that points beyond the last element.
Definition: smallset.h:198
BigInt nrStates() const
Calculates the number of states of this VarSet, which is simply the number of possible joint states o...
Definition: varset.h:130
Defines class Var, which represents a discrete random variable.
iterator begin()
Returns iterator that points to the first element.
Definition: smallset.h:193
mpz_class BigInt
Arbitrary precision integer number.
Definition: util.h:101
std::vector< Var >::const_iterator const_iterator
Constant iterator over the elements.
Definition: smallset.h:182
VarSet(VarIterator begin, VarIterator end, size_t sizeHint=0)
Construct a VarSet from the range between begin and end.
Definition: varset.h:117
Represents a set of variables.
Definition: varset.h:94
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
VarSet(const Var &v)
Construct a VarSet with one element, v.
Definition: varset.h:105
Defines the SmallSet<> class, which represents a set (optimized for a small number of elements)...
VarSet(const SmallSet< Var > &x)
Construct from SmallSet x.
Definition: varset.h:102
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
std::map< Var, size_t > calcState(const VarSet &vs, size_t linearState)
Calculates the joint assignment of the variables in vs corresponding to the linear index linearState...
Definition: varset.cpp:31
Represents a set; the implementation is optimized for a small number of elements. ...
Definition: smallset.h:32
VarSet(const Var &v1, const Var &v2)
Construct a VarSet with two elements, v1 and v2.
Definition: varset.h:108
friend std::ostream & operator<<(std::ostream &os, const VarSet &vs)
Writes a VarSet to an output stream.
Definition: varset.h:141