aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimDecorator_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 >
92 
93  if (this != &from) {
94  if (content_ != nullptr) delete (content_); // should be the case
97  from.content_ = nullptr;
98  }
99 
100  return *this;
101  }
102 
103 
104  template < typename GUM_SCALAR >
106  MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
109 
112  from.content_ = nullptr;
113  }
114 
115 
116  template < typename GUM_SCALAR >
118  const MultiDimDecorator< GUM_SCALAR >& from) noexcept {
123  if (content_ == nullptr)
126  return *this;
127  }
128 
129 
130  // destructor
131 
132  template < typename GUM_SCALAR >
134  if (content_ != nullptr) { delete (content_); }
135 
137  }
138 
139  // return a data, given a Instantiation - final method
140 
141  template < typename GUM_SCALAR >
143  GUM_ERROR(OperationNotAllowed, "_get in the implementation !")
144  }
145 
146  template < typename GUM_SCALAR >
148  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
149  return empty_value_;
150  } else {
151  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->get(i);
152  }
153  }
154 
155  template < typename GUM_SCALAR >
157  const GUM_SCALAR& value) const {
158  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim() == 0) {
160  } else {
161  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->set(i, value);
162  }
163  }
164 
165  // get the size of domains - final method
166 
167  template < typename GUM_SCALAR >
169  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->domainSize();
170  }
171 
172  // add a new var to the sequence of vars - final method
173 
174  template < typename GUM_SCALAR >
176  if (v.domainSize() < 1) {
177  GUM_ERROR(InvalidArgument, "Empty variable " << v << " cannot be added in a Potential")
178  }
179  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->add(v);
180  }
181 
182  // listen to change in each recorded Instantiation. final method
183 
184  template < typename GUM_SCALAR >
186  const DiscreteVariable* const var,
187  Idx oldval,
188  Idx newval) {
190  var,
191  oldval,
192  newval);
193  }
194 
195  // listen to an assignment of a value in a Instantiation
196 
197  template < typename GUM_SCALAR >
200  }
201 
202  // listen to setFirst in each recorded Instantiation. final method.
203 
204  template < typename GUM_SCALAR >
207  }
208 
209  // listen to setLast in each recorded Instantiation. final method.
210 
211  template < typename GUM_SCALAR >
214  }
215 
216  // listen to increment in each recorded Instantiation. final method.
217 
218  template < typename GUM_SCALAR >
221  }
222 
223  // listen to increment in each recorded Instantiation. final method.
224 
225  template < typename GUM_SCALAR >
228  }
229 
230  // add a Instantiation as a slave of this
231 
232  template < typename GUM_SCALAR >
234  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->registerSlave(i);
235  }
236 
237  template < typename GUM_SCALAR >
239  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->erase(var);
240  }
241 
242  template < typename GUM_SCALAR >
244  erase(variable(name));
245  }
246 
247  template < typename GUM_SCALAR >
249  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(i);
250  }
251 
252  template < typename GUM_SCALAR >
253  INLINE const DiscreteVariable&
255  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(name);
256  }
257 
258  template < typename GUM_SCALAR >
260  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->pos(var);
261  }
262 
263  template < typename GUM_SCALAR >
265  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->contains(var);
266  }
267 
268  template < typename GUM_SCALAR >
270  if (content_ == nullptr) return true;
271  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty();
272  }
273 
274  template < typename GUM_SCALAR >
276  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->unregisterSlave(i);
277  }
278 
279  template < typename GUM_SCALAR >
281  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
282  empty_value_ = d;
283  } else {
284  static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->fill(d);
285  }
286  }
287 
288  // notification modification on vars to all Instantiation listeners.
289 
290  template < typename GUM_SCALAR >
292  /*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
293  GUM_ERROR(OperationNotAllowed, "Not implemented yet")
294  }
295 
296  // give a const ref to the sequence of DiscreteVariable*. final method.
297 
298  template < typename GUM_SCALAR >
299  INLINE const Sequence< const DiscreteVariable* >&
301  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variablesSequence();
302  }
303 
304  // get the nbr of vars in the sequence. final method
305 
306  template < typename GUM_SCALAR >
308  return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim();
309  }
310 
311  template < typename GUM_SCALAR >
313  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
314  if (v.size() == 1) {
315  empty_value_ = v[0];
316  } else {
317  GUM_ERROR(SizeError, "Size do not match in populate")
318  }
319  } else {
320  content_->populate(v);
321  }
322  }
323 
324  template < typename GUM_SCALAR >
326  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
328  } else {
329  content_->apply(f);
330  }
331  }
332 
333  template < typename GUM_SCALAR >
334  GUM_SCALAR
336  GUM_SCALAR base) const {
337  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
338  return base;
339  } else {
340  return content_->reduce(f, base);
341  }
342  }
343 
344  // protected access to content_
345  template < typename GUM_SCALAR >
347  return content_;
348  }
349 
350  // protected access to content_
351  template < typename GUM_SCALAR >
354  return content_;
355  }
356 
357  template < typename GUM_SCALAR >
360  }
361 
362  template < typename GUM_SCALAR >
365  }
366 
367  template < typename GUM_SCALAR >
370  }
371 
372  template < typename GUM_SCALAR >
375  if (aContent != nullptr) {
376  // TODO : frees all slave instantiations
377  // TODO : control the dimensions ?
379  content_ = aContent;
380  // registers all instantiations
381  delete (tmp);
382  }
383  }
384 
385 
386  // string representation of internal data about i in this.
387  template < typename GUM_SCALAR >
389  return content_->toString(i);
390  }
391 
392 
393  template < typename GUM_SCALAR >
395  const DiscreteVariable* y) {
396  this->content()->replace(*x, *y);
397  }
398 
399  template < typename GUM_SCALAR >
401  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
403  ss << "<> :: " << empty_value_;
404  return ss.str();
405  } else {
406  return content_->toString();
407  }
408  }
409 
410  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
411 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void _initPotentialOperators__()