aGrUM  0.16.0
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 >
90  std::forward< MultiDimContainer< GUM_SCALAR > >(from));
91  GUM_OP_MOV(MultiDimDecorator);
92 
93  if (this != &from) {
94  if (_content != nullptr) delete (_content); // should be the case
95  _empty_value = from._empty_value;
96  _content = from._content;
97  from._content = nullptr;
98  }
99 
100  return *this;
101  }
102 
103 
104  template < typename GUM_SCALAR >
106  MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
108  std::forward< MultiDimContainer< GUM_SCALAR > >(from)) {
109  GUM_CONS_MOV(MultiDimDecorator);
110 
111  _empty_value = from._empty_value;
112  _content = from._content;
113  from._content = nullptr;
114  }
115 
116 
117  template < typename GUM_SCALAR >
120  GUM_OP_CPY(MultiDimDecorator);
121  ___initPotentialOperators< GUM_SCALAR >();
123  _empty_value = from._empty_value;
124  MultiDimDecorator< GUM_SCALAR >::content()->copy(*from.content());
125  return *this;
126  }
127 
128 
129  // destructor
130 
131  template < typename GUM_SCALAR >
133  if (_content != nullptr) { delete (_content); }
134 
135  GUM_DESTRUCTOR(MultiDimDecorator);
136  }
137 
138  // return a data, given a Instantiation - final method
139 
140  template < typename GUM_SCALAR >
141  INLINE GUM_SCALAR&
143  GUM_ERROR(OperationNotAllowed, "_get in the implementation !");
144  }
145 
146  template < typename GUM_SCALAR >
147  INLINE GUM_SCALAR
149  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
150  return _empty_value;
151  } else {
152  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->get(i);
153  }
154  }
155 
156  template < typename GUM_SCALAR >
158  const GUM_SCALAR& value) const {
159  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim() == 0) {
160  _empty_value = value;
161  } else {
162  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->set(i, value);
163  }
164  }
165 
166  // get the size of domains - final method
167 
168  template < typename GUM_SCALAR >
170  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->domainSize();
171  }
172 
173  // add a new var to the sequence of vars - final method
174 
175  template < typename GUM_SCALAR >
177  if (v.domainSize() < 1) {
179  "Empty variable " << v << " cannot be added in a Potential");
180  }
181  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->add(v);
182  }
183 
184  // listen to change in each recorded Instantiation. final method
185 
186  template < typename GUM_SCALAR >
188  const Instantiation& i,
189  const DiscreteVariable* const var,
190  Idx oldval,
191  Idx newval) {
193  i, var, oldval, newval);
194  }
195 
196  // listen to an assignment of a value in a Instantiation
197 
198  template < typename GUM_SCALAR >
200  const Instantiation& i) {
203  }
204 
205  // listen to setFirst in each recorded Instantiation. final method.
206 
207  template < typename GUM_SCALAR >
209  const Instantiation& i) {
212  }
213 
214  // listen to setLast in each recorded Instantiation. final method.
215 
216  template < typename GUM_SCALAR >
217  INLINE void
220  i);
221  }
222 
223  // listen to increment in each recorded Instantiation. final method.
224 
225  template < typename GUM_SCALAR >
226  INLINE void
229  i);
230  }
231 
232  // listen to increment in each recorded Instantiation. final method.
233 
234  template < typename GUM_SCALAR >
235  INLINE void
238  i);
239  }
240 
241  // add a Instantiation as a slave of this
242 
243  template < typename GUM_SCALAR >
245  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
246  ->registerSlave(i);
247  }
248 
249  template < typename GUM_SCALAR >
251  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->erase(d);
252  }
253 
254  template < typename GUM_SCALAR >
255  INLINE const DiscreteVariable&
257  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(i);
258  }
259 
260  template < typename GUM_SCALAR >
261  INLINE const DiscreteVariable&
262  MultiDimDecorator< GUM_SCALAR >::variable(const std::string& name) const {
263  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(
264  name);
265  }
266 
267  template < typename GUM_SCALAR >
268  INLINE Idx
270  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->pos(d);
271  }
272 
273  template < typename GUM_SCALAR >
274  INLINE bool
276  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->contains(d);
277  }
278 
279  template < typename GUM_SCALAR >
281  if (_content == nullptr) return true;
282  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty();
283  }
284 
285  template < typename GUM_SCALAR >
287  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
288  ->unregisterSlave(i);
289  }
290 
291  template < typename GUM_SCALAR >
292  INLINE void MultiDimDecorator< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
293  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
294  _empty_value = d;
295  } else {
296  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->fill(d);
297  }
298  }
299 
300  // notification modification on vars to all Instantiation listeners.
301 
302  template < typename GUM_SCALAR >
304  /*( (MultiDimContainer<GUM_SCALAR> *) _content)->notifyChange();*/
305  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
306  }
307 
308  // give a const ref to the sequence of DiscreteVariable*. final method.
309 
310  template < typename GUM_SCALAR >
313  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
314  ->variablesSequence();
315  }
316 
317  // get the nbr of vars in the sequence. final method
318 
319  template < typename GUM_SCALAR >
321  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim();
322  }
323 
324  template < typename GUM_SCALAR >
326  const std::vector< GUM_SCALAR >& v) const {
327  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
328  if (v.size() == 1) {
329  _empty_value = v[0];
330  } else {
331  GUM_ERROR(SizeError, "Size do not match in populate")
332  }
333  } else {
334  _content->populate(v);
335  }
336  }
337 
338  template < typename GUM_SCALAR >
340  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
341  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
343  } else {
344  _content->apply(f);
345  }
346  }
347 
348  template < typename GUM_SCALAR >
350  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
351  GUM_SCALAR base) const {
352  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
353  return base;
354  } else {
355  return _content->reduce(f, base);
356  }
357  }
358 
359  // protected access to _content
360  template < typename GUM_SCALAR >
363  return _content;
364  }
365 
366  // protected access to _content
367  template < typename GUM_SCALAR >
370  return _content;
371  }
372 
373  template < typename GUM_SCALAR >
377  }
378 
379  template < typename GUM_SCALAR >
382  ->endMultipleChanges();
383  }
384 
385  template < typename GUM_SCALAR >
386  INLINE void
389  x);
390  }
391 
392  template < typename GUM_SCALAR >
394  MultiDimImplementation< GUM_SCALAR >* aContent) const {
395  if (aContent != nullptr) {
396  // TODO : frees all slave instantiations
397  // TODO : control the dimensions ?
399  _content = aContent;
400  // registers all instantiations
401  delete (tmp);
402  }
403  }
404 
405 
406  // string representation of internal data about i in this.
407  template < typename GUM_SCALAR >
408  INLINE const std::string
410  return _content->toString(i);
411  }
412 
413 
414  template < typename GUM_SCALAR >
415  INLINE void
417  const DiscreteVariable* y) {
418  this->content()->replace(*x, *y);
419  }
420 
421  template < typename GUM_SCALAR >
422  INLINE const std::string MultiDimDecorator< GUM_SCALAR >::toString() const {
423  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
424  std::stringstream ss;
425  ss << "<> :: " << _empty_value;
426  return ss.str();
427  } else {
428  return _content->toString();
429  }
430  }
431 
432  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
433 } /* namespace gum */
a class used to register projections over non-pointers types
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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/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.