aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
operatorPattern4MultiDimImplementation.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 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(GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
53  impl1.name(),
54  impl2.name());
55  } catch (NotFound&) {
56 
57 # ifdef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
58 
59  try {
60  // the function could not be found but, as additions are symmetric, it
61  // may be the case that +(impl2,impl1) does exist in the register. For
62  // instance, we wrote +(MultiDimArray,MultiDimSparse) but not
63  // +(MultiDimSparse,MultiDimArray) and we called
64  // +(MultiDimSparse,MultiDimArray)
65  func = OperatorRegister4MultiDim< T >::Register().get(GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
66  impl2.name(),
67  impl1.name());
68  swapped_names = true;
69  } catch (NotFound&) {
70 # endif /* GUM_MULTI_DIM_SYMMETRIC_OPERATOR */
71 
72  func = OperatorRegister4MultiDim< T >::Register().get(GUM_MULTI_DIM_OPERATOR_FUNC_NAME,
73  impl1.basename(),
74  impl2.basename());
75 
76 # ifdef GUM_MULTI_DIM_SYMMETRIC_OPERATOR
77  }
78 
79 # endif /* GUM_MULTI_DIM_SYMMETRIC_OPERATOR */
80  }
81 
82  // perform the addition
83  if (swapped_names)
84  return func(&impl2, &impl1);
85  else
86  return func(&impl1, &impl2);
87  }
88 } // namespace gum
89 #endif /* GUM_OPERATOR_PATTERN_ALLOWED */