aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
setInst.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #include <agrum/tools/multidim/implementations/multiDimAdressable.h>
23 #include <agrum/tools/multidim/setInst.h>
24 #include <sstream>
25 
26 #ifdef GUM_NO_INLINE
27 # include <agrum/tools/multidim/setInst_inl.h>
28 #endif
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace gum {
33 
34  void SetInst::init__(MultiDimAdressable* master) {
35  // for speed issues
36  const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
37  vars__.resize(v.size());
38  vals__.reserve(v.size());
39  // fill the SetInst
40 
41  for (const auto var: v)
42  add__(*var);
43 
44  // if ( master ) actAsSlave( master->getMasterRef() );
45  }
46 
47  // constructor for a SetInst contained into a MultiDimInterface
48 
49  SetInst::SetInst(MultiDimAdressable& d) : /*master__( 0 ),*/ overflow__(false) {
50  // for debugging purposes
51  GUM_CONSTRUCTOR(SetInst);
52  init__(&d);
53  }
54 
55  SetInst::SetInst(const MultiDimAdressable& d) :
56  /*master__( 0 ),*/ overflow__(false) {
57  // for debugging purposes
58  GUM_CONSTRUCTOR(SetInst);
59  init__(const_cast< MultiDimAdressable* >(&d));
60  }
61 
62  // constructor for a SetInst contained into a MultiDimInterface
63 
64  SetInst::SetInst(MultiDimAdressable* d) : /*master__( 0 ),*/ overflow__(false) {
65  // for debugging purposes
66  GUM_CONSTRUCTOR(SetInst);
67 
68  if (d) init__(d);
69  }
70 
71  // constructor for a SetInst contained into a MultiDimInterface
72  /** this constructor is needed in order to allow creation of SetInst(this)
73  * in MultiDimAdressable and below */
74 
75  SetInst::SetInst(const MultiDimAdressable* const_d) :
76  /*master__( 0 ),*/ overflow__(false) {
77  // for debugging purposes
78  GUM_CONSTRUCTOR(SetInst);
79 
80  if (const_d) init__(const_cast< MultiDimAdressable* >(const_d));
81  }
82 
83  // copy constructor
84 
85  SetInst::SetInst(const SetInst& aI) :
86  /* MultiDimInterface(), master__( 0 ),*/ overflow__(false) {
87  // for debugging purposes
88  GUM_CONS_CPY(SetInst);
89  // copy the content of aI
90  vars__ = aI.vars__;
91  vals__ = aI.vals__;
92  overflow__ = aI.overflow__;
93 
94  // if ( aI.master__ && notifyMaster ) actAsSlave( *aI.master__ );
95  }
96 
97  SetInst::SetInst(const Instantiation& aI) :
98  /* MultiDimInterface(), master__( 0 ),*/ overflow__(false) {
99  // for debugging purposes
100  GUM_CONS_CPY(SetInst);
101  const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
102  vars__.resize(v.size());
103  //__vals.reserve( v.size() );
104  // fill the SetInst
105 
106  for (const auto var: v) {
107  add__(*var);
108  vals__[vars__.pos(var)] = (Size(1) << (aI.val(*var)));
109  }
110  }
111 
112  // operator=
113  SetInst& SetInst::operator=(const SetInst& aI) {
114  // copy the content of aI
115  vars__ = aI.vars__;
116  vals__ = aI.vals__;
117  overflow__ = aI.overflow__;
118 
119  return *this;
120  }
121 
122  // Gives a string version of a SetInst
123  std::string SetInst::toString() const {
124  std::stringstream sstr;
125  // check if the value of the SetInst is correct
126 
127  if (overflow__) { sstr << "<invalid>"; }
128 
129  sstr << "<";
130 
131  Sequence< const DiscreteVariable* >::iterator_safe iter = vars__.begin();
132 
133  if (iter != vars__.end()) {
134  std::stringstream sstr2;
135  sstr2.str("");
136  Size si = variable(iter.pos()).domainSize();
137  Size valb = vals(iter.pos());
138 
139  while (si-- != 0) {
140  std::stringstream sstr4;
141  sstr4 << ((valb & 1) ? "1" : "0") << sstr2.str();
142  valb >>= 1;
143  sstr2.str("");
144  ;
145  sstr2 << sstr4.str();
146  }
147 
148  sstr << variable(iter.pos()).name() << ":" << sstr2.str();
149  ++iter;
150 
151  while (iter != vars__.end()) {
152  std::stringstream sstr3;
153  sstr3 << "";
154  si = variable(iter.pos()).domainSize();
155 
156  valb = vals(iter.pos());
157 
158  while (si-- != 0) {
159  std::stringstream sstr4;
160 
161  sstr4 << ((valb & 1) ? "1" : "0") << sstr3.str();
162  valb >>= 1;
163  sstr3.str("");
164  sstr3 << sstr4.str();
165  }
166 
167  sstr << "|" << variable(iter.pos()).name() << ":" << sstr3.str();
168  ++iter;
169  }
170  }
171 
172  sstr << ">";
173 
174  return sstr.str();
175  }
176 
177  // an operator for user-friendly displaying the content of a SetInst
178  std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
179  aStream << i.toString();
180  return aStream;
181  }
182  gum::SetInst& operator<<(gum::SetInst& inst, const gum::DiscreteVariable& i) {
183  inst.add(i);
184  return inst;
185  }
186  gum::SetInst& operator>>(gum::SetInst& inst, const gum::DiscreteVariable& i) {
187  inst.erase(i);
188  return inst;
189  }
190 
191  void gum::SetInst::assign_values(
192  gum::Bijection< const gum::DiscreteVariable*, const gum::DiscreteVariable* >&
193  bij,
194  const gum::SetInst& i,
195  gum::SetInst& j) {
196  try {
197  for (const auto var: i.variablesSequence())
198  j.chgVal(bij.second(var), i.val(var));
199  } catch (gum::NotFound&) {
200  GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst");
201  }
202  }
203 
204 } /* namespace gum */
205 
206 #endif // DOXYGEN_SHOULD_SKIP_THIS