aGrUM  0.14.2
multiDimImplementation_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 // to ease IDE parser
29 
30 namespace gum {
31 
32  // Default constructor
33 
34  template < typename GUM_SCALAR >
36  MultiDimContainer< GUM_SCALAR >(), __vars(), __slaveInstantiations() {
37  GUM_CONSTRUCTOR(MultiDimImplementation);
38  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
39  __internalChangeState = __InternalChangeState::NO_CHANGE;
40  __domainSize = 1;
41  }
42 
43  // Copy constructor
44 
45  template < typename GUM_SCALAR >
48  MultiDimContainer< GUM_SCALAR >(from),
49  __vars(from.__vars), __internalChangeMethod(from.__internalChangeMethod),
50  __internalChangeState(from.__internalChangeState),
51  __domainSize(from.__domainSize) {
52  GUM_CONS_CPY(MultiDimImplementation);
53  GUM_ASSERT(!this->_isCommitNeeded());
54  }
55 
56  // destructor
57 
58  template < typename GUM_SCALAR >
60  GUM_DESTRUCTOR(MultiDimImplementation);
61  // unregister all remaining slave instantiations
62 
64  __slaveInstantiations.beginSafe();
65  iter != __slaveInstantiations.endSafe();
66  ++iter)
67  (*iter)->forgetMaster();
68  }
69 
70  // add a new var to the sequence of __vars.
71 
72  template < typename GUM_SCALAR >
73  INLINE void
75  // check if the variable already belongs to the tuple of variables
76  // of the Instantiation
77  if (__vars.exists(&v)) {
79  "Var " << v.name() << " already exists in this instantiation");
80  }
81  for (const auto& w : __vars) {
82  if (w->name() == v.name())
84  "A var with name '" << v.name()
85  << "' already exists in this instantiation");
86  }
87 
88  __domainSize *= v.domainSize();
89 
90  __vars.insert(&v);
91 
92  // informs all the slaves that they have to update themselves
94  __slaveInstantiations.beginSafe();
95  iter != __slaveInstantiations.endSafe();
96  ++iter) {
97  (*iter)->addWithMaster(this, v);
98  }
99 
100  if (_isInMultipleChangeMethod()) __setNotCommitedChange();
101  }
102 
103  // removes a var from the variables of the multidimensional matrix
104 
105  template < typename GUM_SCALAR >
106  INLINE void
108  // check that the variable does actually belong to the
109  // MultiDimImplementation
110  if (!__vars.exists(&v)) {
111  GUM_ERROR(NotFound, "Var does not exist in this implementation");
112  }
113 
114  __domainSize /= v.domainSize();
115 
116  __vars.erase(&v);
117 
118  // informs all the slaves that they have to update themselves
120  __slaveInstantiations.beginSafe();
121  iter != __slaveInstantiations.endSafe();
122  ++iter) {
123  (*iter)->eraseWithMaster(this, v);
124  }
125 
126  if (_isInMultipleChangeMethod()) __setNotCommitedChange();
127  }
128 
129  // adds a new var to the sequence of __vars
130  // @todo this function should be declared somewhere?
131 
132  template < typename GUM_SCALAR >
134  operator<<(MultiDimImplementation< GUM_SCALAR >& array,
135  const DiscreteVariable& v) {
136  array.add(v);
137  return array;
138  }
139 
140  // add a Instantiation to the list of slave instantiations
141 
142  template < typename GUM_SCALAR >
143  INLINE bool
145  // check that the Instantiation has the same variables as this
146  if (slave.nbrDim() != __vars.size()) return false;
147 
149  __vars.beginSafe();
150  iter != __vars.endSafe();
151  ++iter)
152  if (!slave.contains(*iter)) return false;
153 
154  slave.synchronizeWithMaster(this);
155 
156  __slaveInstantiations += (&slave);
157 
158  return true;
159  }
160 
161  // removes a Instantiation from the list of slave instantiations
162 
163  template < typename GUM_SCALAR >
164  INLINE bool
166  __slaveInstantiations.eraseByVal(&slave);
167  // TODO This method should return true? Why not use a void instead?
168  return true;
169  }
170 
171  template < typename GUM_SCALAR >
173  return __vars.size();
174  }
175 
176  template < typename GUM_SCALAR >
178  return __domainSize;
179  }
180 
181  template < typename GUM_SCALAR >
182  INLINE const DiscreteVariable&
184  return *(__vars.atPos(i));
185  }
186 
187  template < typename GUM_SCALAR >
189  const std::string& name) const {
190  for (const auto& v : __vars) {
191  if (v->name() == name) return *v;
192  }
193 
195  "'" << name << "' can not be found in the multidim structure.")
196  }
197 
198  template < typename GUM_SCALAR >
199  INLINE Idx
201  return __vars.pos(&v);
202  }
203 
204  template < typename GUM_SCALAR >
206  const DiscreteVariable& v) const {
207  return __vars.exists(&v);
208  }
209 
210  // returns a const ref to the sequence of DiscreteVariable*
211 
212  template < typename GUM_SCALAR >
215  return __vars;
216  }
217 
218  // is this empty ?
219  template < typename GUM_SCALAR >
221  GUM_ASSERT(!this->_isCommitNeeded());
222  return __vars.empty();
223  }
224 
225  template < typename GUM_SCALAR >
227  __internalChangeMethod = __InternalChangeMethod::MULTIPLE_CHANGE;
228  }
229 
230  template < typename GUM_SCALAR >
232  if (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE) {
233  _commitMultipleChanges();
234  __internalChangeState = __InternalChangeState::NO_CHANGE;
235  }
236 
237  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
238  }
239 
240  template < typename GUM_SCALAR >
242  const GUM_SCALAR& x) {
243  if (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE) {
244  _commitMultipleChanges(x);
245  __internalChangeState = __InternalChangeState::NO_CHANGE;
246  }
247 
248  __internalChangeMethod = __InternalChangeMethod::DIRECT_CHANGE;
249  }
250 
251  template < typename GUM_SCALAR >
253  // empty!
254  }
255 
256  template < typename GUM_SCALAR >
258  const GUM_SCALAR&) {
259  // empty!
260  }
261 
262  // get the actual change method of *this
263  template < typename GUM_SCALAR >
264  INLINE bool
266  return (__internalChangeMethod == __InternalChangeMethod::MULTIPLE_CHANGE);
267  }
268 
269  // get the actual state of *this
270  template < typename GUM_SCALAR >
272  return (__internalChangeState == __InternalChangeState::NOT_COMMITTED_CHANGE);
273  }
274 
275  // Returns a constant reference over the list of slaved instantiations.
276  template < typename GUM_SCALAR >
277  INLINE const List< Instantiation* >&
279  return __slaveInstantiations;
280  }
281 
282  // get the actual state of *this
283  template < typename GUM_SCALAR >
285  __internalChangeState = __InternalChangeState::NOT_COMMITTED_CHANGE;
286  }
287 
288  // get the actual state of *this
289  template < typename GUM_SCALAR >
291  return ((float)1) - (float)realSize() / (float)domainSize();
292  }
293 
294  // returns a basename to be used for default operators
295  template < typename GUM_SCALAR >
297  static const std::string str = "MultiDimImplementation";
298  return str;
299  }
300 
301  template < typename GUM_SCALAR >
302  INLINE void
304  const DiscreteVariable* y) {
305  __vars.setAtPos(__vars.pos(x), y);
306 
308  __slaveInstantiations.beginSafe();
309  iter != __slaveInstantiations.endSafe();
310  ++iter) {
311  (**iter).replace(*x, *y);
312  }
313  }
314 
315  template < typename GUM_SCALAR >
317  __vars.swap(p1, p2);
318  }
319 
320  // for friendly displaying the content of the array
321  template < typename GUM_SCALAR >
322  INLINE std::ostream&
323  operator<<(std::ostream& out,
325  return out << static_cast< const MultiDimContainer< GUM_SCALAR >& >(array);
326  }
327 
328 
329  // protected access to _content
330  template < typename GUM_SCALAR >
333  return this;
334  }
335 
336  // protected access to _content
337  template < typename GUM_SCALAR >
340  return this;
341  }
342 } /* namespace gum */
Safe iterators for Sequence.
Definition: sequence.h:1203
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Safe iterators for Lists.
Definition: list.h:2339
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Headers of gum::MultiDimImplementation.
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:80
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:50
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:45
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52