aGrUM  0.13.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);
40  __domainSize = 1;
41  }
42 
43  // Copy constructor
44 
45  template < typename GUM_SCALAR >
48  MultiDimContainer< GUM_SCALAR >(from),
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 
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 
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 >
188  INLINE const DiscreteVariable&
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 >
228  }
229 
230  template < typename GUM_SCALAR >
235  }
236 
238  }
239 
240  template < typename GUM_SCALAR >
241  INLINE void
246  }
247 
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
267  }
268 
269  // get the actual state of *this
270  template < typename GUM_SCALAR >
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 >
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
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
__InternalChangeState __internalChangeState
The current change state.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Safe iterators for Lists.
Definition: list.h:2339
virtual void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
Size __domainSize
This MultiDimImplementation domain size.
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:369
virtual bool unregisterSlave(Instantiation &slave) override
Unregister i as a slave of this MultiDimAdressable.
virtual void _invert(Idx p1, Idx p2)
Inverts variables at position p1 and p2.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
virtual bool empty() const override
Returns true if no var is in *this.
Headers of gum::MultiDimImplementation.
Sequence< const DiscreteVariable * > __vars
List of discrete variables (dimensions).
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
const std::string & basename() const
Returns the base class name of this MultiDimImplementation.
virtual Size domainSize() const =0
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:573
void __setNotCommitedChange()
Change the __internalChangeState to NOT_COMMITTED_CHANGE.
const List< Instantiation * > & _slaves() const
Returns a constant reference over the list of slaved instantiations.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
MultiDimImplementation()
Default constructor.
bool _isInMultipleChangeMethod() const
Get the actual change method of this MultiDimImplementation.
virtual const std::string & name() const =0
Returns the real name of the multiDim implementation.
List< Instantiation * > __slaveInstantiations
List of instantiations of the tuples (sequences) of variables.
virtual Size realSize() const =0
Returns the real number of parameters used for this table.
virtual void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
virtual ~MultiDimImplementation()
Class destructor.
virtual void endMultipleChanges() override
Call this method after doing important changes in this MultiDimContainer.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
__InternalChangeMethod __internalChangeMethod
The current change method.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual void beginMultipleChanges() override
Call this method before doing important changes in this MultiDimContainer.
virtual Size domainSize() const override
Returns the product of the variables domain size.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
bool _isCommitNeeded() const
Get the actual state of *this.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
virtual void _commitMultipleChanges()
Synchronize content after MultipleChanges.
<agrum/multidim/multiDimImplementation.h>
virtual Idx pos(const DiscreteVariable &v) const override
Returns the index of a variable.
virtual bool contains(const DiscreteVariable &v) const override
Returns true if var is in *this.
float compressionRate() const
The compression ratio of the table (depending on the type of implementation).
unsigned long Idx
Type for indexes.
Definition: types.h:43
const std::string & name() const
returns the name of the variable
virtual bool registerSlave(Instantiation &slave) override
Register i as a slave of this MultiDimAdressable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66