aGrUM  0.16.0
multiDimImplementation_tpl.h
Go to the documentation of this file.
1 
30 // to ease IDE parser
32 
33 namespace gum {
34 
35  // Default constructor
36 
37  template < typename GUM_SCALAR >
39  MultiDimContainer< GUM_SCALAR >(), __vars(), __slaveInstantiations() {
40  GUM_CONSTRUCTOR(MultiDimImplementation);
41  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
42  __internalChangeState = __InternalChangeState::NO_CHANGE;
43  __domainSize = 1;
44  }
45 
46  // Copy constructor
47 
48  template < typename GUM_SCALAR >
51  MultiDimContainer< GUM_SCALAR >(from),
52  __vars(from.__vars), __internalChangeMethod(from.__internalChangeMethod),
53  __internalChangeState(from.__internalChangeState),
54  __domainSize(from.__domainSize) {
55  GUM_CONS_CPY(MultiDimImplementation);
56  GUM_ASSERT(!this->_isCommitNeeded());
57  }
58 
59  // destructor
60 
61  template < typename GUM_SCALAR >
63  GUM_DESTRUCTOR(MultiDimImplementation);
64  // unregister all remaining slave instantiations
65 
67  __slaveInstantiations.beginSafe();
68  iter != __slaveInstantiations.endSafe();
69  ++iter)
70  (*iter)->forgetMaster();
71  }
72 
73  // add a new var to the sequence of __vars.
74 
75  template < typename GUM_SCALAR >
76  INLINE void
78  // check if the variable already belongs to the tuple of variables
79  // of the Instantiation
80  if (__vars.exists(&v)) {
82  "Var " << v.name() << " already exists in this instantiation");
83  }
84  for (const auto& w : __vars) {
85  if (w->name() == v.name())
87  "A var with name '" << v.name()
88  << "' already exists in this instantiation");
89  }
90 
91  __domainSize *= v.domainSize();
92 
93  __vars.insert(&v);
94 
95  // informs all the slaves that they have to update themselves
97  __slaveInstantiations.beginSafe();
98  iter != __slaveInstantiations.endSafe();
99  ++iter) {
100  (*iter)->addWithMaster(this, v);
101  }
102 
103  if (_isInMultipleChangeMethod()) __setNotCommitedChange();
104  }
105 
106  // removes a var from the variables of the multidimensional matrix
107 
108  template < typename GUM_SCALAR >
109  INLINE void
111  // check that the variable does actually belong to the
112  // MultiDimImplementation
113  if (!__vars.exists(&v)) {
114  GUM_ERROR(NotFound, "Var does not exist in this implementation");
115  }
116 
117  __domainSize /= v.domainSize();
118 
119  __vars.erase(&v);
120 
121  // informs all the slaves that they have to update themselves
123  __slaveInstantiations.beginSafe();
124  iter != __slaveInstantiations.endSafe();
125  ++iter) {
126  (*iter)->eraseWithMaster(this, v);
127  }
128 
129  if (_isInMultipleChangeMethod()) __setNotCommitedChange();
130  }
131 
132  // adds a new var to the sequence of __vars
133  // @todo this function should be declared somewhere?
134 
135  template < typename GUM_SCALAR >
137  operator<<(MultiDimImplementation< GUM_SCALAR >& array,
138  const DiscreteVariable& v) {
139  array.add(v);
140  return array;
141  }
142 
143  // add a Instantiation to the list of slave instantiations
144 
145  template < typename GUM_SCALAR >
146  INLINE bool
148  // check that the Instantiation has the same variables as this
149  if (slave.nbrDim() != __vars.size()) return false;
150 
152  __vars.beginSafe();
153  iter != __vars.endSafe();
154  ++iter)
155  if (!slave.contains(*iter)) return false;
156 
157  slave.synchronizeWithMaster(this);
158 
159  __slaveInstantiations += (&slave);
160 
161  return true;
162  }
163 
164  // removes a Instantiation from the list of slave instantiations
165 
166  template < typename GUM_SCALAR >
167  INLINE bool
169  __slaveInstantiations.eraseByVal(&slave);
170  // TODO This method should return true? Why not use a void instead?
171  return true;
172  }
173 
174  template < typename GUM_SCALAR >
176  return __vars.size();
177  }
178 
179  template < typename GUM_SCALAR >
181  return __domainSize;
182  }
183 
184  template < typename GUM_SCALAR >
185  INLINE const DiscreteVariable&
187  return *(__vars.atPos(i));
188  }
189 
190  template < typename GUM_SCALAR >
192  const std::string& name) const {
193  for (const auto& v : __vars) {
194  if (v->name() == name) return *v;
195  }
196 
198  "'" << name << "' can not be found in the multidim structure.")
199  }
200 
201  template < typename GUM_SCALAR >
202  INLINE Idx
204  return __vars.pos(&v);
205  }
206 
207  template < typename GUM_SCALAR >
209  const DiscreteVariable& v) const {
210  return __vars.exists(&v);
211  }
212 
213  // returns a const ref to the sequence of DiscreteVariable*
214 
215  template < typename GUM_SCALAR >
218  return __vars;
219  }
220 
221  // is this empty ?
222  template < typename GUM_SCALAR >
224  GUM_ASSERT(!this->_isCommitNeeded());
225  return __vars.empty();
226  }
227 
228  template < typename GUM_SCALAR >
230  __internalChangeMethod = __InternalChangeMethod::MULTIPLE_CHANGE;
231  }
232 
233  template < typename GUM_SCALAR >
235  if (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE) {
236  _commitMultipleChanges();
237  __internalChangeState = __InternalChangeState::NO_CHANGE;
238  }
239 
240  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
241  }
242 
243  template < typename GUM_SCALAR >
245  const GUM_SCALAR& x) {
246  if (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE) {
247  _commitMultipleChanges(x);
248  __internalChangeState = __InternalChangeState::NO_CHANGE;
249  }
250 
251  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
252  }
253 
254  template < typename GUM_SCALAR >
256  // empty!
257  }
258 
259  template < typename GUM_SCALAR >
261  const GUM_SCALAR&) {
262  // empty!
263  }
264 
265  // get the actual change method of *this
266  template < typename GUM_SCALAR >
267  INLINE bool
269  return (__internalChangeMethod == __InternalChangeMethod::MULTIPLE_CHANGE);
270  }
271 
272  // get the actual state of *this
273  template < typename GUM_SCALAR >
275  return (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE);
276  }
277 
278  // Returns a constant reference over the list of slaved instantiations.
279  template < typename GUM_SCALAR >
280  INLINE const List< Instantiation* >&
282  return __slaveInstantiations;
283  }
284 
285  // get the actual state of *this
286  template < typename GUM_SCALAR >
288  __internalChangeState = __InternalChangeState::NOT_COMMITTED_CHANGE;
289  }
290 
291  // get the actual state of *this
292  template < typename GUM_SCALAR >
294  return ((float)1) - (float)realSize() / (float)domainSize();
295  }
296 
297  // returns a basename to be used for default operators
298  template < typename GUM_SCALAR >
300  static const std::string str = "MultiDimImplementation";
301  return str;
302  }
303 
304  template < typename GUM_SCALAR >
305  INLINE void
307  const DiscreteVariable* y) {
308  __vars.setAtPos(__vars.pos(x), y);
309 
311  __slaveInstantiations.beginSafe();
312  iter != __slaveInstantiations.endSafe();
313  ++iter) {
314  (**iter).replace(*x, *y);
315  }
316  }
317 
318  template < typename GUM_SCALAR >
320  __vars.swap(p1, p2);
321  }
322 
323  // for friendly displaying the content of the array
324  template < typename GUM_SCALAR >
325  INLINE std::ostream&
326  operator<<(std::ostream& out,
328  return out << static_cast< const MultiDimContainer< GUM_SCALAR >& >(array);
329  }
330 
331 
332  // protected access to _content
333  template < typename GUM_SCALAR >
336  return this;
337  }
338 
339  // protected access to _content
340  template < typename GUM_SCALAR >
343  return this;
344  }
345 } /* namespace gum */
Safe iterators for Sequence.
Definition: sequence.h:1206
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Safe iterators for Lists.
Definition: list.h:2342
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:372
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual Size domainSize() const =0
MultiDimImplementation()
Default constructor.
INLINE std::ostream & operator<<(std::ostream &out, const MultiDimImplementation< GUM_SCALAR > &array)
For friendly displaying the content of the array.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
void replace(const DiscreteVariable &x, const DiscreteVariable &y)
Replace variables in this multidim.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
virtual Idx pos(const DiscreteVariable &v) const override
Returns the index of a variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55