aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimInterface.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 MultiDimInterface.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 #ifndef GUM_MULTIDIM_INTERFACE_H
30 #define GUM_MULTIDIM_INTERFACE_H
31 
32 #include <iostream>
33 #include <vector>
34 
35 #include <agrum/agrum.h>
36 
37 #include <agrum/tools/core/list.h>
38 #include <agrum/tools/core/sequence.h>
39 
40 #include <agrum/tools/variables/discreteVariable.h>
41 
42 namespace gum {
43 
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45  // Forward declaration.
46  class Instantiation;
47 #endif
48 
49  // ==========================================================================
50  // === MULTIDIM INTERFACE ===
51  // ==========================================================================
52  /**
53  * @class MultiDimInterface
54  * @headerfile multiDimInterface.h <agrum/tools/multidim/multiDimInterface.h>
55  * @ingroup multidim_group
56  *
57  * @brief Interface for all classes addressing in a multiDim fashion.
58  *
59  * This class is a base interface for Instantiation and for all classes
60  * addressed by a Instantiation (mainly: MultiDimContainer and hence for all
61  * dimensionnal containers). Its purpose is to deal with the dimensions.
62  *
63  */
64  class MultiDimInterface {
65  public:
66  /**
67  * @brief Class destructor.
68  * @warning No constructor : interface only.
69  */
70  virtual ~MultiDimInterface(){};
71 
72  // =========================================================================
73  /// @name Accessors / Modifiers
74  // =========================================================================
75  /// @{
76 
77  /**
78  * @brief Returns the number of vars in the multidimensional container.
79  * @return Returns the number of vars in the multidimensional container.
80  */
81  virtual Idx nbrDim() const = 0;
82 
83  /**
84  * @brief Returns the product of the variables domain size.
85  * @return Returns the product of the variables domain size.
86  */
87  virtual Size domainSize() const = 0;
88 
89  /**
90  * @brief Adds a new var to the variables of the multidimensional matrix.
91  * @see operator<<(MultiDimInterface& c, const DiscreteVariable& v)
92  *
93  * @warning Note that the variable passed in argument is not duplicated,
94  * that is, only a pointer toward the variable is kept by the
95  * MultiDimInterface.
96  *
97  * @param v The new var.
98  *
99  * @throw DuplicateElement Raised if the variable already belongs to the
100  * sequence of variables.
101  * @throw OperationNotAllowed Raised if this object is non mutable.
102  */
103  virtual void add(const DiscreteVariable& v) = 0;
104 
105  /**
106  * @brief Removes a var from the variables of the multidimensional matrix.
107  * @see operator>>(MultiDimInterface& c, const DiscreteVariable& v)
108  *
109  * @throw OperationNotAllowed Raised if this object is non mutable.
110  * @throw NotFound Raised if v does not belong to this.
111  */
112  virtual void erase(const DiscreteVariable& v) = 0;
113 
114  /**
115  * @brief Returns a const ref to the sequence of DiscreteVariable*.
116  * @return Returns a const ref to the sequence of DiscreteVariable*.
117  */
118  virtual const Sequence< const DiscreteVariable* >&
119  variablesSequence() const = 0;
120 
121  /**
122  * @brief Returns a const ref to the ith var.
123  * param i An index of this multidimensional matrix.
124  * @return Returns a const ref to the ith var.
125  * @throw NotFound Raised if i does not reference a variable in this
126  * multidimensional matrix.
127  */
128  virtual const DiscreteVariable& variable(Idx i) const = 0;
129 
130  /**
131  * @brief Returns the variable with the name
132  *
133  * @param name The index of the variable
134  * @return Returns the variable qith the name in the tuple.
135  * @warging This function is not O(1)
136  * @throw NotFound Raised if the element cannot be found.
137  */
138  virtual const DiscreteVariable& variable(const std::string& name) const = 0;
139 
140  /**
141  * @brief Returns the index of a variable.
142  * @param v The variable for which the index is returned.
143  * @return Returns the index of a variable.
144  * @throw NotFound Raised if v is not in this multidimensional matrix.
145  */
146  virtual Idx pos(const DiscreteVariable& v) const = 0;
147 
148  /**
149  * @brief Returns true if var is in *this.
150  * @param v A DiscreteVariable.
151  * @return Returns true if var is in *this.
152  */
153  virtual bool contains(const DiscreteVariable& v) const = 0;
154 
155  /**
156  * @brief Returns true if no var is in *this.
157  * @return Returns true if no var is in *this.
158  */
159  virtual bool empty() const = 0;
160 
161  /**
162  * @brief Replace variables in this multidim.
163  *
164  * If x is in this MultiDim and y has the same domain size,
165  * then x will be replace by y in this MultiDim.
166  *
167  * @param x The variable in this which will be replaced.
168  * @param y The variable replacing y.
169  * @throw NotFound Raised if x does not belong to this MultiDim.
170  * @throw OperationNotAllowed If y and x are not interchangeable.
171  * @throw DuplicateElement If y is already in this MultiDim.
172  */
173  void replace(const DiscreteVariable& x, const DiscreteVariable& y);
174 
175  /// @}
176 
177  protected:
178  /**
179  * @brief This is called by MultiDimContainer::replace() to proceed with the
180  * replacing between x and y.
181  *
182  * This is called only when everything have been checked.
183  *
184  * @param x The variable to replace in
185  * @param y The second variable to swap.
186  */
187  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y)
188  = 0;
189  };
190 
191  /**
192  * @brief Adds a new var to the sequence of vars.
193  * @ingroup multidim_group
194  *
195  * This function is not VIRTUAL (call of add which IS virtual). @warning
196  * Note that the variable passed in argument is not duplicated, that is, only
197  * a pointer toward the variable is kept by the MultiDimInterface
198  *
199  * @code mat << var1 << var2; @endcode will insert @code var1 @endcode first.
200  *
201  * @param c the multidim container
202  * @param v the new var
203  * @return a reference to *this
204  *
205  * @throw DuplicateElement Raised if the variable already belongs to the
206  * sequence of variables.
207  * @throw OperationNotAllowed Raised if *this is non mutable.
208  */
209  MultiDimInterface& operator<<(MultiDimInterface& c, const DiscreteVariable& v);
210 
211  /**
212  * @brief Removes a var from the variables of the MutliDimAdressing.
213  * @ingroup multidim_group
214  *
215  * @throw OperationNotAllowed Raised if this object is non mutable.
216  * @throw NotFound Raised if v does not belong to this
217  */
218  MultiDimInterface& operator>>(MultiDimInterface& c, const DiscreteVariable& v);
219 
220 } /* namespace gum */
221 
222 #ifndef GUM_NO_INLINE
223 # include <agrum/tools/multidim/implementations/multiDimInterface_inl.h>
224 #endif // GUM_NO_INLINE
225 
226 #endif /* GUM_MULTIDIM_INTERFACE_H */