aGrUM  0.14.2
partialInstantiationPattern4BaseName.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  ***************************************************************************/
29 
30 // check if we allowed these patterns to be used
31 #ifndef GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED
32 
33 // #warning To use partialIntantiationPattern, you must define
34 // GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED
35 
36 #else
37 namespace gum {
38 
39  // a specialized function instantiating some variables of a table and returning
40  // the result
41 
42 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_NAME
43 # define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR
44  template < typename GUM_SCALAR >
45  MultiDimImplementation< GUM_SCALAR >* GUM_MULTI_DIM_PARTIAL_INSTANTIATION_NAME(
46  const MultiDimImplementation< GUM_SCALAR >* table,
47  const HashTable< const DiscreteVariable*, Idx >& inst_vars)
48 # endif
49 
50  // clang-format off
51 
52 #ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_NAME
53 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE GUM_SCALAR *
54 #define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
55  template <typename GUM_SCALAR>
56  MultiDimImplementation<GUM_SCALAR*>*
58  const MultiDimImplementation<GUM_SCALAR*>* table,
59  const HashTable<const DiscreteVariable*, Idx>& inst_vars )
60 #endif
61 
62  // clang-format on
63 
64  {
65 
66  // get the variables of the uninstantiated table
67  const Sequence< const DiscreteVariable* >& table_vars =
68  table->variablesSequence();
69 
70  // Compute the offset of the variables. In addition, get the offset in
71  // table induced by the instantiation inst_var
72  Idx table_alone_offset = 0;
73  Idx offset = 1;
74  HashTable< const DiscreteVariable*, Idx > var1offset(table_vars.size());
75 
76  for (const auto var : table_vars) {
77  if (inst_vars.exists(var)) { table_alone_offset += inst_vars[var] * offset; }
78 
79  var1offset.insert(var, offset);
80  offset *= var->domainSize();
81  }
82 
83  // Compute the sequence of variables in the result table. Compute as
84  // well the offsets and the domain size of the variables that belong to
85  // result. Finally, compute has_before_incr: this is a Boolean indicating
86  // whether the instantiated variables are the last variables in the
87  // variables sequence of table (true) or not (false). If this Boolean is
88  // true, then we can fill result by parsing both table and result using
89  // only 1-increments.
90  Sequence< const DiscreteVariable* > result_varSeq;
91  std::vector< Idx > table_and_result_offset;
92  std::vector< Idx > table_and_result_domain;
93  Idx result_domain_size = 1;
94  bool has_before_incr = true;
95  bool found_inst_var = false;
96 
97  for (const auto var : table_vars) {
98  if (!inst_vars.exists(var)) {
99  table_and_result_domain.push_back(var->domainSize());
100  table_and_result_offset.push_back(var1offset[var]);
101  result_domain_size *= var->domainSize();
102  result_varSeq << var;
103 
104  if (found_inst_var) has_before_incr = false;
105  } else {
106  found_inst_var = true;
107  }
108  }
109 
110  // table_and_result_value is a vector indictating, for each
111  // uninstantiated variable, how many increments we can still perform on
112  // that variable before we must perform a "major" increment: for
113  // instance, let A and B be two variables of size 10. Then, if
114  // table_and_result_value[A] = 3 and table_and_result_value[B] = 2, this
115  // means that the offset they represent is 78 (10^2 - 32). If we still
116  // increment B twice, then the offset should be 80, which means that we
117  // shall increment A once and decrease B by 10. The value by which
118  // variables shall be decreased is indicated in table_and_result_down
119  std::vector< Idx > table_and_result_value = table_and_result_domain;
120  std::vector< Idx > table_and_result_down = table_and_result_offset;
121 
122  for (unsigned int i = 0; i < table_and_result_down.size(); ++i)
123  table_and_result_down[i] *= (table_and_result_domain[i] - 1);
124 
125  // create a table "result" containing only the variables that are not
126  // instantiated: the variables are stored in the order in which they
127  // appear in "table". Hence, ++ operations on an instantiation on table
128  // will more or less correspond to a ++ operation on an instantiation on
129  // result
130  MultiDimArray< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE >* result =
131  new MultiDimArray< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE >;
132  result->beginMultipleChanges();
133 
134  for (const auto var : result_varSeq)
135  *result << *var;
136 
137  result->endMultipleChanges();
138 
139 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
140  // fill the matrix with any element
141  {
142  const Instantiation table_inst(table);
143  const GUM_SCALAR& any_element = *(table->get(table_inst));
144 
145  for (Idx i = 0; i < result_domain_size; ++i) {
146  result->unsafeSet(i, new GUM_SCALAR(any_element));
147  }
148  }
149 # endif /* GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER */
150 
151  // compute the result: it is now sufficient to loop over the variables
152  // that were not instantiated. ptable and presult are pointers on the
153  // arrays that are directly used for this loop
154  GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* presult =
155  const_cast< GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE* >(
156  &(result->unsafeGet(0)));
157  Instantiation table_inst(table);
158  table_inst += table_alone_offset;
159 
160  // but before doing so, check whether the instantiated variables are the
161  // last ones or not. If so, we can optimize the parsing of ptable and
162  // presult as both tables need be parsed using only 1-increments
163  if (has_before_incr) {
164  for (Idx i = 0; i < result_domain_size; ++i) {
165 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
166  **presult = *(table->get(table_inst));
167 # else
168  *presult = table->get(table_inst);
169 # endif
170 
171  // update the offset of result and table
172  ++table_inst;
173  ++presult;
174  }
175  } else {
176  // here, some uninstantiated variables exist after the instantiated
177  // ones in the variables sequence of table. So, we must perform a more
178  // complicated parsing of ptable
179  for (Idx j = 0; j < result_domain_size; ++j) {
180 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
181  **presult = *(table->get(table_inst));
182 # else
183  *presult = table->get(table_inst);
184 # endif
185 
186  // update the offset of table for the outer loop
187  for (unsigned int k = 0; k < table_and_result_value.size(); ++k) {
188  --table_and_result_value[k];
189 
190  if (table_and_result_value[k]) {
191  table_inst += table_and_result_offset[k];
192  break;
193  }
194 
195  table_and_result_value[k] = table_and_result_domain[k];
196  table_inst -= table_and_result_down[k];
197  }
198 
199  // update the offset of result for the outer loop
200  ++presult;
201  }
202  }
203 
204  return result;
205  }
206 
207 # undef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_TYPE
208 
209 # ifdef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
210 # undef GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER
211 # endif
212 
213 } /* End of namespace gum */
214 
215 #endif /* GUM_PARTIAL_INSTANTIATION_PATTERN_ALLOWED */
#define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_NAME
a specialized partial instantiation function for multiDimArrays
#define GUM_MULTI_DIM_PARTIAL_INSTANTIATION_POINTER_NAME
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Header files of gum::Instantiation.