aGrUM  0.13.2
multiDimDecorator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // include the operators that will be used by the decorators
32 
34 
35 namespace gum {
36  // instrumental and non-API function
37  template < typename GUM_SCALAR >
38  static void ___initPotentialOperators() {
39  static bool first = true;
40 
41  if (first) {
42  first = false;
43 
44  // register the operators that will be used by the decorator
46  op.init();
47 
48  // register the projectors that will be used by the decorator
50  proj.init();
51 
52  // register the projectors that will be used by the decorator
54  comp_proj.init();
55 
56  // register the partial instantiators that will be used by the decorator
58  inst.init();
59  }
60  }
61  // constructors
62 
63  template < typename GUM_SCALAR >
65  MultiDimImplementation< GUM_SCALAR >* aContent, GUM_SCALAR empty_value) :
66  _content(aContent),
67  _empty_value(empty_value) {
68  ___initPotentialOperators< GUM_SCALAR >();
69  GUM_CONSTRUCTOR(MultiDimDecorator);
70  }
71 
72  template < typename GUM_SCALAR >
74  const MultiDimDecorator< GUM_SCALAR >& from) :
75  MultiDimContainer< GUM_SCALAR >(from) {
76  GUM_CONS_CPY(MultiDimDecorator);
77  ___initPotentialOperators< GUM_SCALAR >();
79  content()->copy(from.content());
80  }
81 
82 
83  template < typename GUM_SCALAR >
87  std::forward< MultiDimContainer< GUM_SCALAR > >(from));
88  GUM_OP_MOV(MultiDimDecorator);
89 
90  if (this != &from) {
91  if (_content != nullptr) delete (_content); // should be the case
92  _empty_value = from._empty_value;
93  _content = from._content;
94  from._content = nullptr;
95  }
96 
97  return *this;
98  }
99 
100 
101  template < typename GUM_SCALAR >
103  MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
104  MultiDimContainer< GUM_SCALAR >(
105  std::forward< MultiDimContainer< GUM_SCALAR > >(from)) {
106  GUM_CONS_MOV(MultiDimDecorator);
107 
108  _empty_value = from._empty_value;
109  _content = from._content;
110  from._content = nullptr;
111  }
112 
113 
114  template < typename GUM_SCALAR >
117  GUM_OP_CPY(MultiDimDecorator);
118  ___initPotentialOperators< GUM_SCALAR >();
120  _empty_value = from._empty_value;
121  MultiDimDecorator< GUM_SCALAR >::content()->copy(*from.content());
122  return *this;
123  }
124 
125 
126  // destructor
127 
128  template < typename GUM_SCALAR >
130  if (_content != nullptr) { delete (_content); }
131 
132  GUM_DESTRUCTOR(MultiDimDecorator);
133  }
134 
135  // return a data, given a Instantiation - final method
136 
137  template < typename GUM_SCALAR >
138  INLINE GUM_SCALAR&
140  GUM_ERROR(OperationNotAllowed, "_get in the implementation !");
141  }
142 
143  template < typename GUM_SCALAR >
144  INLINE GUM_SCALAR
146  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
147  return _empty_value;
148  } else {
149  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->get(i);
150  }
151  }
152 
153  template < typename GUM_SCALAR >
155  const GUM_SCALAR& value) const {
156  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim() == 0) {
157  _empty_value = value;
158  } else {
159  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->set(i, value);
160  }
161  }
162 
163  // get the size of domains - final method
164 
165  template < typename GUM_SCALAR >
167  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->domainSize();
168  }
169 
170  // add a new var to the sequence of vars - final method
171 
172  template < typename GUM_SCALAR >
174  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->add(v);
175  }
176 
177  // listen to change in each recorded Instantiation. final method
178 
179  template < typename GUM_SCALAR >
181  const Instantiation& i,
182  const DiscreteVariable* const var,
183  Idx oldval,
184  Idx newval) {
186  i, var, oldval, newval);
187  }
188 
189  // listen to an assignment of a value in a Instantiation
190 
191  template < typename GUM_SCALAR >
193  const Instantiation& i) {
196  }
197 
198  // listen to setFirst in each recorded Instantiation. final method.
199 
200  template < typename GUM_SCALAR >
201  INLINE void
205  }
206 
207  // listen to setLast in each recorded Instantiation. final method.
208 
209  template < typename GUM_SCALAR >
210  INLINE void
213  i);
214  }
215 
216  // listen to increment 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  // add a Instantiation as a slave of this
235 
236  template < typename GUM_SCALAR >
238  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
239  ->registerSlave(i);
240  }
241 
242  template < typename GUM_SCALAR >
244  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->erase(d);
245  }
246 
247  template < typename GUM_SCALAR >
248  INLINE const DiscreteVariable&
250  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(i);
251  }
252 
253  template < typename GUM_SCALAR >
254  INLINE const DiscreteVariable&
255  MultiDimDecorator< GUM_SCALAR >::variable(const std::string& name) const {
256  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(
257  name);
258  }
259 
260  template < typename GUM_SCALAR >
261  INLINE Idx
263  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->pos(d);
264  }
265 
266  template < typename GUM_SCALAR >
267  INLINE bool
269  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->contains(d);
270  }
271 
272  template < typename GUM_SCALAR >
274  if (_content == nullptr) return true;
275  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty();
276  }
277 
278  template < typename GUM_SCALAR >
280  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
281  ->unregisterSlave(i);
282  }
283 
284  template < typename GUM_SCALAR >
285  INLINE void MultiDimDecorator< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
286  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
287  _empty_value = d;
288  } else {
289  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->fill(d);
290  }
291  }
292 
293  // notification modification on vars to all Instantiation listeners.
294 
295  template < typename GUM_SCALAR >
297  /*( (MultiDimContainer<GUM_SCALAR> *) _content)->notifyChange();*/
298  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
299  }
300 
301  // give a const ref to the sequence of DiscreteVariable*. final method.
302 
303  template < typename GUM_SCALAR >
306  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
307  ->variablesSequence();
308  }
309 
310  // get the nbr of vars in the sequence. final method
311 
312  template < typename GUM_SCALAR >
314  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim();
315  }
316 
317  template < typename GUM_SCALAR >
319  const std::vector< GUM_SCALAR >& v) const {
320  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
321  if (v.size() == 1) {
322  _empty_value = v[0];
323  } else {
324  GUM_ERROR(SizeError, "Size do not match in populate")
325  }
326  } else {
327  _content->populate(v);
328  }
329  }
330 
331  template < typename GUM_SCALAR >
333  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
334  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
336  } else {
337  _content->apply(f);
338  }
339  }
340 
341  template < typename GUM_SCALAR >
343  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const {
344  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
345  return base;
346  } else {
347  return _content->reduce(f, base);
348  }
349  }
350 
351  // protected access to _content
352  template < typename GUM_SCALAR >
355  return _content;
356  }
357 
358  // protected access to _content
359  template < typename GUM_SCALAR >
362  return _content;
363  }
364 
365  template < typename GUM_SCALAR >
369  }
370 
371  template < typename GUM_SCALAR >
374  ->endMultipleChanges();
375  }
376 
377  template < typename GUM_SCALAR >
378  INLINE void
381  x);
382  }
383 
384  template < typename GUM_SCALAR >
386  MultiDimImplementation< GUM_SCALAR >* aContent) const {
387  if (aContent != nullptr) {
388  // TODO : frees all slave instantiations
389  // TODO : control the dimensions ?
391  _content = aContent;
392  // registers all instantiations
393  delete (tmp);
394  }
395  }
396 
397 
398  // string representation of internal data about i in this.
399  template < typename GUM_SCALAR >
400  INLINE const std::string
402  return _content->toString(i);
403  }
404 
405 
406  template < typename GUM_SCALAR >
407  INLINE void
409  const DiscreteVariable* y) {
410  this->content()->replace(*x, *y);
411  }
412 
413  template < typename GUM_SCALAR >
414  INLINE const std::string MultiDimDecorator< GUM_SCALAR >::toString() const {
415  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
416  std::stringstream ss;
417  ss << "<> :: " << _empty_value;
418  return ss.str();
419  } else {
420  return _content->toString();
421  }
422  }
423 
424  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
425 } /* namespace gum */
a class used to register projections over non-pointers types
Headers for partial instantiation functions.
void init()
Initialize the partial instantiation functions.
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
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 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:1019
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
STL namespace.
GUM_SCALAR _empty_value
value of the MultiDimDecorator if no dimension.
Efficient functionals for combining multiDims.
Efficient functionals for projecting multidim tables over all their variables.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
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.
static void ___initPotentialOperators()
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 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.
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.
Efficient functionals for projecting multiDimensional tables.
Headers for MultiDimDecorator.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
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:80
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().
void _swapContent(MultiDimImplementation< GUM_SCALAR > *aContent) const
protecte method to swap the implementation behind the Potential
<agrum/multidim/multiDimImplementation.h>
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
~MultiDimDecorator()
Class destructor.
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
unsigned long Idx
Type for indexes.
Definition: types.h:43
virtual const std::string toString() const
Default implementation of MultiDimContainer::set().
A class used to register instantiation functions over non-pointers types.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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.