aGrUM  0.16.0
completeProjectionPattern4BaseName.h
Go to the documentation of this file.
1 
31 // check if we allowed these patterns to be used
32 #ifndef GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
33 
34 // #warning To use completeProjectionPattern, you must define
35 // GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED
36 
37 #else
38 namespace gum {
39 
40  // a specialized function for projecting a multiDimImplementation over a subset
41  // of its variables
42 
43 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME
44 # define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
45  template < typename GUM_SCALAR >
46  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME(
47  const MultiDimImplementation< GUM_SCALAR >* table,
48  Instantiation* instantiation = 0)
49 # endif
50 
51  // clang-format off
52 
53 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME
54 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
55 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
56  template <typename GUM_SCALAR>
57  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME(
58  const MultiDimImplementation<GUM_SCALAR*>* table,
59  Instantiation* instantiation = 0 )
60 #endif
61 
62 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F
63 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR
64  template <typename GUM_SCALAR>
65  GUM_SCALAR GUM_MULTI_DIM_COMPLETE_PROJECTION_NAME_F(
66  const MultiDimImplementation<GUM_SCALAR>* table,
67  GUM_SCALAR ( *f )( const GUM_SCALAR&, const GUM_SCALAR& ),
68  Instantiation* instantiation = 0 )
69 #endif
70 
71 #ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
72 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE GUM_SCALAR*
73 #define GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
74  template <typename GUM_SCALAR>
75  GUM_SCALAR* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F(
76  const MultiDimImplementation<GUM_SCALAR*>* table,
77  GUM_SCALAR* ( *f )(const GUM_SCALAR const*,
78  const GUM_SCALAR const*))
79 #endif
80 
81  // clang-format on
82 
83  {
84 
85 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
86  Instantiation* instantiation = 0;
87 # endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER_NAME_F
88 
89  // first, compute the domain size of the table:
90  Size nb_elt = table->domainSize();
91 
92  // now, parse the table and compute the projection. Start with the
93  // neutral element
94  Instantiation table_inst(table);
95  GUM_SCALAR current_val = GUM_MULTI_DIM_COMPLETE_PROJECTION_NEUTRAL;
96 
97  if (instantiation) {
98  GUM_SCALAR old_val = current_val;
99  Size current_ptr = 0;
100 
101  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
102 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
103  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
104 # else
105  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
106 # endif // GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
107 
108  if (current_val != old_val) {
109  old_val = current_val;
110  current_ptr = i;
111  }
112  }
113 
114  // put the table's variables into the instantiation
115  instantiation->forgetMaster();
116  instantiation->clear();
117  const Sequence< const DiscreteVariable* >& table_vars =
118  table->variablesSequence();
119 
120  for (const auto var : table_vars)
121  instantiation->add(*var);
122 
123  // return the result
124  instantiation->operator+=(current_ptr);
125  return table->get(*instantiation);
126  } else {
127 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
128  GUM_SCALAR old_val = current_val;
129  Size current_ptr = 0;
130 
131  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
132  GUM_MULTI_DIM_COMPLETE_PROJECTION(&current_val, table->get(table_inst));
133 
134  if (current_val != old_val) {
135  old_val = current_val;
136  current_ptr = i;
137  }
138  }
139 
140  table_inst.setFirst();
141  table_inst += current_ptr return table->get(table_inst);
142 # else
143 
144  for (Size i = 0; i < nb_elt; ++i, ++table_inst) {
145  GUM_MULTI_DIM_COMPLETE_PROJECTION(current_val, table->get(table_inst));
146  }
147 
148  return current_val;
149 # endif /* GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER */
150  }
151  }
152 
153 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_TYPE
154 
155 # ifdef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
156 # undef GUM_MULTI_DIM_COMPLETE_PROJECTION_POINTER
157 # endif
158 
159 } /* End of namespace gum */
160 
161 #endif /* GUM_COMPLETE_PROJECTION_PATTERN_ALLOWED */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48