aGrUM  0.16.0
scheduleCombine_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 # include <agrum/agrum.h>
33 # include <limits>
34 
35 namespace gum {
36 
38  template < typename GUM_SCALAR >
40  const ScheduleMultiDim< GUM_SCALAR >& table1,
41  const ScheduleMultiDim< GUM_SCALAR >& table2,
42  MultiDimImplementation< GUM_SCALAR >* (*combine)(
43  const MultiDimImplementation< GUM_SCALAR >&,
44  const MultiDimImplementation< GUM_SCALAR >&)) :
45  ScheduleOperation< GUM_SCALAR >(
46  ScheduleOperation< GUM_SCALAR >::Type::COMBINE_MULTIDIM),
47  __table1(table1), __table2(table2), __args(0), __results(0),
48  __combine(combine) {
49  // for debugging purposes
50  GUM_CONSTRUCTOR(ScheduleCombine);
51 
52  // compute the variables of the resulting table
53  Sequence< const DiscreteVariable* > vars = __table1.variablesSequence();
54  const Sequence< const DiscreteVariable* >& vars2 =
55  __table2.variablesSequence();
56 
58  vars2.beginSafe();
59  iter != vars2.endSafe();
60  ++iter) {
61  if (!vars.exists(*iter)) { vars.insert(*iter); }
62  }
63 
64  // create the scheduleMultiDim that should result from the combination of
65  // table1 and table2
66  __result = new ScheduleMultiDim< GUM_SCALAR >(vars);
67  }
68 
70  template < typename GUM_SCALAR >
72  const ScheduleCombine< GUM_SCALAR >& from) :
73  ScheduleOperation< GUM_SCALAR >(from),
74  __table1(from.__table1), __table2(from.__table2),
75  __result(new ScheduleMultiDim< GUM_SCALAR >(*(from.__result))), __args(0),
76  __results(0), __combine(from.__combine) {
77  // for debugging purposes
78  GUM_CONS_CPY(ScheduleCombine);
79  }
80 
82  template < typename GUM_SCALAR >
83  ScheduleCombine< GUM_SCALAR >*
85  return new ScheduleCombine< GUM_SCALAR >(*this);
86  }
87 
89  template < typename GUM_SCALAR >
91  // for debugging purposes
92  GUM_DESTRUCTOR(ScheduleCombine);
93  delete __result;
94 
95  if (__args) delete __args;
96 
97  if (__results) delete __results;
98  }
99 
101  template < typename GUM_SCALAR >
102  ScheduleCombine< GUM_SCALAR >& ScheduleCombine< GUM_SCALAR >::
103  operator=(const ScheduleCombine< GUM_SCALAR >& from) {
104  // avoid self assignment
105  if (this != &from) {
107  __table1 = from.__table1;
108  __table2 = from.__table2;
109  *__result = *(from.__result);
110  __combine = from.__combine;
111 
112  // update __args and __results if they were already created
113  if (__args) {
114  __args->clear();
115  __args->insert(&__table1);
116  __args->insert(&__table2);
117  }
118 
119  if (__results) {
120  __results->clear();
121  __results->insert(__result);
122  }
123  }
124 
125  return *this;
126  }
127 
129  template < typename GUM_SCALAR >
131  operator==(const ScheduleOperation< GUM_SCALAR >& op) const {
132  if (this->type() != op.type()) return false;
133 
134  const ScheduleCombine< GUM_SCALAR >& real_op =
135  static_cast< const ScheduleCombine< GUM_SCALAR >& >(op);
136  return ((((__table1 == real_op.__table1) && (__table2 == real_op.__table2))
137  || ((__table1 == real_op.__table2) && (__table2 == real_op.__table1)))
138  && (__combine == real_op.__combine));
139  }
140 
142  template < typename GUM_SCALAR >
144  operator!=(const ScheduleOperation< GUM_SCALAR >& op) const {
145  return !operator==(op);
146  }
147 
149  template < typename GUM_SCALAR >
151  if (__result->isAbstract()) {
152  // first, get the multidims to combine
153  const MultiDimImplementation< GUM_SCALAR >& t1 = __table1.multiDim();
154  const MultiDimImplementation< GUM_SCALAR >& t2 = __table2.multiDim();
155 
156  // perform the combination and store the result
157  MultiDimImplementation< GUM_SCALAR >* res = __combine(t1, t2);
158  __result->setMultiDim(*res);
159  }
160  }
161 
164  template < typename GUM_SCALAR >
166  const Sequence< const DiscreteVariable* >& seq1 = __table1.variablesSequence();
167  const Sequence< const DiscreteVariable* >& seq2 = __table2.variablesSequence();
168 
169  if (seq1.empty() && seq2.empty()) return 0.0f;
170 
171  float size = 1;
172 
173  for (const auto var : seq1)
174  size *= var->domainSize();
175 
176  for (const auto var : seq2)
177  if (!seq1.exists(var)) size *= var->domainSize();
178 
179  return size;
180  }
181 
183  template < typename GUM_SCALAR >
184  std::pair< long, long > ScheduleCombine< GUM_SCALAR >::memoryUsage() const {
185  const Sequence< const DiscreteVariable* >& seq1 = __table1.variablesSequence();
186  const Sequence< const DiscreteVariable* >& seq2 = __table2.variablesSequence();
187 
188  if (seq1.empty() && seq2.empty()) return std::pair< long, long >(0, 0);
189 
190  long size = 1;
191 
192  for (const auto var : seq1) {
193  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
194  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
195  }
196 
197  size *= long(var->domainSize());
198  }
199 
200  for (const auto var : seq2)
201  if (!seq1.exists(var)) {
202  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
203  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
204  }
205 
206  size *= long(var->domainSize());
207  }
208 
209  return std::pair< long, long >(size, size);
210  }
211 
213  template < typename GUM_SCALAR >
214  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
216  if (!__args) {
217  __args = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
218  __args->insert(&__table1);
219  __args->insert(&__table2);
220  }
221 
222  return *__args;
223  }
224 
226  template < typename GUM_SCALAR >
227  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
229  if (!__results) {
230  __results = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
231  __results->insert(__result);
232  }
233 
234  return *__results;
235  }
236 
238  template < typename GUM_SCALAR >
239  std::string ScheduleCombine< GUM_SCALAR >::toString() const {
240  return __result->toString() + " = combine ( " + __table1.toString() + " , "
241  + __table2.toString() + " )";
242  }
243 
245  template < typename GUM_SCALAR >
246  INLINE const ScheduleMultiDim< GUM_SCALAR >&
248  return *__result;
249  }
250 
251 } // namespace gum
252 
253 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
void execute()
executes the operation
INLINE bool operator==(const ScheduleOperation< GUM_SCALAR > &) const
operator ==
ScheduleMultiDim< GUM_SCALAR > __table1
the first table to combine
Type type() const
returns the name of the operation
Sequence< const ScheduleMultiDim< GUM_SCALAR > *> * __args
the set of ScheduleMultidims passed in arguments
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string toString() const
displays the content of the operation
virtual ScheduleCombine< GUM_SCALAR > * newFactory() const
virtual copy constructor: creates a clone of the operation
ScheduleCombine(const ScheduleMultiDim< GUM_SCALAR > &table1, const ScheduleMultiDim< GUM_SCALAR > &table2, MultiDimImplementation< GUM_SCALAR > *(*combine)(const MultiDimImplementation< GUM_SCALAR > &, const MultiDimImplementation< GUM_SCALAR > &))
default constructor
std::pair< long, long > memoryUsage() const
returns the memory consumption used during the operation
ScheduleCombine< GUM_SCALAR > & operator=(const ScheduleCombine< GUM_SCALAR > &)
copy operator
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
float nbOperations() const
returns an estimation of the number of elementary operations needed to perform the ScheduleOperation ...
MultiDimImplementation< GUM_SCALAR > *(* __combine)(const MultiDimImplementation< GUM_SCALAR > &, const MultiDimImplementation< GUM_SCALAR > &)
the function actually used to perform the combination
ScheduleOperation(Type t)
default constructor
~ScheduleCombine()
destructor
Sequence< const ScheduleMultiDim< GUM_SCALAR > *> * __results
the set of ScheduleMultidims resulting from the operation
ScheduleMultiDim< GUM_SCALAR > * __result
the result of the operation
ScheduleOperation< GUM_SCALAR > & operator=(const ScheduleOperation< GUM_SCALAR > &)
copy operator
const Sequence< const ScheduleMultiDim< GUM_SCALAR > *> & multiDimResults() const
returns the set of multidims that should be the result of the operation
INLINE const ScheduleMultiDim< GUM_SCALAR > & result() const
returns the scheduleMultidim resulting from the execution of the operation
ScheduleMultiDim< GUM_SCALAR > __table2
the second table to combine with
const Sequence< const ScheduleMultiDim< GUM_SCALAR > *> & multiDimArgs() const
returns the set of multidims passed in argument to the operation
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition: sequence.h:1038
INLINE bool operator!=(const ScheduleOperation< GUM_SCALAR > &) const
operator !=