aGrUM  0.14.2
setInst.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Ariele-Paolo MAESANO *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
21 #include <agrum/multidim/setInst.h>
22 #include <sstream>
23 
24 #ifdef GUM_NO_INLINE
26 #endif
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  void SetInst::__init(MultiDimAdressable* master) {
33  // for speed issues
34  const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
35  __vars.resize(v.size());
36  __vals.reserve(v.size());
37  // fill the SetInst
38 
39  for (const auto var : v)
40  __add(*var);
41 
42  // if ( master ) actAsSlave( master->getMasterRef() );
43  }
44 
45  // constructor for a SetInst contained into a MultiDimInterface
46 
47  SetInst::SetInst(MultiDimAdressable& d) : /*__master( 0 ),*/ __overflow(false) {
48  // for debugging purposes
49  GUM_CONSTRUCTOR(SetInst);
50  __init(&d);
51  }
52 
54  /*__master( 0 ),*/ __overflow(false) {
55  // for debugging purposes
56  GUM_CONSTRUCTOR(SetInst);
57  __init(const_cast< MultiDimAdressable* >(&d));
58  }
59 
60  // constructor for a SetInst contained into a MultiDimInterface
61 
62  SetInst::SetInst(MultiDimAdressable* d) : /*__master( 0 ),*/ __overflow(false) {
63  // for debugging purposes
64  GUM_CONSTRUCTOR(SetInst);
65 
66  if (d) __init(d);
67  }
68 
69  // constructor for a SetInst contained into a MultiDimInterface
73  SetInst::SetInst(const MultiDimAdressable* const_d) :
74  /*__master( 0 ),*/ __overflow(false) {
75  // for debugging purposes
76  GUM_CONSTRUCTOR(SetInst);
77 
78  if (const_d) __init(const_cast< MultiDimAdressable* >(const_d));
79  }
80 
81  // copy constructor
82 
83  SetInst::SetInst(const SetInst& aI) :
84  /* MultiDimInterface(), __master( 0 ),*/ __overflow(false) {
85  // for debugging purposes
86  GUM_CONS_CPY(SetInst);
87  // copy the content of aI
88  __vars = aI.__vars;
89  __vals = aI.__vals;
90  __overflow = aI.__overflow;
91 
92  // if ( aI.__master && notifyMaster ) actAsSlave( *aI.__master );
93  }
94 
95  SetInst::SetInst(const Instantiation& aI) :
96  /* MultiDimInterface(), __master( 0 ),*/ __overflow(false) {
97  // for debugging purposes
98  GUM_CONS_CPY(SetInst);
99  const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
100  __vars.resize(v.size());
101  //__vals.reserve( v.size() );
102  // fill the SetInst
103 
104  for (const auto var : v) {
105  __add(*var);
106  __vals[__vars.pos(var)] = (Size(1) << (aI.val(*var)));
107  }
108  }
109 
110  // operator=
111  SetInst& SetInst::operator=(const SetInst& aI) {
112  // copy the content of aI
113  __vars = aI.__vars;
114  __vals = aI.__vals;
115  __overflow = aI.__overflow;
116 
117  return *this;
118  }
119 
120  // Gives a string version of a SetInst
121  std::string SetInst::toString() const {
122  std::stringstream sstr;
123  // check if the value of the SetInst is correct
124 
125  if (__overflow) { sstr << "<invalid>"; }
126 
127  sstr << "<";
128 
130 
131  if (iter != __vars.end()) {
132  std::stringstream sstr2;
133  sstr2.str("");
134  Size si = variable(iter.pos()).domainSize();
135  Size valb = vals(iter.pos());
136 
137  while (si-- != 0) {
138  std::stringstream sstr4;
139  sstr4 << ((valb & 1) ? "1" : "0") << sstr2.str();
140  valb >>= 1;
141  sstr2.str("");
142  ;
143  sstr2 << sstr4.str();
144  }
145 
146  sstr << variable(iter.pos()).name() << ":" << sstr2.str();
147  ++iter;
148 
149  while (iter != __vars.end()) {
150  std::stringstream sstr3;
151  sstr3 << "";
152  si = variable(iter.pos()).domainSize();
153 
154  valb = vals(iter.pos());
155 
156  while (si-- != 0) {
157  std::stringstream sstr4;
158 
159  sstr4 << ((valb & 1) ? "1" : "0") << sstr3.str();
160  valb >>= 1;
161  sstr3.str("");
162  sstr3 << sstr4.str();
163  }
164 
165  sstr << "|" << variable(iter.pos()).name() << ":" << sstr3.str();
166  ++iter;
167  }
168  }
169 
170  sstr << ">";
171 
172  return sstr.str();
173  }
174 
175  // an operator for user-friendly displaying the content of a SetInst
176  std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
177  aStream << i.toString();
178  return aStream;
179  }
181  inst.add(i);
182  return inst;
183  }
185  inst.erase(i);
186  return inst;
187  }
188 
191  bij,
192  const gum::SetInst& i,
193  gum::SetInst& j) {
194  try {
195  for (const auto var : i.variablesSequence())
196  j.chgVal(bij.second(var), i.val(var));
197  } catch (gum::NotFound&) {
198  GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst");
199  }
200  }
201 
202 } /* namespace gum */
203 
204 #endif // DOXYGEN_SHOULD_SKIP_THIS
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
virtual Size domainSize() const final
Returns the product of the variables domain size.
static void assign_values(Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const SetInst &i, SetInst &j)
Assign the values of i in j, using bij as a bijection between i and j variables.
SetInst & chgVal(const DiscreteVariable &v, Idx newVal)
Assign newVal to variable v in the SetInst.
void add(const DiscreteVariable &v)
Adds a new variable in the SetInst.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::string toString() const
Give a string version of SetInst.
const Sequence< const DiscreteVariable *> & variablesSequence() const
Returns the sequence of DiscreteVariable of this SetInst.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
Class for assigning/browsing values to tuples of discrete variables.
Definition: setInst.h:79
void __add(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition: sequence.h:1034
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
std::vector< Size > __vals
The current SetInst: the value of the tuple.
Definition: setInst.h:846
bool __overflow
The overflow flag.
Definition: setInst.h:849
Sequence< const DiscreteVariable *> __vars
The tuple of variables to be instantiated.
Definition: setInst.h:843
void erase(const DiscreteVariable &v)
Removes a variable from the SetInst.
MultiDimAdressable()
Default constructor.
SetInst()
Default constructor: creates an empty tuple.
Idx val(Idx i) const
Returns the current value of a variable at a given position.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void __init(MultiDimAdressable *master)
Intialize the SetInst.
MultiDimInterface & operator>>(MultiDimInterface &c, const DiscreteVariable &v)
Removes a var from the variables of the MutliDimAdressing.
SetInst & operator=(const SetInst &aI)
Copy operator.
Headers for the abstract base class for all multi dimensionnal containers.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
Headers of SetInst.