aGrUM  0.14.2
scheduleCombine_tpl.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  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 # include <agrum/agrum.h>
30 # include <limits>
31 
32 namespace gum {
33 
35  template < typename GUM_SCALAR >
37  const ScheduleMultiDim< GUM_SCALAR >& table1,
38  const ScheduleMultiDim< GUM_SCALAR >& table2,
39  MultiDimImplementation< GUM_SCALAR >* (*combine)(
40  const MultiDimImplementation< GUM_SCALAR >&,
41  const MultiDimImplementation< GUM_SCALAR >&)) :
42  ScheduleOperation< GUM_SCALAR >(
43  ScheduleOperation< GUM_SCALAR >::Type::COMBINE_MULTIDIM),
44  __table1(table1), __table2(table2), __args(0), __results(0),
45  __combine(combine) {
46  // for debugging purposes
47  GUM_CONSTRUCTOR(ScheduleCombine);
48 
49  // compute the variables of the resulting table
50  Sequence< const DiscreteVariable* > vars = __table1.variablesSequence();
51  const Sequence< const DiscreteVariable* >& vars2 =
52  __table2.variablesSequence();
53 
55  vars2.beginSafe();
56  iter != vars2.endSafe();
57  ++iter) {
58  if (!vars.exists(*iter)) { vars.insert(*iter); }
59  }
60 
61  // create the scheduleMultiDim that should result from the combination of
62  // table1 and table2
63  __result = new ScheduleMultiDim< GUM_SCALAR >(vars);
64  }
65 
67  template < typename GUM_SCALAR >
69  const ScheduleCombine< GUM_SCALAR >& from) :
70  ScheduleOperation< GUM_SCALAR >(from),
71  __table1(from.__table1), __table2(from.__table2),
72  __result(new ScheduleMultiDim< GUM_SCALAR >(*(from.__result))), __args(0),
73  __results(0), __combine(from.__combine) {
74  // for debugging purposes
75  GUM_CONS_CPY(ScheduleCombine);
76  }
77 
79  template < typename GUM_SCALAR >
80  ScheduleCombine< GUM_SCALAR >*
82  return new ScheduleCombine< GUM_SCALAR >(*this);
83  }
84 
86  template < typename GUM_SCALAR >
88  // for debugging purposes
89  GUM_DESTRUCTOR(ScheduleCombine);
90  delete __result;
91 
92  if (__args) delete __args;
93 
94  if (__results) delete __results;
95  }
96 
98  template < typename GUM_SCALAR >
99  ScheduleCombine< GUM_SCALAR >& ScheduleCombine< GUM_SCALAR >::
100  operator=(const ScheduleCombine< GUM_SCALAR >& from) {
101  // avoid self assignment
102  if (this != &from) {
104  __table1 = from.__table1;
105  __table2 = from.__table2;
106  *__result = *(from.__result);
107  __combine = from.__combine;
108 
109  // update __args and __results if they were already created
110  if (__args) {
111  __args->clear();
112  __args->insert(&__table1);
113  __args->insert(&__table2);
114  }
115 
116  if (__results) {
117  __results->clear();
118  __results->insert(__result);
119  }
120  }
121 
122  return *this;
123  }
124 
126  template < typename GUM_SCALAR >
128  operator==(const ScheduleOperation< GUM_SCALAR >& op) const {
129  if (this->type() != op.type()) return false;
130 
131  const ScheduleCombine< GUM_SCALAR >& real_op =
132  static_cast< const ScheduleCombine< GUM_SCALAR >& >(op);
133  return ((((__table1 == real_op.__table1) && (__table2 == real_op.__table2))
134  || ((__table1 == real_op.__table2) && (__table2 == real_op.__table1)))
135  && (__combine == real_op.__combine));
136  }
137 
139  template < typename GUM_SCALAR >
141  operator!=(const ScheduleOperation< GUM_SCALAR >& op) const {
142  return !operator==(op);
143  }
144 
146  template < typename GUM_SCALAR >
148  if (__result->isAbstract()) {
149  // first, get the multidims to combine
150  const MultiDimImplementation< GUM_SCALAR >& t1 = __table1.multiDim();
151  const MultiDimImplementation< GUM_SCALAR >& t2 = __table2.multiDim();
152 
153  // perform the combination and store the result
154  MultiDimImplementation< GUM_SCALAR >* res = __combine(t1, t2);
155  __result->setMultiDim(*res);
156  }
157  }
158 
161  template < typename GUM_SCALAR >
163  const Sequence< const DiscreteVariable* >& seq1 = __table1.variablesSequence();
164  const Sequence< const DiscreteVariable* >& seq2 = __table2.variablesSequence();
165 
166  if (seq1.empty() && seq2.empty()) return 0.0f;
167 
168  float size = 1;
169 
170  for (const auto var : seq1)
171  size *= var->domainSize();
172 
173  for (const auto var : seq2)
174  if (!seq1.exists(var)) size *= var->domainSize();
175 
176  return size;
177  }
178 
180  template < typename GUM_SCALAR >
181  std::pair< long, long > ScheduleCombine< GUM_SCALAR >::memoryUsage() const {
182  const Sequence< const DiscreteVariable* >& seq1 = __table1.variablesSequence();
183  const Sequence< const DiscreteVariable* >& seq2 = __table2.variablesSequence();
184 
185  if (seq1.empty() && seq2.empty()) return std::pair< long, long >(0, 0);
186 
187  long size = 1;
188 
189  for (const auto var : seq1) {
190  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
191  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
192  }
193 
194  size *= long(var->domainSize());
195  }
196 
197  for (const auto var : seq2)
198  if (!seq1.exists(var)) {
199  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
200  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
201  }
202 
203  size *= long(var->domainSize());
204  }
205 
206  return std::pair< long, long >(size, size);
207  }
208 
210  template < typename GUM_SCALAR >
211  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
213  if (!__args) {
214  __args = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
215  __args->insert(&__table1);
216  __args->insert(&__table2);
217  }
218 
219  return *__args;
220  }
221 
223  template < typename GUM_SCALAR >
224  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
226  if (!__results) {
227  __results = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
228  __results->insert(__result);
229  }
230 
231  return *__results;
232  }
233 
235  template < typename GUM_SCALAR >
236  std::string ScheduleCombine< GUM_SCALAR >::toString() const {
237  return __result->toString() + " = combine ( " + __table1.toString() + " , "
238  + __table2.toString() + " )";
239  }
240 
242  template < typename GUM_SCALAR >
243  INLINE const ScheduleMultiDim< GUM_SCALAR >&
245  return *__result;
246  }
247 
248 } // namespace gum
249 
250 #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
a Combination operator class used for scheduling inferences
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
gum is the global namespace for all aGrUM entities
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:52
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition: sequence.h:1035
INLINE bool operator!=(const ScheduleOperation< GUM_SCALAR > &) const
operator !=