aGrUM  0.14.2
completeProjectionPattern4MultiDimArray.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 // check if we allowed these patterns to be used
29 #ifndef GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
30 
31 // #warning To use completeProjectionPattern, you must define
32 // GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
33 
34 #else
35 namespace gum {
36 
37  // a specialized function for projecting a multiDimArray over all its variables
38 
39 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME
40 # define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
41  template < typename GUM_SCALAR >
42  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME(
43  const MultiDimArray< GUM_SCALAR >* table, Instantiation* instantiation)
44 # endif
45 
46  // clang-format off
47 
48 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME
49 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
50 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
51  template <typename GUM_SCALAR>
52  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME(
53  const MultiDimArray<GUM_SCALAR*>* table,
54  Instantiation* instantiation )
55 #endif
56 
57 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F
58 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
59  template <typename GUM_SCALAR>
60  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F(
61  const MultiDimArray<GUM_SCALAR>* table,
62  GUM_SCALAR ( *f )( const GUM_SCALAR&,
63  const GUM_SCALAR& ),
64  Instantiation* instantiation )
65 #endif
66 
67 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
68 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
69 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
70  template <typename GUM_SCALAR>
71  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F(
72  const MultiDimArray<GUM_SCALAR*>* table,
73  GUM_SCALAR* ( *f )( const GUM_SCALAR const*,
74  const GUM_SCALAR const* ),
75  Instantiation* instantiation )
76 #endif
77 
78 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME
79 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
80  template <typename GUM_SCALAR>
81  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME(
82  const MultiDimImplementation<GUM_SCALAR>* ttable,
83  Instantiation* instantiation )
84 #endif
85 
86 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME
87 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR *
88 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
89  template <typename GUM_SCALAR>
90  GUM_SCALAR*
91  GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME(
92  const MultiDimImplementation<GUM_SCALAR*>* ttable,
93  Instantiation* instantiation )
94 #endif
95 
96  // clang-format on
97 
98  {
99 
100 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_IMPL2ARRAY_NAME
101  const auto table =
102  reinterpret_cast< const MultiDimArray< GUM_SCALAR >* >(ttable);
103 # endif
104 
105 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_IMPL2ARRAY_NAME
106  const auto table =
107  reinterpret_cast< const MultiDimArray< GUM_SCALAR* >* >(ttable);
108 # endif
109 
110  // first, compute the domain size of the table:
111  Size nb_elt = table->domainSize();
112 
113  // now, parse the table and compute the projection. Start with the
114  // neutral element
115  GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* ptable =
116  const_cast< GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* >(
117  &(table->unsafeGet(0)));
118  GUM_SCALAR current_val = GUM_MULTI_DIM_COMPLETE_PROJECTION_NEUTRAL;
119 
120  if (instantiation) {
121  GUM_SCALAR old_val = current_val;
122  GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* current_ptr = ptable;
123  GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* deb_ptr = ptable;
124 
125  for (Size i = 0; i < nb_elt; ++i, ++ptable) {
126 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
127  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, *ptable);
128 # else
129  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, *ptable);
130 # endif
131 
132  if (current_val != old_val) {
133  old_val = current_val;
134  current_ptr = ptable;
135  }
136  }
137 
138  // put the table's variables into the instantiation
139  instantiation->forgetMaster();
140  instantiation->clear();
141  const Sequence< const DiscreteVariable* >& table_vars =
142  table->variablesSequence();
143 
144  for (const auto var : table_vars)
145  instantiation->add(*var);
146 
147  // return the result
148  table->fromOffset(*instantiation, Size(current_ptr - deb_ptr));
149 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
150  return *current_ptr;
151 # else
152  return current_val;
153 # endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
154  } else {
155 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
156  GUM_SCALAR old_val = current_val;
157  GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE* current_ptr = ptable;
158 
159  for (Size i = 0; i < nb_elt; ++i, ++ptable) {
160  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, *ptable);
161 
162  if (current_val != old_val) {
163  old_val = current_val;
164  current_ptr = ptable;
165  }
166  }
167 
168  return *current_ptr;
169 # else
170 
171  for (Size i = 0; i < nb_elt; ++i, ++ptable) {
172  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, *ptable);
173  }
174 
175  return current_val;
176 # endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
177  }
178  }
179 
180 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE
181 
182 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
183 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
184 # endif
185 
186 } /* End of namespace gum */
187 
188 #endif /* GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED */
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45