aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimImplementation.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of gum::MultiDimImplementation.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 #ifndef GUM_MULTI_DIM_IMPLEMENTATION_H
29 #define GUM_MULTI_DIM_IMPLEMENTATION_H
30 
31 #include <vector>
32 
33 #include <agrum/agrum.h>
34 
35 #include <agrum/tools/multidim/implementations/multiDimContainer.h>
36 
37 namespace gum {
38 
39  // ===========================================================================
40  // === GUM_MULTI_DIM_IMPLEMENTATION ===
41  // ===========================================================================
42  /**
43  * @class MultiDimImplementation
44  * @headerfile multiDimImplementation.h
45  *<agrum/tools/multidim/multiDimImplementation.h>
46  * @ingroup multidim_group
47  *
48  * @brief Abstract base class for all multi dimensionnal implementations
49  *
50  * The gum::MultiDimImplementation is an abstract class for all
51  * multidimensional implementation of container of GUM_SCALAR. Its purpose is
52  * to implement base algorithms with no regard to how the storage is done
53  * (tree, matrix...)
54  *
55  * It deals also with variables and implements pure virtual methods concerned
56  * with dimensions.
57  *
58  * See operator<<(std::ostream&, const MultiDimImplementation<GUM_SCALAR>&)
59  * to print a gum::MultiDimImplementation.
60  *
61  * @tparam GUM_SCALAR The type of the scalar stored in this multidimensional
62  * matrix.
63  */
64  template < typename GUM_SCALAR >
65  class MultiDimImplementation: public MultiDimContainer< GUM_SCALAR > {
66  public:
67  // =========================================================================
68  /// @name Constructors / Destructors
69  // =========================================================================
70  /// @{
71 
72  /**
73  * @brief Default constructor.
74  */
76 
77  /**
78  * @brief Copy constructor.
79  * @param from The MultiDimImplementation to copy.
80  */
81  MultiDimImplementation(const MultiDimImplementation< GUM_SCALAR >& from);
82 
83  /**
84  * @brief Class destructor.
85  */
86  virtual ~MultiDimImplementation();
87 
88  /// @}
89  // =========================================================================
90  /// @name Accessors / Modifiers
91  // =========================================================================
92  /// @{
93 
94  /**
95  * @brief Returns the real name of the multiDim implementation
96  *
97  * In aGrUM, all the types of multi-dimensional arrays/functionals have a
98  * name that describes what they are in reality. For instance, a table
99  * stored in extension is a "MultiDimArray", one that stores only non zero
100  * elements is a "MultiDimSparseArray", and so on. These names are unique
101  * for each type of implementation and is used by the system to determine
102  * which is the best functions to use, say, when we wish to use operators
103  * such as operator+ on two MultiDimImplementations.
104  *
105  * @return Returns the real name of the multiDim implementation
106  */
107  virtual const std::string& name() const = 0;
108 
109  /**
110  * @brief Returns the base class name of this MultiDimImplementation.
111  *
112  * This method is used for chosing a proposer operator when no specialized
113  * operator have been defined.
114  */
115  const std::string& basename() const;
116 
117  /**
118  * @brief Returns the real number of parameters used for this table.
119  *
120  * This function is used by the MultiDimImplementation::compressionRate()
121  * method.
122  *
123  * @see MultiDimImplementation::compressionRate()
124  *
125  * @return Returns the real number of parameters used for this table.
126  */
127  virtual Size realSize() const = 0;
128 
129  /**
130  * @brief The compression ratio of the table (depending on the type of
131  * implementation).
132  *
133  * This method uses domainSize() and realSize() to compute the ration,
134  * both methods are virtual and should be surcharge if a subclass has a
135  * special policies about memory management.
136  *
137  * @warning This compression ratio is not exactly the memory compression
138  * ratio. It is computed in terms of number of parameters.
139  *
140  * @return Returns the compression ration of the table.
141  */
142  float compressionRate() const;
143 
144  /// @}
145  // ========================================================================
146  /// @name MultiDimInterface implementation
147  // ========================================================================
148  /// @{
149  virtual Idx nbrDim() const override;
150 
151  virtual Size domainSize() const override;
152 
153  virtual void add(const DiscreteVariable& v) override;
154 
155  virtual void erase(const DiscreteVariable& v) override;
156 
157  virtual const Sequence< const DiscreteVariable* >&
158  variablesSequence() const override;
159 
160  const DiscreteVariable& variable(Idx i) const override;
161  const DiscreteVariable& variable(const std::string& name) const override;
162 
163  virtual Idx pos(const DiscreteVariable& v) const override;
164 
165  virtual bool contains(const DiscreteVariable& v) const override;
166 
167  virtual bool empty() const override;
168 
169  /// @}
170  // =========================================================================
171  /// @name MultiDimAdressable implementation
172  // =========================================================================
173  /// @{
174 
175  virtual bool registerSlave(Instantiation& slave) override;
176 
177  virtual bool unregisterSlave(Instantiation& slave) override;
178 
179  /// @}
180  // =========================================================================
181  /// @name MultiDimContainer implementation
182  // =========================================================================
183  /// @{
184 
185  using MultiDimContainer< GUM_SCALAR >::get;
186 
187  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const override = 0;
188 
190 
192 
193  void endMultipleChanges(const GUM_SCALAR&) override;
194 
195  /// @}
196 
197  protected:
198  // =========================================================================
199  /// @name Fast large modifications in structures
200  // =========================================================================
201  /// @{
202 
203  /**
204  * @brief Synchronize content after MultipleChanges.
205  */
206  virtual void commitMultipleChanges_();
207 
208  /**
209  * @brief Synchronize content after MultipleChanges.
210  * @param value Default value for uninitialized values.
211  */
212  virtual void commitMultipleChanges_(const GUM_SCALAR& value);
213 
214  /**
215  * @brief Get the actual change method of this MultiDimImplementation.
216  * @return Returns true if in multiple changes.
217  */
218  bool isInMultipleChangeMethod_() const;
219 
220  /**
221  * @brief Get the actual state of *this.
222  * @return Returns true if a commit is needed.
223  */
224  bool isCommitNeeded_() const;
225 
226  /**
227  * @brief Returns a constant reference over the list of slaved
228  * instantiations.
229  * @return Returns a constant reference over the list of slaved
230  * instantiations.
231  */
232  const List< Instantiation* >& slaves_() const;
233 
234  /**
235  * @brief Replace variable x by y.
236  *
237  * Technically this should be call by any subclass overloading this method
238  * to proceed with the changes in this class containers.
239  *
240  * @param x The first variable to swap.
241  * @param y The second variable to swap.
242  */
243  virtual void replace_(const DiscreteVariable* x,
244  const DiscreteVariable* y) override;
245 
246  /**
247  * @brief Inverts variables at position p1 and p2
248  *
249  * Call this from subclass when you want to invert position of two
250  * variables in the MultiDimImplementation.
251  *
252  * @param p1 The first position.
253  * @param p2 The second position.
254  */
255  virtual void invert_(Idx p1, Idx p2);
256 
257  /// @}
258 
259  /**
260  * @brief Returns the implementation for this object (may be *this).
261  */
262  virtual const MultiDimImplementation< GUM_SCALAR >* content() const final;
263 
264  /**
265  * @brief Returns the implementation for this object (may be *this).
266  */
267  virtual MultiDimImplementation< GUM_SCALAR >* content() final;
268 
269  private:
270  /// List of discrete variables (dimensions).
272 
273  /// List of instantiations of the tuples (sequences) of variables.
275 
276  /// Used to represent in which change method this MultiDimImplementation is.
277  enum class InternalChangeMethod__ : char
278  {
281  };
282 
283  /// Used to represent in which change state this MultiDimImplementation is.
284  enum class InternalChangeState__ : char
285  {
286  NO_CHANGE,
288  };
289 
290  /// The current change method.
292 
293  /// The current change state.
295 
296  /// This MultiDimImplementation domain size.
298 
299  /**
300  * @brief Change the internalChangeState__ to NOT_COMMITTED_CHANGE.
301  */
302  void setNotCommitedChange__();
303  };
304 
305  /**
306  * @brief For friendly displaying the content of the array.
307  * @ingroup multidim_group
308  */
309  template < typename GUM_SCALAR >
312 
313 } /* namespace gum */
314 
315 #include <agrum/tools/multidim/implementations/multiDimImplementation_tpl.h>
316 
317 #endif /* GUM_MULTI_DIM_IMPLEMENTATION_H */
InternalChangeState__
Used to represent in which change state this MultiDimImplementation is.
float compressionRate() const
The compression ratio of the table (depending on the type of implementation).
InternalChangeState__ internalChangeState__
The current change state.
const List< Instantiation *> & slaves_() const
Returns a constant reference over the list of slaved instantiations.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
const DiscreteVariable & variable(const std::string &name) const override
Returns the variable with the name.
virtual void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
virtual void commitMultipleChanges_()
Synchronize content after MultipleChanges.
void endMultipleChanges(const GUM_SCALAR &) override
Call this method after doing important changes in this MultiDimContainer.
Sequence< const DiscreteVariable *> vars__
List of discrete variables (dimensions).
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual MultiDimImplementation< GUM_SCALAR > * content() final
Returns the implementation for this object (may be *this).
virtual bool unregisterSlave(Instantiation &slave) override
Unregister i as a slave of this MultiDimAdressable.
virtual bool empty() const override
Returns true if no var is in *this.
InternalChangeMethod__ internalChangeMethod__
The current change method.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
virtual void commitMultipleChanges_(const GUM_SCALAR &value)
Synchronize content after MultipleChanges.
MultiDimImplementation()
Default constructor.
List< Instantiation *> slaveInstantiations__
List of instantiations of the tuples (sequences) of variables.
virtual void invert_(Idx p1, Idx p2)
Inverts variables at position p1 and p2.
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.
Size domainSize__
This MultiDimImplementation domain size.
MultiDimImplementation(const MultiDimImplementation< GUM_SCALAR > &from)
Copy constructor.
virtual Size realSize() const =0
Returns the real number of parameters used for this table.
std::ostream & operator<<(std::ostream &, const MultiDimImplementation< GUM_SCALAR > &)
For friendly displaying the content of the array.
virtual void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
virtual ~MultiDimImplementation()
Class destructor.
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*.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
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.
void setNotCommitedChange__()
Change the internalChangeState__ to NOT_COMMITTED_CHANGE.
InternalChangeMethod__
Used to represent in which change method this MultiDimImplementation is.
virtual Idx pos(const DiscreteVariable &v) const override
Returns the index of a variable.
bool isCommitNeeded_() const
Get the actual state of *this.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual bool contains(const DiscreteVariable &v) const override
Returns true if var is in *this.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const override=0
Creates an empty clone of this MultiDimContainer.
const std::string & basename() const
Returns the base class name of this MultiDimImplementation.
virtual bool registerSlave(Instantiation &slave) override
Register i as a slave of this MultiDimAdressable.