aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimWithOffset.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 the MultiDimWithOffset class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 #ifndef GUM_MULTI_DIM_WITH_OFFSET_H
30 #define GUM_MULTI_DIM_WITH_OFFSET_H
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
34 #include <vector>
35 
36 namespace gum {
37  // ==========================================================================
38  // === GUM_MULTI_DIM_WITH_OFFSET === */
39  // ==========================================================================
40  /**
41  * @class MultiDimWithOffset
42  * @headerfile multiDimWithOffset.h <agrum/tools/multidim/multiDimWithOffset.h>
43  * @brief Abstract class for Multidimensional matrix stored as an array in
44  * memory and with an offset associated with each slave instantiation.
45  *
46  * @ingroup multidim_group
47  * @tparam GUM_SCALAR The type of scalar stored in the multidimensional
48  * table.
49  */
50  template < typename GUM_SCALAR >
52  public:
53  // =========================================================================
54  /// @name Constructors, destructor and copy.
55  // =========================================================================
56  /// @{
57 
58  /**
59  * @brief Class constructor.
60  */
62 
63  /**
64  * @brief Copy constructor.
65  *
66  * The newly created matrix contains the same variables but no
67  * instantiation is associated to it.
68  *
69  * @param from The MultiDimWithOffset to copy.
70  */
71  MultiDimWithOffset(const MultiDimWithOffset< GUM_SCALAR >& from);
72 
73  /**
74  * @brief Class destrucor.
75  */
76  virtual ~MultiDimWithOffset();
77 
78  /**
79  * Copy operator.
80  *
81  * @param from The multidimensional matrix we copy into this.
82  */
83  MultiDimWithOffset< GUM_SCALAR >& operator=(const MultiDimWithOffset< GUM_SCALAR >& from);
84 
85  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const = 0;
86 
87  /// @}
88  // =========================================================================
89  /// @name Accessors and modifiers
90  // =========================================================================
91  /// @{
92 
93  virtual void add(const DiscreteVariable& v);
94 
95  virtual void erase(const DiscreteVariable& v);
96 
97  virtual void fill(const GUM_SCALAR& d) const = 0;
98 
99  virtual std::string toString(const Instantiation* i) const;
100 
101  virtual void changeNotification(const Instantiation& i,
102  const DiscreteVariable* const var,
103  Idx oldval,
104  Idx newval);
105 
106  virtual void setChangeNotification(const Instantiation& i);
107 
108  virtual void setFirstNotification(const Instantiation& i);
109 
110  virtual void setLastNotification(const Instantiation& i);
111 
112  void setIncNotification(const Instantiation& i);
113 
114  void setDecNotification(const Instantiation& i);
115 
116  virtual bool registerSlave(Instantiation& i);
117 
118  virtual bool unregisterSlave(Instantiation& i);
119 
120  /// @}
121  // =========================================================================
122  /// @name Access to offset in MultiDimWithOffset
123  // =========================================================================
124  ///@{
125 
126  ///
127  /**
128  * @brief Compute offset from an Instantiation (in this array).
129  *
130  * If the instantiation is not fully compatible with the MultiDimWithOffset,
131  * no exception will be thrown
132  * but 0 is assumed for dimensions not present in the instantiation.
133  * for instance : M<<a<<b<<c; with i=b:1|c:2|d:1 then M.toOffset(i) give the
134  * offset of a:0|b:1|c:2.
135  */
136  Size toOffset(const Instantiation& i) const;
137 
138  /**
139  * @brief Set the Instantiation to the values corresponding to the offset
140  * (in this array).
141  *
142  * We assume that the given instantiation already contains all the variables
143  * contained in the multidimarray (if V is the set of variables of this tab,
144  * V must be a subset of variables in result or the exact set).
145  *
146  * @param i The instantiation i modified giving the offset.
147  * @param offset The offset used to compute the value of i.
148  * @return Returns a reference over i.
149  */
151 
152  ///@}
153 
154  protected:
155  /**
156  * @brief Compute the offset of a Instantiation.
157  *
158  * If the instantiation is not fully compatible with the MultiDimWithOffset,
159  * no exception thrown
160  * but 0 is assumed for dimensions not present in the instantiation.
161  *
162  * for instance : M<<a<<b<<c; with i=b:1|c:2|d:1 then M.getOffs_(i) give the
163  * offset of a:0|b:1|c:2.
164  *
165  * @param i An instantiation for which the offset is computed.
166  * @return The offset of i.
167  */
168  Size getOffs_(const Instantiation& i) const;
169 
170  /**
171  * @brief The gaps between consecutive values of a given variable.
172  *
173  * For each variable, we keep track of the interval between two consecutive
174  * values of the variable in vector values. Thus, shifting from one value
175  * to the next one is equivalent to incrementing/decrementing by gaps the
176  * current offset w.r.t. vector values.
177  */
179 
180  /// The position in the array of each slave Instantiation.
182 
183  /**
184  * @brief For a given index of a value in the vector values, this method
185  * computes the corresponding instantiation.
186  *
187  * @param result the result of this methods, we assume that the given
188  * instantiation already contains all the variables contained in the
189  * MultiDimArray (if V is the set of variables of this tab, V must be a
190  * subset of variables in result or the exact set)
191  * @param indice The index in the vector values
192  */
193  void computeInstantiationValue_(Instantiation& result, Size indice) const;
194 
195  virtual GUM_SCALAR& get_(const Instantiation& i) const = 0;
196  };
197 } /* namespace gum */
198 
199 #include <agrum/tools/multidim/implementations/multiDimWithOffset_tpl.h>
200 
201 #endif /* GUM_MULTI_DIM_WITH_OFFSET_H */
virtual void setLastNotification(const Instantiation &i)
Listen to setLast in a given Instantiation.
Instantiation & fromOffset(Instantiation &i, Size offset) const
Set the Instantiation to the values corresponding to the offset (in this array).
void setDecNotification(const Instantiation &i)
Listen to increment in each recorded Instantiation.
Size toOffset(const Instantiation &i) const
Compute offset from an Instantiation (in this array).
virtual bool unregisterSlave(Instantiation &i)
Unregister i as a slave of this MultiDimAdressable.
virtual void fill(const GUM_SCALAR &d) const =0
Fill the table with d.
virtual std::string toString(const Instantiation *i) const
Display the internal representation of i.
virtual void setChangeNotification(const Instantiation &i)
Listen to an assignment of a value in a Instantiation.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
void computeInstantiationValue_(Instantiation &result, Size indice) const
For a given index of a value in the vector values, this method computes the corresponding instantiati...
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)
Listen to changes in a given Instantiation.
MultiDimWithOffset< GUM_SCALAR > & operator=(const MultiDimWithOffset< GUM_SCALAR > &from)
Copy operator.
virtual ~MultiDimWithOffset()
Class destrucor.
HashTable< const DiscreteVariable *, Size > gaps_
The gaps between consecutive values of a given variable.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
Abstract class for Multidimensional matrix stored as an array in memory and with an offset associated...
MultiDimWithOffset()
Class constructor.
void setIncNotification(const Instantiation &i)
Listen to increment in a given Instantiation.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const =0
Class constructor.
virtual void setFirstNotification(const Instantiation &i)
Listen to setFirst in a given Instantiation.
MultiDimWithOffset(const MultiDimWithOffset< GUM_SCALAR > &from)
Copy constructor.
virtual GUM_SCALAR & get_(const Instantiation &i) const =0
Return a data, given a Instantiation.
virtual bool registerSlave(Instantiation &i)
Register i as a slave of this MultiDimAdressable.