aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
multiDimDecorator_tpl.h
Go to the documentation of this file.
1 
30 // include the operators that will be used by the decorators
35 
37 
38 namespace gum {
39  // instrumental and non-API function
40  template < typename GUM_SCALAR >
42  static bool first = true;
43 
44  if (first) {
45  first = false;
46 
47  // register the operators that will be used by the decorator
49  op.init();
50 
51  // register the projectors that will be used by the decorator
53  proj.init();
54 
55  // register the projectors that will be used by the decorator
57  comp_proj.init();
58 
59  // register the partial instantiators that will be used by the decorator
61  inst.init();
62  }
63  }
64 
65  // constructors
66  template < typename GUM_SCALAR >
68  MultiDimImplementation< GUM_SCALAR >* aContent, GUM_SCALAR empty_value) :
69  _content(aContent),
70  _empty_value(empty_value) {
71  ___initPotentialOperators< GUM_SCALAR >();
72  GUM_CONSTRUCTOR(MultiDimDecorator);
73  }
74 
75  template < typename GUM_SCALAR >
77  const MultiDimDecorator< GUM_SCALAR >& from) :
78  MultiDimContainer< GUM_SCALAR >(from) {
79  GUM_CONS_CPY(MultiDimDecorator);
80  ___initPotentialOperators< GUM_SCALAR >();
82  content()->copy(from.content());
83  }
84 
85 
86  template < typename GUM_SCALAR >
91  std::forward< MultiDimContainer< GUM_SCALAR > >(from));
92  GUM_OP_MOV(MultiDimDecorator);
93 
94  if (this != &from) {
95  if (_content != nullptr) delete (_content); // should be the case
96  _empty_value = from._empty_value;
97  _content = from._content;
98  from._content = nullptr;
99  }
100 
101  return *this;
102  }
103 
104 
105  template < typename GUM_SCALAR >
107  MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
109  std::forward< MultiDimContainer< GUM_SCALAR > >(from)) {
110  GUM_CONS_MOV(MultiDimDecorator);
111 
112  _empty_value = from._empty_value;
113  _content = from._content;
114  from._content = nullptr;
115  }
116 
117 
118  template < typename GUM_SCALAR >
121  const MultiDimDecorator< GUM_SCALAR >& from) noexcept {
122  GUM_OP_CPY(MultiDimDecorator);
123  ___initPotentialOperators< GUM_SCALAR >();
125  _empty_value = from._empty_value;
126  MultiDimDecorator< GUM_SCALAR >::content()->copy(*from.content());
127  return *this;
128  }
129 
130 
131  // destructor
132 
133  template < typename GUM_SCALAR >
135  if (_content != nullptr) { delete (_content); }
136 
137  GUM_DESTRUCTOR(MultiDimDecorator);
138  }
139 
140  // return a data, given a Instantiation - final method
141 
142  template < typename GUM_SCALAR >
143  INLINE GUM_SCALAR&
145  GUM_ERROR(OperationNotAllowed, "_get in the implementation !");
146  }
147 
148  template < typename GUM_SCALAR >
149  INLINE GUM_SCALAR
151  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
152  return _empty_value;
153  } else {
154  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->get(i);
155  }
156  }
157 
158  template < typename GUM_SCALAR >
160  const GUM_SCALAR& value) const {
161  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim() == 0) {
162  _empty_value = value;
163  } else {
164  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->set(i, value);
165  }
166  }
167 
168  // get the size of domains - final method
169 
170  template < typename GUM_SCALAR >
172  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->domainSize();
173  }
174 
175  // add a new var to the sequence of vars - final method
176 
177  template < typename GUM_SCALAR >
179  if (v.domainSize() < 1) {
181  "Empty variable " << v << " cannot be added in a Potential");
182  }
183  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->add(v);
184  }
185 
186  // listen to change in each recorded Instantiation. final method
187 
188  template < typename GUM_SCALAR >
190  const Instantiation& i,
191  const DiscreteVariable* const var,
192  Idx oldval,
193  Idx newval) {
195  i, var, oldval, newval);
196  }
197 
198  // listen to an assignment of a value in a Instantiation
199 
200  template < typename GUM_SCALAR >
202  const Instantiation& i) {
205  }
206 
207  // listen to setFirst in each recorded Instantiation. final method.
208 
209  template < typename GUM_SCALAR >
211  const Instantiation& i) {
214  }
215 
216  // listen to setLast in each recorded Instantiation. final method.
217 
218  template < typename GUM_SCALAR >
219  INLINE void
222  i);
223  }
224 
225  // listen to increment in each recorded Instantiation. final method.
226 
227  template < typename GUM_SCALAR >
228  INLINE void
231  i);
232  }
233 
234  // listen to increment in each recorded Instantiation. final method.
235 
236  template < typename GUM_SCALAR >
237  INLINE void
240  i);
241  }
242 
243  // add a Instantiation as a slave of this
244 
245  template < typename GUM_SCALAR >
247  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
248  ->registerSlave(i);
249  }
250 
251  template < typename GUM_SCALAR >
253  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->erase(d);
254  }
255 
256  template < typename GUM_SCALAR >
257  INLINE void MultiDimDecorator< GUM_SCALAR >::erase(const std::string& name) {
258  erase(variable(name));
259  }
260 
261  template < typename GUM_SCALAR >
262  INLINE const DiscreteVariable&
264  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(i);
265  }
266 
267  template < typename GUM_SCALAR >
268  INLINE const DiscreteVariable&
269  MultiDimDecorator< GUM_SCALAR >::variable(const std::string& name) const {
270  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(
271  name);
272  }
273 
274  template < typename GUM_SCALAR >
275  INLINE Idx
277  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->pos(d);
278  }
279 
280  template < typename GUM_SCALAR >
281  INLINE bool
283  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->contains(d);
284  }
285 
286  template < typename GUM_SCALAR >
288  if (_content == nullptr) return true;
289  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty();
290  }
291 
292  template < typename GUM_SCALAR >
294  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
295  ->unregisterSlave(i);
296  }
297 
298  template < typename GUM_SCALAR >
299  INLINE void MultiDimDecorator< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
300  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
301  _empty_value = d;
302  } else {
303  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->fill(d);
304  }
305  }
306 
307  // notification modification on vars to all Instantiation listeners.
308 
309  template < typename GUM_SCALAR >
311  /*( (MultiDimContainer<GUM_SCALAR> *) _content)->notifyChange();*/
312  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
313  }
314 
315  // give a const ref to the sequence of DiscreteVariable*. final method.
316 
317  template < typename GUM_SCALAR >
320  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
321  ->variablesSequence();
322  }
323 
324  // get the nbr of vars in the sequence. final method
325 
326  template < typename GUM_SCALAR >
328  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim();
329  }
330 
331  template < typename GUM_SCALAR >
333  const std::vector< GUM_SCALAR >& v) const {
334  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
335  if (v.size() == 1) {
336  _empty_value = v[0];
337  } else {
338  GUM_ERROR(SizeError, "Size do not match in populate")
339  }
340  } else {
341  _content->populate(v);
342  }
343  }
344 
345  template < typename GUM_SCALAR >
347  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
348  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
350  } else {
351  _content->apply(f);
352  }
353  }
354 
355  template < typename GUM_SCALAR >
357  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
358  GUM_SCALAR base) const {
359  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
360  return base;
361  } else {
362  return _content->reduce(f, base);
363  }
364  }
365 
366  // protected access to _content
367  template < typename GUM_SCALAR >
370  return _content;
371  }
372 
373  // protected access to _content
374  template < typename GUM_SCALAR >
377  return _content;
378  }
379 
380  template < typename GUM_SCALAR >
384  }
385 
386  template < typename GUM_SCALAR >
389  ->endMultipleChanges();
390  }
391 
392  template < typename GUM_SCALAR >
393  INLINE void
396  x);
397  }
398 
399  template < typename GUM_SCALAR >
401  MultiDimImplementation< GUM_SCALAR >* aContent) const {
402  if (aContent != nullptr) {
403  // TODO : frees all slave instantiations
404  // TODO : control the dimensions ?
406  _content = aContent;
407  // registers all instantiations
408  delete (tmp);
409  }
410  }
411 
412 
413  // string representation of internal data about i in this.
414  template < typename GUM_SCALAR >
415  INLINE const std::string
417  return _content->toString(i);
418  }
419 
420 
421  template < typename GUM_SCALAR >
422  INLINE void
424  const DiscreteVariable* y) {
425  this->content()->replace(*x, *y);
426  }
427 
428  template < typename GUM_SCALAR >
429  INLINE const std::string MultiDimDecorator< GUM_SCALAR >::toString() const {
430  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
431  std::stringstream ss;
432  ss << "<> :: " << _empty_value;
433  return ss.str();
434  } else {
435  return _content->toString();
436  }
437  }
438 
439  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
440 } /* namespace gum */
a class used to register projections over non-pointers types
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void init()
Initialize the partial instantiation functions.
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimImplementation< GUM_SCALAR > * _content
The true container.
virtual Size domainSize() const final
Returns the product of the variables domain size.
virtual const std::string toString() const
Default implementation of MultiDimContainer::set().
virtual bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.
GUM_SCALAR & _get(const Instantiation &i) const final
Return a data, given a Insantiation - final method.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
GUM_SCALAR _empty_value
value of the MultiDimDecorator if no dimension.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Base class for discrete random variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
a class used to register operators over non-pointers typesThis class is of course completely redundan...
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y)
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y...
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
virtual Size domainSize() const =0
virtual void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Class constructor.
virtual void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
void _swapContent(MultiDimImplementation< GUM_SCALAR > *aContent) const
protecte method to swap the implementation behind the Potential
virtual bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
virtual void notifyChange() const final
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.
virtual void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
Decorator design pattern in order to separate implementations from multidimensional matrix concepts...
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
<agrum/tools/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
~MultiDimDecorator()
Class destructor.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
void ___initPotentialOperators()
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
A class used to register instantiation functions over non-pointers types.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
a class used to register complete projections over non-pointers typesThis class is of course complete...
MultiDimContainer & operator=(const MultiDimContainer< GUM_SCALAR > &src)
Default constructor.