aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimDecorator.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 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 >&
81  operator=(const MultiDimDecorator& from) noexcept;
82 
83  /**
84  * @brief Class move constructor
85  */
86  MultiDimDecorator(MultiDimDecorator< GUM_SCALAR >&&) noexcept;
87 
88  /**
89  * @brief assignment operator.
90  */
91  MultiDimDecorator< GUM_SCALAR >& operator=(MultiDimDecorator&& from);
92 
93  /**
94  * @brief Class destructor.
95  */
97 
98  /// @}
99  // =========================================================================
100  /// @name MultiDimInterface implementation
101  // =========================================================================
102  /// @{
103 
104  virtual Idx nbrDim() const final;
105 
106  virtual Size domainSize() const final;
107 
108  virtual void add(const DiscreteVariable& v) final;
109 
110  virtual void erase(const DiscreteVariable& var) final;
111  virtual void erase(const std::string& name) final;
112 
113  virtual const Sequence< const DiscreteVariable* >&
114  variablesSequence() const final;
115  virtual const DiscreteVariable& variable(Idx) const final;
116  virtual const DiscreteVariable& variable(const std::string& name) const final;
117 
118  virtual Idx pos(const DiscreteVariable& var) const final;
119 
120  virtual bool contains(const DiscreteVariable& var) const final;
121 
122  virtual bool empty() const final;
123 
124  /// @}
125  // ========================================================================
126  /// @name MultiDimAddressable implementation
127  // ========================================================================
128  /// @{
129 
130  virtual bool unregisterSlave(Instantiation& i) final;
131 
132  virtual bool registerSlave(Instantiation& i) final;
133 
134  virtual void changeNotification(const Instantiation& i,
135  const DiscreteVariable* const var,
136  Idx oldval,
137  Idx newval) final;
138 
139  virtual void setChangeNotification(const Instantiation& i) final;
140 
141  virtual void setFirstNotification(const Instantiation& i) final;
142 
143  virtual void setLastNotification(const Instantiation& i) final;
144 
145  virtual void setIncNotification(const Instantiation& i) final;
146 
147  virtual void setDecNotification(const Instantiation& i) final;
148 
149  virtual void notifyChange() const final;
150 
151  /// @}
152  // =========================================================================
153  /// @name MultiDimContainer implementation
154  // =========================================================================
155  /// @{
156 
157  /**
158  * @brief Default implementation of MultiDimContainer::set().
159  *
160  * Calls get_ as a r-value.
161  */
162  virtual void set(const Instantiation& i, const GUM_SCALAR& value) const final;
163 
164  /**
165  * @brief Default implementation of MultiDimContainer::get().
166  *
167  * Calls get_ as a l-value.
168  */
169  virtual GUM_SCALAR get(const Instantiation& i) const final;
170 
171  virtual void fill(const GUM_SCALAR& d) const final;
172 
173  /**
174  * @brief Automatically fills this MultiDimContainer with the values in
175  * v.
176  *
177  * The order used to fill this MultiDimContainer is the same as with an
178  * instantiation over it.
179  * @code
180  * Size cpt = 0;
181  * Instantiation i( *this );
182  * for (i.setFirst(); !i.end(); ++i, ++cpt) {
183  * set(i, v[cpt]);
184  * }
185  * @endcode
186  *
187  * @param v Vector of values.
188  * @throw SizeError Raised if v size's does not matches this
189  * MultiDimContainer domain size.
190  */
191  virtual void populate(const std::vector< GUM_SCALAR >& v) const final;
192 
193  /**
194  * @brief Apply a function on every element of the container
195  * @param f the function to apply
196  */
197  virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final;
198 
199  /**
200  * @brief compute lfold for this container
201  * @param f the function to apply
202  * @param base the initial value
203  */
204  virtual GUM_SCALAR
205  reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
206  GUM_SCALAR base) const final;
207 
208  virtual MultiDimDecorator< GUM_SCALAR >* newFactory() const = 0;
209 
210  virtual void beginMultipleChanges() final;
211 
212  virtual void endMultipleChanges() final;
213 
214  virtual void endMultipleChanges(const GUM_SCALAR&) final;
215 
216  virtual std::string toString(const Instantiation* i) const;
217 
218  virtual std::string toString() const;
219 
220  /// @}
221  // =========================================================================
222  /// @name Various methods.
223  // =========================================================================
224  /// @{
225 
226 
227  /**
228  * @brief Returns the implementation for this object (may be *this).
229  */
230  virtual const MultiDimImplementation< GUM_SCALAR >* content() const final;
231 
232  /**
233  * @brief Returns the implementation for this object (may be *this).
234  */
235  virtual MultiDimImplementation< GUM_SCALAR >* content() final;
236 
237  /// @}
238  protected:
239  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
240 
241  /**
242  * protecte method to swap the implementation behind the Potential
243  * @warning unsafe method for slave Instantiations !
244  */
245  void swapContent_(MultiDimImplementation< GUM_SCALAR >* aContent) const;
246 
247  /**
248  * The true container.
249  */
251 
252  /**
253  * Return a data, given a Insantiation - final method.
254  * @param i The instantiation.
255  * @throw NullElement
256  * @throw NotFound
257  */
258  GUM_SCALAR& get_(const Instantiation& i) const final;
259 
260  /**
261  * value of the MultiDimDecorator if no dimension.
262  *
263  */
264  mutable GUM_SCALAR empty_value_;
265  };
266 
267 } /* namespace gum */
268 
269 #include <agrum/tools/multidim/implementations/multiDimDecorator_tpl.h>
270 
271 #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:669
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().