aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimDecorator.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 for MultiDimDecorator.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 #ifndef GUM_MULTI_DIM_DECORATOR_H
29 #define GUM_MULTI_DIM_DECORATOR_H
30 
31 #include <agrum/agrum.h>
32 
33 #include <agrum/tools/multidim/implementations/multiDimArray.h>
34 
35 namespace gum {
36 
37  // ===========================================================================
38  // === MULTIDIM DECORATOR ===
39  // ===========================================================================
40 
41  /**
42  * @class MultiDimDecorator
43  * @headerfile multiDimDecorator.h <agrum/tools/multidim/multiDimDecorator.h>
44  * @ingroup multidim_group
45  *
46  * @brief Decorator design pattern in order to separate implementations from
47  * multidimensional matrix concepts.
48  *
49  * A MultiDimDecorator is a virtual class for all encapsulation of
50  * MultiDimImplementation, for example probability, utility, etc. It
51  * implements a decorator design pattern in order to have a array, tree,
52  * sparse or matrix (...) implementation of MultiDimImplementation.
53  *
54  * @tparam GUM_SCALAR The type of the scalar stored in this multidimensional
55  * matrix.
56  */
57  template < typename GUM_SCALAR >
59  public:
60  // =========================================================================
61  /// @name Constructors / Destructors
62  // =========================================================================
63  /// @{
64 
65  /**
66  * @brief Class constructor.
67  * @param aContent The implementation used by this MultiDimDecorator.
68  */
69  MultiDimDecorator(MultiDimImplementation< GUM_SCALAR >* aContent = nullptr,
70  GUM_SCALAR empty_value = (GUM_SCALAR)0);
71 
72  /**
73  * @brief copy constructor & assignment
74  */
75  MultiDimDecorator(const MultiDimDecorator< GUM_SCALAR >& from);
76 
77  /**
78  * @brief copy operator
79  */
80  MultiDimDecorator< GUM_SCALAR >& operator=(const MultiDimDecorator& from) noexcept;
81 
82  /**
83  * @brief Class move constructor
84  */
85  MultiDimDecorator(MultiDimDecorator< GUM_SCALAR >&&) noexcept;
86 
87  /**
88  * @brief assignment operator.
89  */
90  MultiDimDecorator< GUM_SCALAR >& operator=(MultiDimDecorator&& from);
91 
92  /**
93  * @brief Class destructor.
94  */
96 
97  /// @}
98  // =========================================================================
99  /// @name MultiDimInterface implementation
100  // =========================================================================
101  /// @{
102 
103  virtual Idx nbrDim() const final;
104 
105  virtual Size domainSize() const final;
106 
107  virtual void add(const DiscreteVariable& v) final;
108 
109  virtual void erase(const DiscreteVariable& var) final;
110  virtual void erase(const std::string& name) final;
111 
112  virtual const Sequence< const DiscreteVariable* >& variablesSequence() const final;
113  virtual const DiscreteVariable& variable(Idx) const final;
114  virtual const DiscreteVariable& variable(const std::string& name) const final;
115 
116  virtual Idx pos(const DiscreteVariable& var) const final;
117 
118  virtual bool contains(const DiscreteVariable& var) const final;
119 
120  virtual bool empty() const final;
121 
122  /// @}
123  // ========================================================================
124  /// @name MultiDimAddressable implementation
125  // ========================================================================
126  /// @{
127 
128  virtual bool unregisterSlave(Instantiation& i) final;
129 
130  virtual bool registerSlave(Instantiation& i) final;
131 
132  virtual void changeNotification(const Instantiation& i,
133  const DiscreteVariable* const var,
134  Idx oldval,
135  Idx newval) final;
136 
137  virtual void setChangeNotification(const Instantiation& i) final;
138 
139  virtual void setFirstNotification(const Instantiation& i) final;
140 
141  virtual void setLastNotification(const Instantiation& i) final;
142 
143  virtual void setIncNotification(const Instantiation& i) final;
144 
145  virtual void setDecNotification(const Instantiation& i) final;
146 
147  virtual void notifyChange() const final;
148 
149  /// @}
150  // =========================================================================
151  /// @name MultiDimContainer implementation
152  // =========================================================================
153  /// @{
154 
155  /**
156  * @brief Default implementation of MultiDimContainer::set().
157  *
158  * Calls get_ as a r-value.
159  */
160  virtual void set(const Instantiation& i, const GUM_SCALAR& value) const final;
161 
162  /**
163  * @brief Default implementation of MultiDimContainer::get().
164  *
165  * Calls get_ as a l-value.
166  */
167  virtual GUM_SCALAR get(const Instantiation& i) const final;
168 
169  virtual void fill(const GUM_SCALAR& d) const final;
170 
171  /**
172  * @brief Automatically fills this MultiDimContainer with the values in
173  * v.
174  *
175  * The order used to fill this MultiDimContainer is the same as with an
176  * instantiation over it.
177  * @code
178  * Size cpt = 0;
179  * Instantiation i( *this );
180  * for (i.setFirst(); !i.end(); ++i, ++cpt) {
181  * set(i, v[cpt]);
182  * }
183  * @endcode
184  *
185  * @param v Vector of values.
186  * @throw SizeError Raised if v size's does not matches this
187  * MultiDimContainer domain size.
188  */
189  virtual void populate(const std::vector< GUM_SCALAR >& v) const final;
190 
191  /**
192  * @brief Apply a function on every element of the container
193  * @param f the function to apply
194  */
195  virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final;
196 
197  /**
198  * @brief compute lfold for this container
199  * @param f the function to apply
200  * @param base the initial value
201  */
202  virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
203  GUM_SCALAR base) const final;
204 
205  virtual MultiDimDecorator< GUM_SCALAR >* newFactory() const = 0;
206 
207  virtual void beginMultipleChanges() final;
208 
209  virtual void endMultipleChanges() final;
210 
211  virtual void endMultipleChanges(const GUM_SCALAR&) final;
212 
213  virtual std::string toString(const Instantiation* i) const;
214 
215  virtual std::string toString() const;
216 
217  /// @}
218  // =========================================================================
219  /// @name Various methods.
220  // =========================================================================
221  /// @{
222 
223 
224  /**
225  * @brief Returns the implementation for this object (may be *this).
226  */
227  virtual const MultiDimImplementation< GUM_SCALAR >* content() const final;
228 
229  /**
230  * @brief Returns the implementation for this object (may be *this).
231  */
232  virtual MultiDimImplementation< GUM_SCALAR >* content() final;
233 
234  /// @}
235  protected:
236  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
237 
238  /**
239  * protecte method to swap the implementation behind the Potential
240  * @warning unsafe method for slave Instantiations !
241  */
242  void swapContent_(MultiDimImplementation< GUM_SCALAR >* aContent) const;
243 
244  /**
245  * The true container.
246  */
248 
249  /**
250  * Return a data, given a Insantiation - final method.
251  * @param i The instantiation.
252  * @throw NullElement
253  * @throw NotFound
254  */
255  GUM_SCALAR& get_(const Instantiation& i) const final;
256 
257  /**
258  * value of the MultiDimDecorator if no dimension.
259  *
260  */
261  mutable GUM_SCALAR empty_value_;
262  };
263 
264 } /* namespace gum */
265 
266 #include <agrum/tools/multidim/implementations/multiDimDecorator_tpl.h>
267 
268 #endif /* GUM_MULTI_DIM_DECORATOR_H */
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const final
Returns the product of the variables domain size.
virtual bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y...
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
virtual void erase(const std::string &name) final
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
MultiDimDecorator(MultiDimDecorator< GUM_SCALAR > &&) noexcept
Class move constructor.
virtual MultiDimDecorator< GUM_SCALAR > * newFactory() const =0
Default implementation of MultiDimContainer::set().
virtual const DiscreteVariable & variable(const std::string &name) const final
Returns the variable with the name.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
virtual void endMultipleChanges(const GUM_SCALAR &) final
Default implementation of MultiDimContainer::set().
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.
virtual bool empty() const final
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 populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
virtual void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Class constructor.
virtual void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
virtual bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.
MultiDimDecorator(const MultiDimDecorator< GUM_SCALAR > &from)
copy constructor & assignment
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
virtual void notifyChange() const final
GUM_SCALAR & get_(const Instantiation &i) const final
Return a data, given a Insantiation - final method.
void swapContent_(MultiDimImplementation< GUM_SCALAR > *aContent) const
protecte method to swap the implementation behind the Potential
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
virtual void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.
virtual void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
Decorator design pattern in order to separate implementations from multidimensional matrix concepts...
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
virtual MultiDimImplementation< GUM_SCALAR > * content() final
Returns the implementation for this object (may be *this).
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
~MultiDimDecorator()
Class destructor.
virtual std::string toString() const
Default implementation of MultiDimContainer::set().
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
MultiDimDecorator< GUM_SCALAR > & operator=(MultiDimDecorator &&from)
assignment operator.
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
virtual std::string toString(const Instantiation *i) const
Default implementation of MultiDimContainer::set().