aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimDecorator_tpl.h
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 /**
23  * @file
24  * @brief Implementation for MultiDimDecorator.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 // include the operators that will be used by the decorators
30 #include <agrum/tools/multidim/utils/operators/completeProjections4MultiDim.h>
31 #include <agrum/tools/multidim/utils/operators/operators4MultiDim.h>
32 #include <agrum/tools/multidim/utils/operators/projections4MultiDim.h>
33 #include <agrum/tools/multidim/utils/partialInstantiation4MultiDim.h>
34 
35 #include <agrum/tools/multidim/implementations/multiDimDecorator.h>
36 
37 namespace gum {
38  // instrumental and non-API function
39  template < typename GUM_SCALAR >
41  static bool first = true;
42 
43  if (first) {
44  first = false;
45 
46  // register the operators that will be used by the decorator
47  Operators4MultiDimInitialize< GUM_SCALAR > op;
48  op.init();
49 
50  // register the projectors that will be used by the decorator
51  Projections4MultiDimInitialize< GUM_SCALAR > proj;
52  proj.init();
53 
54  // register the projectors that will be used by the decorator
55  CompleteProjections4MultiDimInitialize< GUM_SCALAR > comp_proj;
56  comp_proj.init();
57 
58  // register the partial instantiators that will be used by the decorator
59  PartialInstantiation4MultiDimInitialize< GUM_SCALAR > inst;
60  inst.init();
61  }
62  }
63 
64  // constructors
65  template < typename GUM_SCALAR >
66  INLINE MultiDimDecorator< GUM_SCALAR >::MultiDimDecorator(
73  }
74 
75  template < typename GUM_SCALAR >
77  const MultiDimDecorator< GUM_SCALAR >& from) :
82  content()->copy(from.content());
83  }
84 
85 
86  template < typename GUM_SCALAR >
93 
94  if (this != &from) {
95  if (content_ != nullptr) delete (content_); // should be the case
98  from.content_ = nullptr;
99  }
100 
101  return *this;
102  }
103 
104 
105  template < typename GUM_SCALAR >
107  MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
111 
114  from.content_ = nullptr;
115  }
116 
117 
118  template < typename GUM_SCALAR >
121  const MultiDimDecorator< GUM_SCALAR >& from) noexcept {
126  if (content_ == nullptr)
127  content_ = static_cast< MultiDimImplementation< GUM_SCALAR >* >(
128  from.content()->newFactory());
130  return *this;
131  }
132 
133 
134  // destructor
135 
136  template < typename GUM_SCALAR >
138  if (content_ != nullptr) { delete (content_); }
139 
141  }
142 
143  // return a data, given a Instantiation - final method
144 
145  template < typename GUM_SCALAR >
148  GUM_ERROR(OperationNotAllowed, "_get in the implementation !");
149  }
150 
151  template < typename GUM_SCALAR >
154  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
155  return empty_value_;
156  } else {
157  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->get(i);
158  }
159  }
160 
161  template < typename GUM_SCALAR >
163  const GUM_SCALAR& value) const {
164  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim() == 0) {
166  } else {
167  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->set(i, value);
168  }
169  }
170 
171  // get the size of domains - final method
172 
173  template < typename GUM_SCALAR >
175  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->domainSize();
176  }
177 
178  // add a new var to the sequence of vars - final method
179 
180  template < typename GUM_SCALAR >
182  if (v.domainSize() < 1) {
184  "Empty variable " << v << " cannot be added in a Potential");
185  }
186  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->add(v);
187  }
188 
189  // listen to change in each recorded Instantiation. final method
190 
191  template < typename GUM_SCALAR >
193  const Instantiation& i,
194  const DiscreteVariable* const var,
195  Idx oldval,
196  Idx newval) {
197  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
199  }
200 
201  // listen to an assignment of a value in a Instantiation
202 
203  template < typename GUM_SCALAR >
205  const Instantiation& i) {
206  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
208  }
209 
210  // listen to setFirst in each recorded Instantiation. final method.
211 
212  template < typename GUM_SCALAR >
214  const Instantiation& i) {
215  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
217  }
218 
219  // listen to setLast in each recorded Instantiation. final method.
220 
221  template < typename GUM_SCALAR >
222  INLINE void
225  i);
226  }
227 
228  // listen to increment in each recorded Instantiation. final method.
229 
230  template < typename GUM_SCALAR >
231  INLINE void
234  i);
235  }
236 
237  // listen to increment in each recorded Instantiation. final method.
238 
239  template < typename GUM_SCALAR >
240  INLINE void
243  i);
244  }
245 
246  // add a Instantiation as a slave of this
247 
248  template < typename GUM_SCALAR >
250  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
251  ->registerSlave(i);
252  }
253 
254  template < typename GUM_SCALAR >
256  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->erase(var);
257  }
258 
259  template < typename GUM_SCALAR >
261  erase(variable(name));
262  }
263 
264  template < typename GUM_SCALAR >
265  INLINE const DiscreteVariable&
267  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(i);
268  }
269 
270  template < typename GUM_SCALAR >
271  INLINE const DiscreteVariable&
273  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(
274  name);
275  }
276 
277  template < typename GUM_SCALAR >
278  INLINE Idx
280  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->pos(var);
281  }
282 
283  template < typename GUM_SCALAR >
284  INLINE bool
286  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->contains(
287  var);
288  }
289 
290  template < typename GUM_SCALAR >
292  if (content_ == nullptr) return true;
293  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty();
294  }
295 
296  template < typename GUM_SCALAR >
298  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
299  ->unregisterSlave(i);
300  }
301 
302  template < typename GUM_SCALAR >
304  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
305  empty_value_ = d;
306  } else {
307  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->fill(d);
308  }
309  }
310 
311  // notification modification on vars to all Instantiation listeners.
312 
313  template < typename GUM_SCALAR >
315  /*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
316  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
317  }
318 
319  // give a const ref to the sequence of DiscreteVariable*. final method.
320 
321  template < typename GUM_SCALAR >
322  INLINE const Sequence< const DiscreteVariable* >&
324  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
325  ->variablesSequence();
326  }
327 
328  // get the nbr of vars in the sequence. final method
329 
330  template < typename GUM_SCALAR >
332  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim();
333  }
334 
335  template < typename GUM_SCALAR >
337  const std::vector< GUM_SCALAR >& v) const {
338  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
339  if (v.size() == 1) {
340  empty_value_ = v[0];
341  } else {
342  GUM_ERROR(SizeError, "Size do not match in populate")
343  }
344  } else {
345  content_->populate(v);
346  }
347  }
348 
349  template < typename GUM_SCALAR >
351  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
352  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
354  } else {
355  content_->apply(f);
356  }
357  }
358 
359  template < typename GUM_SCALAR >
362  GUM_SCALAR base) const {
363  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
364  return base;
365  } else {
366  return content_->reduce(f, base);
367  }
368  }
369 
370  // protected access to content_
371  template < typename GUM_SCALAR >
374  return content_;
375  }
376 
377  // protected access to content_
378  template < typename GUM_SCALAR >
381  return content_;
382  }
383 
384  template < typename GUM_SCALAR >
386  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
388  }
389 
390  template < typename GUM_SCALAR >
392  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)
393  ->endMultipleChanges();
394  }
395 
396  template < typename GUM_SCALAR >
397  INLINE void
400  x);
401  }
402 
403  template < typename GUM_SCALAR >
406  if (aContent != nullptr) {
407  // TODO : frees all slave instantiations
408  // TODO : control the dimensions ?
410  content_ = aContent;
411  // registers all instantiations
412  delete (tmp);
413  }
414  }
415 
416 
417  // string representation of internal data about i in this.
418  template < typename GUM_SCALAR >
419  INLINE std::string
421  return content_->toString(i);
422  }
423 
424 
425  template < typename GUM_SCALAR >
426  INLINE void
428  const DiscreteVariable* y) {
429  this->content()->replace(*x, *y);
430  }
431 
432  template < typename GUM_SCALAR >
434  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
436  ss << "<> :: " << empty_value_;
437  return ss.str();
438  } else {
439  return content_->toString();
440  }
441  }
442 
443  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
444 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void initPotentialOperators___()