aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
operatorPattern4MultiDimImplementation.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 the pattern used by all binary MultiDimImplementations operators
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 // check if we allowed these patterns to be used
30 #ifndef GUM_OPERATOR_PATTERN_ALLOWED
31 
32 // #warning To use operatorPattern4MultiDimImplementation.h, you must define
33 // GUM_OPERATOR_PATTERN_ALLOWED
34 
35 #else
36 
37 namespace gum {
38 
39 # ifdef GUM_MULTI_DIM_OPERATOR_NAME
40  template < typename T >
41  MultiDimImplementation< T >*
42  GUM_MULTI_DIM_OPERATOR_NAME(const MultiDimImplementation< T >& impl1,
43  const MultiDimImplementation< T >& impl2) {
44 # endif
45 
46  typename OperatorRegister4MultiDim< T >::OperatorPtr func;
47  bool swapped_names = false;
48 
49  // get the appropriate function to perform the operation
50  try {
51  // try to find +(impl1,impl2) in the register
52  func = OperatorRegister4MultiDim< T >::Register().get(
53  GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
54  impl1.name(),
55  impl2.name());
56  } catch (NotFound&) {
57 
58 # ifdef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
59 
60  try {
61  // the function could not be found but, as additions are symmetric, it
62  // may be the case that +(impl2,impl1) does exist in the register. For
63  // instance, we wrote +(MultiDimArray,MultiDimSparse) but not
64  // +(MultiDimSparse,MultiDimArray) and we called
65  // +(MultiDimSparse,MultiDimArray)
66  func = OperatorRegister4MultiDim< T >::Register().get(
67  GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
68  impl2.name(),
69  impl1.name());
70  swapped_names = true;
71  } catch (NotFound&) {
72 # endif /* GUM_MULTI_DIM_SYMMETRIC_OPERATOR */
73 
74  func = OperatorRegister4MultiDim< T >::Register().get(
75  GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
76  impl1.basename(),
77  impl2.basename());
78 
79 # ifdef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
80  }
81 
82 # endif /* GUM_MULTI_DIM_SYMMETRIC_OPERATOR */
83  }
84 
85  // perform the addition
86  if (swapped_names)
87  return func(&impl2, &impl1);
88  else
89  return func(&impl1, &impl2);
90  }
91 } // namespace gum
92 #endif /* GUM_OPERATOR_PATTERN_ALLOWED */