aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimImplementation.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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* >& variablesSequence() const override;
158 
159  const DiscreteVariable& variable(Idx i) const override;
160  const DiscreteVariable& variable(const std::string& name) const override;
161 
162  virtual Idx pos(const DiscreteVariable& v) const override;
163 
164  virtual bool contains(const DiscreteVariable& v) const override;
165 
166  virtual bool empty() const override;
167 
168  /// @}
169  // =========================================================================
170  /// @name MultiDimAdressable implementation
171  // =========================================================================
172  /// @{
173 
174  virtual bool registerSlave(Instantiation& slave) override;
175 
176  virtual bool unregisterSlave(Instantiation& slave) override;
177 
178  /// @}
179  // =========================================================================
180  /// @name MultiDimContainer implementation
181  // =========================================================================
182  /// @{
183 
184  using MultiDimContainer< GUM_SCALAR >::get;
185 
186  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const override = 0;
187 
189 
191 
192  void endMultipleChanges(const GUM_SCALAR&) override;
193 
194  /// @}
195 
196  protected:
197  // =========================================================================
198  /// @name Fast large modifications in structures
199  // =========================================================================
200  /// @{
201 
202  /**
203  * @brief Synchronize content after MultipleChanges.
204  */
205  virtual void commitMultipleChanges_();
206 
207  /**
208  * @brief Synchronize content after MultipleChanges.
209  * @param value Default value for uninitialized values.
210  */
211  virtual void commitMultipleChanges_(const GUM_SCALAR& value);
212 
213  /**
214  * @brief Get the actual change method of this MultiDimImplementation.
215  * @return Returns true if in multiple changes.
216  */
217  bool isInMultipleChangeMethod_() const;
218 
219  /**
220  * @brief Get the actual state of *this.
221  * @return Returns true if a commit is needed.
222  */
223  bool isCommitNeeded_() const;
224 
225  /**
226  * @brief Returns a constant reference over the list of slaved
227  * instantiations.
228  * @return Returns a constant reference over the list of slaved
229  * instantiations.
230  */
231  const List< Instantiation* >& slaves_() const;
232 
233  /**
234  * @brief Replace variable x by y.
235  *
236  * Technically this should be call by any subclass overloading this method
237  * to proceed with the changes in this class containers.
238  *
239  * @param x The first variable to swap.
240  * @param y The second variable to swap.
241  */
242  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y) override;
243 
244  /**
245  * @brief Inverts variables at position p1 and p2
246  *
247  * Call this from subclass when you want to invert position of two
248  * variables in the MultiDimImplementation.
249  *
250  * @param p1 The first position.
251  * @param p2 The second position.
252  */
253  virtual void invert_(Idx p1, Idx p2);
254 
255  /// @}
256 
257  /**
258  * @brief Returns the implementation for this object (may be *this).
259  */
260  virtual const MultiDimImplementation< GUM_SCALAR >* content() const final;
261 
262  /**
263  * @brief Returns the implementation for this object (may be *this).
264  */
265  virtual MultiDimImplementation< GUM_SCALAR >* content() final;
266 
267  private:
268  /// List of discrete variables (dimensions).
270 
271  /// List of instantiations of the tuples (sequences) of variables.
273 
274  /// Used to represent in which change method this MultiDimImplementation is.
275  enum class _InternalChangeMethod_ : char
276  {
279  };
280 
281  /// Used to represent in which change state this MultiDimImplementation is.
282  enum class _InternalChangeState_ : char
283  {
284  NO_CHANGE,
286  };
287 
288  /// The current change method.
290 
291  /// The current change state.
293 
294  /// This MultiDimImplementation domain size.
296 
297  /**
298  * @brief Change the _internalChangeState_ to NOT_COMMITTED_CHANGE.
299  */
300  void _setNotCommitedChange_();
301  };
302 
303  /**
304  * @brief For friendly displaying the content of the array.
305  * @ingroup multidim_group
306  */
307  template < typename GUM_SCALAR >
309 
310 } /* namespace gum */
311 
312 #include <agrum/tools/multidim/implementations/multiDimImplementation_tpl.h>
313 
314 #endif /* GUM_MULTI_DIM_IMPLEMENTATION_H */
float compressionRate() const
The compression ratio of the table (depending on the type of implementation).
const List< Instantiation *> & slaves_() const
Returns a constant reference over the list of slaved instantiations.
_InternalChangeMethod_ _internalChangeMethod_
The current change method.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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.
void _setNotCommitedChange_()
Change the internalChangeState to NOT_COMMITTED_CHANGE.
virtual void commitMultipleChanges_()
Synchronize content after MultipleChanges.
void endMultipleChanges(const GUM_SCALAR &) override
Call this method after doing important changes in this MultiDimContainer.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
_InternalChangeState_
Used to represent in which change state this MultiDimImplementation is.
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).
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.
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.
_InternalChangeMethod_
Used to represent in which change method this MultiDimImplementation is.
MultiDimImplementation()
Default constructor.
Size _domainSize_
This MultiDimImplementation domain size.
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.
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.
_InternalChangeState_ _internalChangeState_
The current change state.
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.
List< Instantiation *> _slaveInstantiations_
List of instantiations of the tuples (sequences) of variables.
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.