aGrUM  0.14.3
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 >
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 
62  // constructors
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 :
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  if (v.domainSize() < 1) {
176  "Empty variable " << v << " cannot be added in a Potential");
177  }
178  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->add(v);
179  }
180 
181  // listen to change in each recorded Instantiation. final method
182 
183  template < typename GUM_SCALAR >
185  const Instantiation& i,
186  const DiscreteVariable* const var,
187  Idx oldval,
188  Idx newval) {
190  i, var, oldval, newval);
191  }
192 
193  // listen to an assignment of a value in a Instantiation
194 
195  template < typename GUM_SCALAR >
197  const Instantiation& i) {
200  }
201 
202  // listen to setFirst in each recorded Instantiation. final method.
203 
204  template < typename GUM_SCALAR >
206  const Instantiation& i) {
209  }
210 
211  // listen to setLast in each recorded Instantiation. final method.
212 
213  template < typename GUM_SCALAR >
214  INLINE void
217  i);
218  }
219 
220  // listen to increment in each recorded Instantiation. final method.
221 
222  template < typename GUM_SCALAR >
223  INLINE void
226  i);
227  }
228 
229  // listen to increment in each recorded Instantiation. final method.
230 
231  template < typename GUM_SCALAR >
232  INLINE void
235  i);
236  }
237 
238  // add a Instantiation as a slave of this
239 
240  template < typename GUM_SCALAR >
242  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
243  ->registerSlave(i);
244  }
245 
246  template < typename GUM_SCALAR >
248  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->erase(d);
249  }
250 
251  template < typename GUM_SCALAR >
252  INLINE const DiscreteVariable&
254  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(i);
255  }
256 
257  template < typename GUM_SCALAR >
258  INLINE const DiscreteVariable&
259  MultiDimDecorator< GUM_SCALAR >::variable(const std::string& name) const {
260  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->variable(
261  name);
262  }
263 
264  template < typename GUM_SCALAR >
265  INLINE Idx
267  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->pos(d);
268  }
269 
270  template < typename GUM_SCALAR >
271  INLINE bool
273  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->contains(d);
274  }
275 
276  template < typename GUM_SCALAR >
278  if (_content == nullptr) return true;
279  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty();
280  }
281 
282  template < typename GUM_SCALAR >
284  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
285  ->unregisterSlave(i);
286  }
287 
288  template < typename GUM_SCALAR >
289  INLINE void MultiDimDecorator< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
290  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
291  _empty_value = d;
292  } else {
293  static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->fill(d);
294  }
295  }
296 
297  // notification modification on vars to all Instantiation listeners.
298 
299  template < typename GUM_SCALAR >
301  /*( (MultiDimContainer<GUM_SCALAR> *) _content)->notifyChange();*/
302  GUM_ERROR(OperationNotAllowed, "Not implemented yet");
303  }
304 
305  // give a const ref to the sequence of DiscreteVariable*. final method.
306 
307  template < typename GUM_SCALAR >
310  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)
311  ->variablesSequence();
312  }
313 
314  // get the nbr of vars in the sequence. final method
315 
316  template < typename GUM_SCALAR >
318  return static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->nbrDim();
319  }
320 
321  template < typename GUM_SCALAR >
323  const std::vector< GUM_SCALAR >& v) const {
324  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
325  if (v.size() == 1) {
326  _empty_value = v[0];
327  } else {
328  GUM_ERROR(SizeError, "Size do not match in populate")
329  }
330  } else {
331  _content->populate(v);
332  }
333  }
334 
335  template < typename GUM_SCALAR >
337  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
338  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
340  } else {
341  _content->apply(f);
342  }
343  }
344 
345  template < typename GUM_SCALAR >
347  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
348  GUM_SCALAR base) const {
349  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
350  return base;
351  } else {
352  return _content->reduce(f, base);
353  }
354  }
355 
356  // protected access to _content
357  template < typename GUM_SCALAR >
360  return _content;
361  }
362 
363  // protected access to _content
364  template < typename GUM_SCALAR >
367  return _content;
368  }
369 
370  template < typename GUM_SCALAR >
374  }
375 
376  template < typename GUM_SCALAR >
379  ->endMultipleChanges();
380  }
381 
382  template < typename GUM_SCALAR >
383  INLINE void
386  x);
387  }
388 
389  template < typename GUM_SCALAR >
391  MultiDimImplementation< GUM_SCALAR >* aContent) const {
392  if (aContent != nullptr) {
393  // TODO : frees all slave instantiations
394  // TODO : control the dimensions ?
396  _content = aContent;
397  // registers all instantiations
398  delete (tmp);
399  }
400  }
401 
402 
403  // string representation of internal data about i in this.
404  template < typename GUM_SCALAR >
405  INLINE const std::string
407  return _content->toString(i);
408  }
409 
410 
411  template < typename GUM_SCALAR >
412  INLINE void
414  const DiscreteVariable* y) {
415  this->content()->replace(*x, *y);
416  }
417 
418  template < typename GUM_SCALAR >
419  INLINE const std::string MultiDimDecorator< GUM_SCALAR >::toString() const {
420  if (static_cast< MultiDimContainer< GUM_SCALAR >* >(_content)->empty()) {
421  std::stringstream ss;
422  ss << "<> :: " << _empty_value;
423  return ss.str();
424  } else {
425  return _content->toString();
426  }
427  }
428 
429  //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
430 } /* 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().
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:1019
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.
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.
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.
Efficient functionals for projecting multiDimensional tables.
Headers for MultiDimDecorator.
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().
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
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:45
~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:52
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.