aGrUM  0.16.0
scheduleProject_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 # include <agrum/agrum.h>
32 # include <limits>
33 
34 namespace gum {
35 
37  template < typename GUM_SCALAR >
39  const ScheduleMultiDim< GUM_SCALAR >& table,
40  const Set< const DiscreteVariable* >& del_vars,
41  MultiDimImplementation< GUM_SCALAR >* (*project)(
42  const MultiDimImplementation< GUM_SCALAR >&,
43  const Set< const DiscreteVariable* >&)) :
44  ScheduleOperation< GUM_SCALAR >(
45  ScheduleOperation< GUM_SCALAR >::Type::PROJECT_MULTIDIM),
46  __table(table), __del_vars(del_vars), __args(0), __results(0),
47  __project(project) {
48  // for debugging purposes
49  GUM_CONSTRUCTOR(ScheduleProject);
50 
51  // compute the variables that shall belong to the result of the projection
52  Sequence< const DiscreteVariable* > vars = __table.variablesSequence();
53 
54  for (const auto var : del_vars)
55  vars.erase(var);
56 
57  // create the scheduleMultiDim that should result from the combination of
58  // table1 and table2
59  __result = new ScheduleMultiDim< GUM_SCALAR >(vars);
60  }
61 
63  template < typename GUM_SCALAR >
65  const ScheduleProject< GUM_SCALAR >& f) :
66  ScheduleOperation< GUM_SCALAR >(f),
68  __result(new ScheduleMultiDim< GUM_SCALAR >(*(f.__result))), __args(0),
70  // for debugging purposes
71  GUM_CONS_CPY(ScheduleProject);
72  }
73 
75  template < typename GUM_SCALAR >
76  ScheduleProject< GUM_SCALAR >*
78  return new ScheduleProject< GUM_SCALAR >(*this);
79  }
80 
82  template < typename GUM_SCALAR >
84  // for debugging purposes
85  GUM_DESTRUCTOR(ScheduleProject);
86  delete __result;
87 
88  if (__args) delete __args;
89 
90  if (__results) delete __results;
91  }
92 
94  template < typename GUM_SCALAR >
95  ScheduleProject< GUM_SCALAR >& ScheduleProject< GUM_SCALAR >::
96  operator=(const ScheduleProject< GUM_SCALAR >& from) {
97  // avoid self assignment
98  if (this != &from) {
100  __table = from.__table;
101  __del_vars = from.__del_vars;
102  *__result = *(from.__result);
103  __project = from.__project;
104 
105  // update __args and __results if they were already created
106  if (__args) {
107  __args->clear();
108  __args->insert(&__table);
109  }
110 
111  if (__results) {
112  __results->clear();
113  __results->insert(__result);
114  }
115  }
116 
117  return *this;
118  }
119 
121  template < typename GUM_SCALAR >
123  operator==(const ScheduleOperation< GUM_SCALAR >& op) const {
124  if (this->type() != op.type()) return false;
125 
126  const ScheduleProject< GUM_SCALAR >& real_op =
127  static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
128  return ((__table == real_op.__table) && (__del_vars == real_op.__del_vars)
129  && (__project == real_op.__project));
130  }
131 
133  template < typename GUM_SCALAR >
135  operator!=(const ScheduleOperation< GUM_SCALAR >& op) const {
136  if (this->type() != op.type()) return true;
137 
138  const ScheduleProject< GUM_SCALAR >& real_op =
139  static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
140  return ((__table != real_op.__table) || (__del_vars != real_op.__del_vars)
141  || (__project != real_op.__project));
142  }
143 
145  template < typename GUM_SCALAR >
147  if (__result->isAbstract()) {
148  const MultiDimImplementation< GUM_SCALAR >& t = __table.multiDim();
149  MultiDimImplementation< GUM_SCALAR >* res = __project(t, __del_vars);
150  __result->setMultiDim(*res);
151  }
152  }
153 
156  template < typename GUM_SCALAR >
157  INLINE float ScheduleProject< GUM_SCALAR >::nbOperations() const {
158  return float(__table.domainSize());
159  }
160 
162  template < typename GUM_SCALAR >
163  std::pair< long, long > ScheduleProject< GUM_SCALAR >::memoryUsage() const {
164  long size = 1;
165  const Sequence< const DiscreteVariable* >& seq = __table.variablesSequence();
166 
167  for (const auto var : seq)
168  if (!__del_vars.contains(var)) {
169  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
170  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
171  }
172 
173  size *= long(var->domainSize());
174  }
175 
176  return std::pair< long, long >(size, size);
177  }
178 
180  template < typename GUM_SCALAR >
181  INLINE const ScheduleMultiDim< GUM_SCALAR >&
183  return *__result;
184  }
185 
187  template < typename GUM_SCALAR >
188  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
190  if (!__args) {
191  __args = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
192  __args->insert(&__table);
193  }
194 
195  return *__args;
196  }
197 
199  template < typename GUM_SCALAR >
200  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
202  if (!__results) {
203  __results = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
204  __results->insert(__result);
205  }
206 
207  return *__results;
208  }
209 
211  template < typename GUM_SCALAR >
212  std::string ScheduleProject< GUM_SCALAR >::toString() const {
213  return __result->toString() + " = project ( " + __table.toString() + " , "
214  + __del_vars.toString() + " )";
215  }
216 
217 } /* namespace gum */
218 
219 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual ScheduleProject< GUM_SCALAR > * newFactory() const
virtual copy constructor: creates a clone of the operation
const ScheduleMultiDim< GUM_SCALAR > & result() const
returns the scheduleMultidim resulting from the execution of the operation
Type type() const
returns the name of the operation
std::pair< long, long > memoryUsage() const
returns the memory consumption used during the operation
float nbOperations() const
returns an estimation of the number of elementary operations needed to perform the ScheduleOperation ...
ScheduleMultiDim< GUM_SCALAR > __table
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void execute()
executes the operation
const Sequence< const ScheduleMultiDim< GUM_SCALAR > *> & multiDimArgs() const
returns the set of multidims passed in argument to the operation
bool operator!=(const ScheduleOperation< GUM_SCALAR > &) const
operator !=
ScheduleMultiDim< GUM_SCALAR > * __result
the result of the operation
Sequence< const ScheduleMultiDim< GUM_SCALAR > *> * __results
the set of ScheduleMultidims resulting from the operation
virtual ~ScheduleProject()
destructor
std::string toString() const
displays the content of the operation
const Sequence< const ScheduleMultiDim< GUM_SCALAR > *> & multiDimResults() const
returns the set of multidims that should be the result of the operation
ScheduleOperation(Type t)
default constructor
ScheduleOperation< GUM_SCALAR > & operator=(const ScheduleOperation< GUM_SCALAR > &)
copy operator
bool operator==(const ScheduleOperation< GUM_SCALAR > &) const
operator ==
ScheduleProject(const ScheduleMultiDim< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars, MultiDimImplementation< GUM_SCALAR > *(*project)(const MultiDimImplementation< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
default constructor
ScheduleProject< GUM_SCALAR > & operator=(const ScheduleProject< GUM_SCALAR > &)
copy operator
Set< const DiscreteVariable *> __del_vars
Sequence< const ScheduleMultiDim< GUM_SCALAR > *> * __args
the set of ScheduleMultidims passed in arguments
MultiDimImplementation< GUM_SCALAR > *(* __project)(const MultiDimImplementation< GUM_SCALAR > &, const Set< const DiscreteVariable * > &)
the projection operator
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55