aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
completeProjectionRegister4MultiDim.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 A container for registering complete projection functions on
25  * multiDimImplementations, i.e., projections over all variables
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #ifndef GUM_COMPLETE_PROJECTION_REGISTER_4_MULTI_DIM_H
31 #define GUM_COMPLETE_PROJECTION_REGISTER_4_MULTI_DIM_H
32 
33 #include <iostream>
34 #include <string>
35 #include <utility>
36 
37 #include <agrum/tools/core/hashTable.h>
38 #include <agrum/tools/core/set.h>
39 #include <agrum/tools/multidim/instantiation.h>
40 #include <agrum/tools/variables/discreteVariable.h>
41 
42 namespace gum {
43 
44  // the base object used by the projections
45  template < typename GUM_SCALAR >
46  class MultiDimImplementation;
47 
48  // ===========================================================================
49  // === GUM_MULTI_DIM_COMPLETE_PROJECTION_REGISTER ===
50  // ===========================================================================
51 
52  // clang-format off
53  /**
54  * @class CompleteProjectionRegister4MultiDim
55  * @headerfile completeProjectionRegister4MultiDim.h agrum/multdim/operators/completeProjectionRegister4MultiDim.h
56  * @ingroup multidim_op_group
57  *
58  * @brief A container for registering complete projection functions on
59  * multiDimImplementations, i.e., functions projecting tables over all their
60  * variables.
61  *
62  */
63  // clang-format on
64  template < typename GUM_SCALAR >
65  class CompleteProjectionRegister4MultiDim {
66  public:
67  /// the type of functions used by the register
68  typedef GUM_SCALAR (*CompleteProjectionPtr)(const MultiDimImplementation< GUM_SCALAR >*,
69  Instantiation* instantiation);
70 
71  // =========================================================================
72  /// @name Accessors / Modifiers
73  // =========================================================================
74  /// @{
75 
76  /**
77  * @brief Adds a new entry into the register.
78  *
79  * This method inserts a new function (\e newFunction) taking a multiDim of
80  * type \e type_multidim (which actually inherit from
81  * MultiDimImplementation) and, possibly, a pointer on an instantiation in
82  * arguments. This new function's purpose is to achieve the projection
83  * described by \e projection_name. For instance, if projection_name is
84  * "max", "min", "sum" or "product", the new function performs the usual
85  * algebraic operations.
86  *
87  * @param projection_name describes the name of the operation performed by
88  * newFunction. Usual operation names are "min", "max", "sum", "product"
89  * @param type_multidim the \e real type of the multiDim taken in argument
90  * by function \e newFunction.
91  *
92  * @param newFunction a pointer to the new function to register. Note that
93  * although \e newFunction actually performs an operation on multiDims of
94  * type \e type_multidim, it should be declared as taking in argument two
95  * MultiDimImplementations. This constraint is imposed by the C++ typing
96  * system.
97  */
98  void insert(const std::string& projection_name,
99  const std::string& type_multidim,
100  CompleteProjectionPtr newFunction);
101 
102  /**
103  * @brief Removes a given entry from the register.
104  *
105  * removes the function, if any, that performs the projection described by
106  * \e projection_name, and that takes in argument a multiDim of type \e
107  * type_multidim
108  *
109  * @param projection_name describes the name of the projection performed by
110  * the function to remove. Usual projection names are "min", "max", "sum",
111  * "product"
112  *
113  * @param type_multidim the \e real type of the multiDim taken in argument
114  * by the function to remove
115  */
116  void erase(const std::string& projection_name, const std::string& type_multidim);
117 
118  /**
119  * @brief Indicates whether a given entry exists in the register.
120  *
121  * Indicates if the register contains a function that performs the
122  * projection described by \e projection_name, and that takes in argument a
123  * multiDim of type \e type_multidim.
124  *
125  * @param projection_name describes the name of the projection performed by
126  * the function we look for. Usual projection names are "min", "max",
127  * "sum", "product".
128  *
129  * @param type_multidim the \e real type of the multiDim taken in argument
130  * by the function we look for.
131  */
132  bool exists(const std::string& projection_name, const std::string& type_multidim) const;
133 
134  /**
135  * @brief returns the specialized projection operator assigned to a given
136  * type of MultiDimImplementation
137  *
138  * returns the function, if any, that performs the projection described by
139  * \e projection_name, and that takes in argument a multiDim of type \e
140  * type_multidim
141  *
142  * @param projection_name describes the name of the projection performed by
143  * the function we look for. Usual projection names are "min", "max",
144  * "sum", "product"
145  *
146  * @param type_multidim the \e real type of the multiDim taken in argument
147  * by the function we look for @throws NotFound exception is thrown if the
148  * operator we look for does not exist within this register.
149  */
150  CompleteProjectionPtr get(const std::string& projection_name,
151  const std::string& type_multidim) const;
152 
153  /// @}
154 
155  // =========================================================================
156  /// @name Named Constructors
157  // =========================================================================
158  /// @{
159 
160  /**
161  * @brief A named constructor that constructs one and only one Register per
162  * data type.
163  *
164  * Note that this constructor prevents the famous init order fiasco.
165  */
166  static CompleteProjectionRegister4MultiDim& Register();
167 
168  /// @}
169 
170  private:
171  // =========================================================================
172  /// @name Constructors / Destructors
173  // =========================================================================
174  /// @{
175 
176  /// Default constructor: creates an empty register
177  CompleteProjectionRegister4MultiDim();
178 
179  /// copy operator: never to be used
180  CompleteProjectionRegister4MultiDim(const CompleteProjectionRegister4MultiDim&);
181 
182  /// Destructor
183  ~CompleteProjectionRegister4MultiDim();
184 
185  /// @}
186 
187  /// The set of associations for a given projection type
188  typedef HashTable< std::string, CompleteProjectionPtr > CompleteProjectionSet;
189 
190  /**
191  * @brief A mapping from the types of MultiDimImplementations to
192  * projection operators.
193  *
194  * In this type, the strings represent the very types of the
195  * MultiDimImplementations that will be combined. Hence, to a subtype of
196  * MultiDimImplementation is associated a function to project this subtype
197  * of hypermatrix (the CompleteProjectionPtr)
198  */
199  HashTable< std::string, CompleteProjectionSet* > _set_;
200  };
201 
202  /// A function to more easily register new projection functions in MultiDims
203  /// @ingroup multidim_op_group
204  template < typename GUM_SCALAR >
205  void registerCompleteProjection(
206  const std::string& projection_name,
207  const std::string& type_multidim,
208  typename CompleteProjectionRegister4MultiDim< GUM_SCALAR >::CompleteProjectionPtr function);
209 
210 } /* namespace gum */
211 
212 // always include the implementations
213 #include <agrum/tools/multidim/utils/operators/completeProjectionRegister4MultiDim_tpl.h>
214 
215 #endif /* GUM_COMPLETE_PROJECTION_REGISTER_MULTI_DIM_H */