aGrUM  0.14.2
completeProjectionPattern4BaseName.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 multiDimImplementation over a subset
38  // of its variables
39 
40 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME
41 # define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
42  template < typename GUM_SCALAR >
43  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME(
44  const MultiDimImplementation< GUM_SCALAR >* table,
45  Instantiation* instantiation = 0)
46 # endif
47 
48  // clang-format off
49 
50 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME
51 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
52 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
53  template <typename GUM_SCALAR>
54  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME(
55  const MultiDimImplementation<GUM_SCALAR*>* table,
56  Instantiation* instantiation = 0 )
57 #endif
58 
59 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F
60 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
61  template <typename GUM_SCALAR>
62  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F(
63  const MultiDimImplementation<GUM_SCALAR>* table,
64  GUM_SCALAR ( *f )( const GUM_SCALAR&, const GUM_SCALAR& ),
65  Instantiation* instantiation = 0 )
66 #endif
67 
68 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
69 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
70 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
71  template <typename GUM_SCALAR>
72  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F(
73  const MultiDimImplementation<GUM_SCALAR*>* table,
74  GUM_SCALAR* ( *f )(const GUM_SCALAR const*,
75  const GUM_SCALAR const*))
76 #endif
77 
78  // clang-format on
79 
80  {
81 
82 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
83  Instantiation* instantiation = 0;
84 # endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
85 
86  // first, compute the domain size of the table:
87  Size nb_elt = table->domainSize();
88 
89  // now, parse the table and compute the projection. Start with the
90  // neutral element
91  Instantiation table_inst(table);
92  GUM_SCALAR current_val = GUM_MULTI_DIM_COMPLETE_PROJECTION_NEUTRAL;
93 
94  if (instantiation) {
95  GUM_SCALAR old_val = current_val;
96  Size current_ptr = 0;
97 
98  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
99 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
100  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
101 # else
102  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
103 # endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
104 
105  if (current_val != old_val) {
106  old_val = current_val;
107  current_ptr = i;
108  }
109  }
110 
111  // put the table's variables into the instantiation
112  instantiation->forgetMaster();
113  instantiation->clear();
114  const Sequence< const DiscreteVariable* >& table_vars =
115  table->variablesSequence();
116 
117  for (const auto var : table_vars)
118  instantiation->add(*var);
119 
120  // return the result
121  instantiation->operator+=(current_ptr);
122  return table->get(*instantiation);
123  } else {
124 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
125  GUM_SCALAR old_val = current_val;
126  Size current_ptr = 0;
127 
128  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
129  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
130 
131  if (current_val != old_val) {
132  old_val = current_val;
133  current_ptr = i;
134  }
135  }
136 
137  table_inst.setFirst();
138  table_inst += current_ptr return table->get(table_inst);
139 # else
140 
141  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
142  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
143  }
144 
145  return current_val;
146 # endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
147  }
148  }
149 
150 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE
151 
152 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
153 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
154 # endif
155 
156 } /* End of namespace gum */
157 
158 #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