29 #ifndef GUM_PROJECTION_PATTERN_ALLOWED 40 # ifdef GUM_MULTI_DIM_PROJECTION_NAME 41 # define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR 42 template <
typename GUM_SCALAR >
43 MultiDimArray< GUM_SCALAR >* GUM_MULTI_DIM_PROJECTION_NAME(
44 const MultiDimArray< GUM_SCALAR >* table,
45 const Set< const DiscreteVariable* >& del_vars)
50 #ifdef GUM_MULTI_DIM_PROJECTION_POINTER_NAME 51 #define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR * 52 #define GUM_MULTI_DIM_PROJECTION_POINTER 53 template <
typename GUM_SCALAR>
54 MultiDimArray<GUM_SCALAR*>* GUM_MULTI_DIM_PROJECTION_POINTER_NAME(
55 const MultiDimArray<GUM_SCALAR*>* table,
56 const Set<const DiscreteVariable*>& del_vars )
59 #ifdef GUM_MULTI_DIM_PROJECTION_NAME_F 60 #define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR 61 template <
typename GUM_SCALAR>
62 MultiDimArray<GUM_SCALAR>* GUM_MULTI_DIM_PROJECTION_NAME_F(
63 const MultiDimArray<GUM_SCALAR>* table,
64 const Set<const DiscreteVariable*>& del_vars,
65 GUM_SCALAR ( *f )(
const GUM_SCALAR&,
const GUM_SCALAR& ) )
68 #ifdef GUM_MULTI_DIM_PROJECTION_POINTER_NAME_F 69 #define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR * 70 #define GUM_MULTI_DIM_PROJECTION_POINTER 71 template <
typename GUM_SCALAR>
72 MultiDimArray<GUM_SCALAR*>* GUM_MULTI_DIM_PROJECTION_POINTER_NAME_F(
73 const MultiDimArray<GUM_SCALAR*>* table,
74 const Set<const DiscreteVariable*>& del_vars,
75 GUM_SCALAR* ( *f )(
const GUM_SCALAR
const*,
76 const GUM_SCALAR
const* ) )
79 #ifdef GUM_MULTI_DIM_PROJECTION_IMPL2ARRAY_NAME 80 #define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR 81 template <
typename GUM_SCALAR>
82 MultiDimImplementation<GUM_SCALAR>*
83 GUM_MULTI_DIM_PROJECTION_IMPL2ARRAY_NAME(
84 const MultiDimImplementation<GUM_SCALAR>* ttable,
85 const Set<const DiscreteVariable*>& del_vars )
88 #ifdef GUM_MULTI_DIM_PROJECTION_POINTER_IMPL2ARRAY_NAME 89 #define GUM_MULTI_DIM_PROJECTION_TYPE GUM_SCALAR * 90 #define GUM_MULTI_DIM_PROJECTION_POINTER 91 template <
typename GUM_SCALAR>
92 MultiDimImplementation<GUM_SCALAR*>*
93 GUM_MULTI_DIM_PROJECTION_POINTER_IMPL2ARRAY_NAME(
94 const MultiDimImplementation<GUM_SCALAR*>* ttable,
95 const Set<const DiscreteVariable*>& del_vars )
102 # ifdef GUM_MULTI_DIM_PROJECTION_IMPL2ARRAY_NAME 103 const MultiDimArray< GUM_SCALAR >* table =
104 reinterpret_cast< const MultiDimArray< GUM_SCALAR >*
>(ttable);
107 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER_IMPL2ARRAY_NAME 108 const MultiDimArray< GUM_SCALAR* >* table =
109 reinterpret_cast< const MultiDimArray< GUM_SCALAR* >*
>(ttable);
114 const GUM_SCALAR neutral_element = GUM_MULTI_DIM_PROJECTION_NEUTRAL;
118 const Sequence< const DiscreteVariable* >& table_vars =
119 table->variablesSequence();
120 bool need_swapping = table_vars.size() >= 2 * del_vars.size();
122 if (!need_swapping) {
132 std::vector< Idx > table_and_result_offset;
133 std::vector< Idx > table_and_result_domain;
134 std::vector< Idx > before_incr;
135 unsigned int nb_positive_before_incr = 0;
136 Idx table_alone_domain_size = 1;
137 Idx result_domain_size = 1;
138 Idx table_domain_size = 1;
139 Sequence< const DiscreteVariable* > result_varSeq;
141 Idx tmp_before_incr = 1;
142 bool has_before_incr =
false;
144 for (
const auto var : table_vars) {
145 table_domain_size *= var->domainSize();
147 if (!del_vars.exists(var)) {
148 if (has_before_incr) {
149 before_incr.push_back(tmp_before_incr - 1);
150 has_before_incr =
false;
151 ++nb_positive_before_incr;
153 before_incr.push_back(0);
156 table_and_result_domain.push_back(var->domainSize());
157 table_and_result_offset.push_back(result_domain_size);
158 result_domain_size *= var->domainSize();
160 result_varSeq << var;
162 tmp_before_incr *= var->domainSize();
163 has_before_incr =
true;
164 table_alone_domain_size *= var->domainSize();
168 std::vector< Idx > table_and_result_value = table_and_result_domain;
169 std::vector< Idx > current_incr = before_incr;
170 std::vector< Idx > table_and_result_down = table_and_result_offset;
172 for (
unsigned int i = 0; i < table_and_result_down.size(); ++i) {
173 table_and_result_down[i] *= (table_and_result_domain[i] - 1);
180 MultiDimArray< GUM_MULTI_DIM_PROJECTION_TYPE >* result =
181 new MultiDimArray< GUM_MULTI_DIM_PROJECTION_TYPE >;
183 if (!result_varSeq.size()) {
return result; }
185 result->beginMultipleChanges();
187 for (
const auto var : result_varSeq)
191 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 192 result->endMultipleChanges();
194 for (
Idx i = 0; i < result_domain_size; ++i) {
195 result->unsafeSet(i,
new GUM_SCALAR(neutral_element));
199 result->endMultipleChanges(neutral_element);
214 GUM_MULTI_DIM_PROJECTION_TYPE* pt =
215 const_cast< GUM_MULTI_DIM_PROJECTION_TYPE*
>(&(table->unsafeGet(0)));
216 GUM_MULTI_DIM_PROJECTION_TYPE* pres =
217 const_cast< GUM_MULTI_DIM_PROJECTION_TYPE*
>(&(result->unsafeGet(0)));
218 GUM_MULTI_DIM_PROJECTION_TYPE* pres_deb = pres;
220 if (!nb_positive_before_incr) {
221 for (
Idx i = 0; i < table_alone_domain_size; ++i) {
222 for (
Idx j = 0; j < result_domain_size; ++j) {
223 # ifdef GUM_MULTI_DIM_PROJECTION_EFFECTIVE_TYPE 224 GUM_MULTI_DIM_PROJECTION(*pres, *pt);
226 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 227 **pres = GUM_MULTI_DIM_PROJECTION(*pres, *pt);
229 *pres = GUM_MULTI_DIM_PROJECTION(*pres, *pt);
244 Idx result_offset = 0;
246 for (
Idx i = 0; i < table_domain_size; ++i) {
247 # ifdef GUM_MULTI_DIM_PROJECTION_EFFECTIVE_TYPE 248 GUM_MULTI_DIM_PROJECTION(pres[result_offset], *pt);
250 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 251 *(pres[result_offset]) =
252 GUM_MULTI_DIM_PROJECTION(pres[result_offset], *pt);
254 pres[result_offset] = GUM_MULTI_DIM_PROJECTION(pres[result_offset], *pt);
262 for (
unsigned int k = 0; k < current_incr.size(); ++k) {
264 if (current_incr[k]) {
269 current_incr[k] = before_incr[k];
272 --table_and_result_value[k];
274 if (table_and_result_value[k]) {
275 result_offset += table_and_result_offset[k];
279 table_and_result_value[k] = table_and_result_domain[k];
280 result_offset -= table_and_result_down[k];
297 std::vector< Idx > table_alone_offset;
298 std::vector< Idx > table_alone_domain;
300 Idx table_alone_domain_size = 1;
301 HashTable< const DiscreteVariable*, Idx > var1offset(table_vars.size());
303 for (
const auto var : table_vars) {
304 if (del_vars.exists(var)) {
305 table_alone_domain.push_back(var->domainSize());
306 table_alone_offset.push_back(offset);
307 table_alone_domain_size *= var->domainSize();
310 var1offset.insert(var, offset);
311 offset *= var->domainSize();
314 std::vector< Idx > table_alone_value = table_alone_domain;
315 std::vector< Idx > table_alone_down = table_alone_offset;
317 for (
unsigned int i = 0; i < table_alone_down.size(); ++i)
318 table_alone_down[i] *= (table_alone_domain[i] - 1);
329 Sequence< const DiscreteVariable* > result_varSeq;
330 std::vector< Idx > table_and_result_offset;
331 std::vector< Idx > table_and_result_domain;
332 Idx result_domain_size = 1;
333 bool has_before_incr =
false;
334 bool found_proj_var =
false;
336 for (
const auto var : table_vars) {
337 if (!del_vars.exists(var)) {
338 table_and_result_domain.push_back(var->domainSize());
339 table_and_result_offset.push_back(var1offset[var]);
340 found_proj_var =
true;
341 result_domain_size *= var->domainSize();
342 result_varSeq << var;
344 if (found_proj_var) has_before_incr =
true;
348 std::vector< Idx > table_and_result_value = table_and_result_domain;
349 std::vector< Idx > table_and_result_down = table_and_result_offset;
351 for (
unsigned int i = 0; i < table_and_result_down.size(); ++i) {
352 table_and_result_down[i] *= (table_and_result_domain[i] - 1);
359 MultiDimArray< GUM_MULTI_DIM_PROJECTION_TYPE >* result =
360 new MultiDimArray< GUM_MULTI_DIM_PROJECTION_TYPE >;
361 result->beginMultipleChanges();
363 for (
const auto var : result_varSeq)
366 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 367 result->endMultipleChanges();
370 for (
Idx i = 0; i < result_domain_size; ++i) {
371 result->unsafeSet(i,
new GUM_SCALAR(neutral_element));
375 result->endMultipleChanges(neutral_element);
390 GUM_MULTI_DIM_PROJECTION_TYPE* pt =
391 const_cast< GUM_MULTI_DIM_PROJECTION_TYPE*
>(&(table->unsafeGet(0)));
392 GUM_MULTI_DIM_PROJECTION_TYPE* pres =
393 const_cast< GUM_MULTI_DIM_PROJECTION_TYPE*
>(&(result->unsafeGet(0)));
397 if (!has_before_incr) {
398 for (
Idx i = 0; i < result_domain_size; ++i) {
399 for (
Idx j = 0; j < table_alone_domain_size; ++j) {
400 # ifdef GUM_MULTI_DIM_PROJECTION_EFFECTIVE_TYPE 401 GUM_MULTI_DIM_PROJECTION(*pres, *pt);
403 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 404 **pres = GUM_MULTI_DIM_PROJECTION(*pres, *pt);
406 *pres = GUM_MULTI_DIM_PROJECTION(*pres, *pt);
420 Idx table_offset = 0;
422 for (
Idx j = 0; j < result_domain_size; ++j) {
423 for (
Idx i = 0; i < table_alone_domain_size; ++i) {
424 # ifdef GUM_MULTI_DIM_PROJECTION_EFFECTIVE_TYPE 425 GUM_MULTI_DIM_PROJECTION(*pres, pt[table_offset]);
427 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 428 **pres = GUM_MULTI_DIM_PROJECTION(*pres, pt[table_offset]);
430 *pres = GUM_MULTI_DIM_PROJECTION(*pres, pt[table_offset]);
435 for (
unsigned int k = 0; k < table_alone_value.size(); ++k) {
436 --table_alone_value[k];
438 if (table_alone_value[k]) {
439 table_offset += table_alone_offset[k];
443 table_alone_value[k] = table_alone_domain[k];
444 table_offset -= table_alone_down[k];
449 for (
unsigned int k = 0; k < table_and_result_value.size(); ++k) {
450 --table_and_result_value[k];
452 if (table_and_result_value[k]) {
453 table_offset += table_and_result_offset[k];
457 table_and_result_value[k] = table_and_result_domain[k];
458 table_offset -= table_and_result_down[k];
471 # undef GUM_MULTI_DIM_PROJECTION_TYPE 473 # ifdef GUM_MULTI_DIM_PROJECTION_POINTER 474 # undef GUM_MULTI_DIM_PROJECTION_POINTER gum is the global namespace for all aGrUM entities