aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimInterface.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 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* >& variablesSequence() const = 0;
119 
120  /**
121  * @brief Returns a const ref to the ith var.
122  * param i An index of this multidimensional matrix.
123  * @return Returns a const ref to the ith var.
124  * @throw NotFound Raised if i does not reference a variable in this
125  * multidimensional matrix.
126  */
127  virtual const DiscreteVariable& variable(Idx i) const = 0;
128 
129  /**
130  * @brief Returns the variable with the name
131  *
132  * @param name The index of the variable
133  * @return Returns the variable qith the name in the tuple.
134  * @warging This function is not O(1)
135  * @throw NotFound Raised if the element cannot be found.
136  */
137  virtual const DiscreteVariable& variable(const std::string& name) const = 0;
138 
139  /**
140  * @brief Returns the index of a variable.
141  * @param v The variable for which the index is returned.
142  * @return Returns the index of a variable.
143  * @throw NotFound Raised if v is not in this multidimensional matrix.
144  */
145  virtual Idx pos(const DiscreteVariable& v) const = 0;
146 
147  /**
148  * @brief Returns true if var is in *this.
149  * @param v A DiscreteVariable.
150  * @return Returns true if var is in *this.
151  */
152  virtual bool contains(const DiscreteVariable& v) const = 0;
153 
154  /**
155  * @brief Returns true if no var is in *this.
156  * @return Returns true if no var is in *this.
157  */
158  virtual bool empty() const = 0;
159 
160  /**
161  * @brief Replace variables in this multidim.
162  *
163  * If x is in this MultiDim and y has the same domain size,
164  * then x will be replace by y in this MultiDim.
165  *
166  * @param x The variable in this which will be replaced.
167  * @param y The variable replacing y.
168  * @throw NotFound Raised if x does not belong to this MultiDim.
169  * @throw OperationNotAllowed If y and x are not interchangeable.
170  * @throw DuplicateElement If y is already in this MultiDim.
171  */
172  void replace(const DiscreteVariable& x, const DiscreteVariable& y);
173 
174  /// @}
175 
176  protected:
177  /**
178  * @brief This is called by MultiDimContainer::replace() to proceed with the
179  * replacing between x and y.
180  *
181  * This is called only when everything have been checked.
182  *
183  * @param x The variable to replace in
184  * @param y The second variable to swap.
185  */
186  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y) = 0;
187  };
188 
189  /**
190  * @brief Adds a new var to the sequence of vars.
191  * @ingroup multidim_group
192  *
193  * This function is not VIRTUAL (call of add which IS virtual). @warning
194  * Note that the variable passed in argument is not duplicated, that is, only
195  * a pointer toward the variable is kept by the MultiDimInterface
196  *
197  * @code mat << var1 << var2; @endcode will insert @code var1 @endcode first.
198  *
199  * @param c the multidim container
200  * @param v the new var
201  * @return a reference to *this
202  *
203  * @throw DuplicateElement Raised if the variable already belongs to the
204  * sequence of variables.
205  * @throw OperationNotAllowed Raised if *this is non mutable.
206  */
207  MultiDimInterface& operator<<(MultiDimInterface& c, const DiscreteVariable& v);
208 
209  /**
210  * @brief Removes a var from the variables of the MutliDimAdressing.
211  * @ingroup multidim_group
212  *
213  * @throw OperationNotAllowed Raised if this object is non mutable.
214  * @throw NotFound Raised if v does not belong to this
215  */
216  MultiDimInterface& operator>>(MultiDimInterface& c, const DiscreteVariable& v);
217 
218 } /* namespace gum */
219 
220 #ifndef GUM_NO_INLINE
221 # include <agrum/tools/multidim/implementations/multiDimInterface_inl.h>
222 #endif // GUM_NO_INLINE
223 
224 #endif /* GUM_MULTIDIM_INTERFACE_H */