aGrUM  0.14.2
multiDimBucket.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  ***************************************************************************/
28 #ifndef GUM_MULTIDIM_BUCKET_H
29 #define GUM_MULTIDIM_BUCKET_H
30 
31 #include <climits>
32 #include <sstream>
33 
34 #include <agrum/core/bijection.h>
35 #include <agrum/core/hashTable.h>
36 #include <agrum/core/sequence.h>
37 #include <agrum/core/set.h>
38 
41 
42 namespace gum {
43 
71  template < typename GUM_SCALAR >
72  class MultiDimBucket : public MultiDimReadOnly< GUM_SCALAR > {
73  public:
74  // ========================================================================
76  // ========================================================================
78 
87  explicit MultiDimBucket(Size bufferSize = INT_MAX);
88 
94 
98  virtual ~MultiDimBucket();
99 
101  // ========================================================================
103  // ========================================================================
105 
111  void add(const MultiDimContainer< GUM_SCALAR >& impl);
112 
118  void add(const MultiDimContainer< GUM_SCALAR >* impl);
119 
124  void erase(const MultiDimContainer< GUM_SCALAR >& impl);
125 
130  void erase(const MultiDimContainer< GUM_SCALAR >* impl);
131 
137  bool contains(const MultiDimContainer< GUM_SCALAR >& impl) const;
138 
146 
151  Size bucketSize() const;
152 
157  bool isBucketEmpty() const;
158 
165  bool bucketChanged() const;
166 
172  const MultiDimArray< GUM_SCALAR >& bucket() const;
173 
178  Size bufferSize() const;
179 
189  void setBufferSize(Size amount);
190 
210  void compute(bool force = false) const;
211 
213  // ========================================================================
215  // ========================================================================
217 
218  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const override;
219 
220  const std::string& name() const override;
221 
222  virtual void add(const DiscreteVariable& v) override;
223 
224  virtual void erase(const DiscreteVariable& v) override;
225 
226  virtual Size realSize() const override;
227 
228  bool contains(const DiscreteVariable& v) const override;
229 
230  virtual GUM_SCALAR get(const Instantiation& i) const override;
231 
232  virtual void changeNotification(const Instantiation& i,
233  const DiscreteVariable* const var,
234  Idx oldval,
235  Idx newval) override;
236 
237  virtual void setFirstNotification(const Instantiation& i) override;
238 
239  virtual void setLastNotification(const Instantiation& i) override;
240 
241  virtual void setIncNotification(const Instantiation& i) override;
242 
243  virtual void setDecNotification(const Instantiation& i) override;
244 
245  virtual void setChangeNotification(const Instantiation& i) override;
246 
247  virtual bool registerSlave(Instantiation& i) override;
248 
249  virtual bool unregisterSlave(Instantiation& i) override;
250 
251  virtual MultiDimAdressable& getMasterRef() override;
252 
253  virtual const MultiDimAdressable& getMasterRef() const override;
254 
255  virtual const std::string toString(const Instantiation* i) const override;
256 
258 
264  multidims() const;
265 
266  protected:
267  virtual void _commitMultipleChanges() override;
268 
273  virtual GUM_SCALAR& _get(const Instantiation& i) const override;
274 
275  virtual void _replace(const DiscreteVariable* x,
276  const DiscreteVariable* y) override;
277 
278  private:
281 
285 
288 
292 
295 
298 
305  void __addVariable(const DiscreteVariable* var);
306 
313  void __eraseVariable(const DiscreteVariable* var);
314 
320  void __initializeBuffer();
321 
325  void __eraseBuffer();
326 
336  GUM_SCALAR __computeValue(const Instantiation& value) const;
337 
340  mutable bool __changed;
341 
345 
347  std::string __name;
348  };
349 
350 
351 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
352  extern template class MultiDimBucket< double >;
353 #endif
354 
355 
356 } /* namespace gum */
357 
359 
360 #endif /* GUM_MULTIDIM_BUCKET_H */
virtual bool unregisterSlave(Instantiation &i) override
Unregister i as a slave of this MultiDimAdressable.
virtual void setDecNotification(const Instantiation &i) override
Listen to increment in each recorded Instantiation.
Size __bufferSize
The number of element allowed in __bucket.
Instantiation __allVarsInst
Instantiation over all variable in this.
MultiDimArray< GUM_SCALAR > * __bucket
The result table of this bucket.
Implementation of the MultiDimBucket class.
Header of the MultiDimReadOnly class.
HashTable< const MultiDimContainer< GUM_SCALAR > *, Instantiation *> __multiDims
The set of MultiDimContainer in this bucket.
Header file of gum::Sequence, a class for storing (ordered) sequences of objects. ...
virtual void setChangeNotification(const Instantiation &i) override
Listen to an assignment of a value in a Instantiation.
Sets of elements (i.e.
std::string __name
The class name.
virtual Size realSize() const override
Returns the real number of parameters used for this table.
bool isBucketEmpty() const
Returns true if this bucket is empty.
virtual const std::string toString() const
Returns a representation of this MultiDimContainer.
A multidim implementation for buckets.
HashTable< const Instantiation *, GUM_SCALAR > __slavesValue
This table is used to keep the last value computed for an instantiation when the value are computed o...
GUM_SCALAR __computeValue(const Instantiation &value) const
Compute the value of the final table of this bucket given i.
Base class for discrete random variable.
bool contains(const MultiDimContainer< GUM_SCALAR > &impl) const
Returns true if the MultiDimContainer is in this bucket.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const override
Default constructor.
Abstract base class for all multi dimensionnal read only structure.
The class for generic Hash Tables.
Definition: hashTable.h:676
virtual ~MultiDimBucket()
Destructor.
void __addVariable(const DiscreteVariable *var)
Add a variable to __allVariables, and do nothing if var is already in the set.
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
void __initializeBuffer()
Initialize the internal buffer.
void __eraseBuffer()
Clean the buffer and switch it&#39;s instantiation to this bucket.
void erase(const MultiDimContainer< GUM_SCALAR > &impl)
Remove a MultiDimContainer from this bucket.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) override
Listen to changes in a given Instantiation.
virtual void setLastNotification(const Instantiation &i) override
Listen to setLast in a given Instantiation.
const MultiDimArray< GUM_SCALAR > & bucket() const
Returns the MultiDimArray used by this MultiDimBucket.
void __eraseVariable(const DiscreteVariable *var)
Erase a variable from __allVariables if no other multidimensional table uses it in this bucket...
Size bucketSize() const
Returns the number of MultiDimContainer in in this bukcet.
MultiDimBucket(Size bufferSize=INT_MAX)
Default constructor.
virtual GUM_SCALAR & _get(const Instantiation &i) const override
void compute(bool force=false) const
This method computes the final table of this bucket.
virtual void setIncNotification(const Instantiation &i) override
Listen to increment in a given Instantiation.
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:51
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
Bijection< Instantiation *, Instantiation *> __instantiations
Bijection between instantiations registered on this and their equivalent on __bucket.
bool bucketChanged() const
Returns true if the bucket need re-computation since the last computation.
bool __changed
Flag used to know if changes has occurred in the bucket since last computation.
virtual void setFirstNotification(const Instantiation &i) override
Listen to setFirst in a given Instantiation.
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual void _commitMultipleChanges() override
Synchronize content after MultipleChanges.
Set< const DiscreteVariable *> __allVariables
The set of all variables of the multidims in this bucket.
Size bufferSize() const
Returns the amount of memory allowed for this bucket.
virtual bool registerSlave(Instantiation &i) override
Register i as a slave of this MultiDimAdressable.
Size Idx
Type for indexes.
Definition: types.h:50
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Header of the MultiDimArray class.
void setBufferSize(Size amount)
Changes the amount of memory allowed for this bucket.
const Set< const DiscreteVariable *> & allVariables() const
Returns the sequence of all the variables contained in the bucket.
const HashTable< const MultiDimContainer< GUM_SCALAR > *, Instantiation *> & multidims() const
Returns the MultiDimContainer and their respective Instantiation.
const std::string & name() const override
Returns the real name of the multiDim implementation.
virtual MultiDimAdressable & getMasterRef() override
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
void add(const MultiDimContainer< GUM_SCALAR > &impl)
Add a MultiDimContainer in the bucket.
Class hash tables iterators.
Set of pairs of elements with fast search for both elements.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.