51#ifndef DOXYGEN_SHOULD_SKIP_THIS
60 template <
typename TABLE >
63 TABLE (*project)(
const TABLE&,
65 const bool is_result_persistent) :
67 _arg_(&table), _del_vars_(del_vars), _project_(project) {
69 Sequence< const DiscreteVariable* > vars = table.variablesSequence();
70 for (
const auto var: del_vars) {
71 if (vars.exists(var)) vars.erase(var);
76 if (table.variablesSequence().empty() && !table.isAbstract())
77 _result_ =
new ScheduleMultiDim< TABLE >(table.multiDim(),
true, Idx(0));
78 else _result_ =
new ScheduleMultiDim< TABLE >(vars, Idx(0));
82 _results_ << _result_;
85 GUM_CONSTRUCTOR(ScheduleProjection);
89 template <
typename TABLE >
90 ScheduleProjection< TABLE >::ScheduleProjection(
const ScheduleProjection< TABLE >& from) :
91 ScheduleOperator(from), _arg_(from._arg_), _del_vars_(from._del_vars_),
92 _project_(from._project_) {
94 _result_ =
new ScheduleMultiDim< TABLE >(*(from._result_));
98 _results_ << _result_;
101 GUM_CONS_CPY(ScheduleProjection);
105 template <
typename TABLE >
106 ScheduleProjection< TABLE >::ScheduleProjection(ScheduleProjection< TABLE >&& from) :
107 ScheduleOperator(
std::move(from)), _arg_(from._arg_), _result_(from._result_),
108 _del_vars_(
std::move(from._del_vars_)), _project_(from._project_) {
110 from.makeResultsPersistent(
true);
111 from._result_ =
nullptr;
115 _results_ << _result_;
118 GUM_CONS_MOV(ScheduleProjection);
122 template <
typename TABLE >
123 ScheduleProjection< TABLE >* ScheduleProjection< TABLE >::clone()
const {
124 return new ScheduleProjection< TABLE >(*
this);
128 template <
typename TABLE >
129 ScheduleProjection< TABLE >::~ScheduleProjection() {
130 if (!this->hasPersistentResults()) {
delete _result_; }
133 GUM_DESTRUCTOR(ScheduleProjection);
137 template <
typename TABLE >
138 ScheduleProjection< TABLE >&
139 ScheduleProjection< TABLE >::operator=(
const ScheduleProjection< TABLE >& from) {
147 *_result_ = *(from._result_);
148 ScheduleOperator::operator=(from);
150 _del_vars_ = std::move(new_del_vars);
154 _project_ = from._project_;
160 template <
typename TABLE >
161 ScheduleProjection< TABLE >&
162 ScheduleProjection< TABLE >::operator=(ScheduleProjection< TABLE >&& from) {
165 if (!this->hasPersistentResults())
delete _result_;
166 _result_ = from._result_;
167 ScheduleOperator::operator=(std::move(from));
169 _del_vars_ = std::move(from._del_vars_);
173 _project_ = from._project_;
175 from.makeResultsPersistent(
true);
176 from._result_ =
nullptr;
182 template <
typename TABLE >
183 bool ScheduleProjection< TABLE >::operator==(
const ScheduleProjection< TABLE >& op)
const {
184 return (_project_ == op._project_) && (*_arg_ == *op._arg_) && (_del_vars_ == op._del_vars_);
188 template <
typename TABLE >
189 bool ScheduleProjection< TABLE >::operator==(
const ScheduleOperator& op)
const {
190 if (ScheduleOperator::operator!=(op))
return false;
193 const ScheduleProjection< TABLE >& real_op
194 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
195 return ScheduleProjection< TABLE >::operator==(real_op);
196 }
catch (std::bad_cast&) {
return false; }
200 template <
typename TABLE >
201 bool ScheduleProjection< TABLE >::operator!=(
const ScheduleOperator& op)
const {
202 return !ScheduleProjection< TABLE >::operator==(op);
206 template <
typename TABLE >
207 bool ScheduleProjection< TABLE >::operator!=(
const ScheduleProjection< TABLE >& op)
const {
208 return !ScheduleProjection< TABLE >::operator==(op);
212 template <
typename TABLE >
213 bool ScheduleProjection< TABLE >::hasSimilarArguments(
214 const ScheduleProjection< TABLE >& op)
const {
215 return (_arg_->hasSameVariables(*op._arg_) && (_del_vars_ == op._del_vars_));
219 template <
typename TABLE >
220 bool ScheduleProjection< TABLE >::hasSimilarArguments(
const ScheduleOperator& op)
const {
222 const ScheduleProjection< TABLE >& real_op
223 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
224 return ScheduleProjection< TABLE >::hasSimilarArguments(real_op);
225 }
catch (std::bad_cast&) {
return false; }
229 template <
typename TABLE >
230 bool ScheduleProjection< TABLE >::hasSameArguments(
const ScheduleProjection< TABLE >& op)
const {
231 return (_arg_->hasSameVariables(*op._arg_) && _arg_->hasSameContent(*op._arg_)
232 && (_del_vars_ == op._del_vars_));
236 template <
typename TABLE >
237 bool ScheduleProjection< TABLE >::hasSameArguments(
const ScheduleOperator& op)
const {
239 const ScheduleProjection< TABLE >& real_op
240 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
241 return ScheduleProjection< TABLE >::hasSameArguments(real_op);
242 }
catch (std::bad_cast&) {
return false; }
246 template <
typename TABLE >
247 bool ScheduleProjection< TABLE >::isSameOperator(
const ScheduleProjection< TABLE >& op)
const {
248 return _project_ == op._project_;
252 template <
typename TABLE >
253 bool ScheduleProjection< TABLE >::isSameOperator(
const ScheduleOperator& op)
const {
255 const ScheduleProjection< TABLE >& real_op
256 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
257 return ScheduleProjection< TABLE >::isSameOperator(real_op);
258 }
catch (std::bad_cast&) {
return false; }
262 template <
typename TABLE >
263 const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::arg()
const {
268 template <
typename TABLE >
269 const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::args()
const {
274 template <
typename TABLE >
275 const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::result()
const {
280 template <
typename TABLE >
281 const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::results()
const {
286 template <
typename TABLE >
287 void ScheduleProjection< TABLE >::updateArgs(
288 const Sequence< const IScheduleMultiDim* >& new_args) {
291 if (new_args.size() != Size(1)) {
293 "Method ScheduleProjection::updateArgs expects 1 new "
294 <<
"argument, but " << new_args.size() <<
" were passed.");
296 const ScheduleMultiDim< TABLE >* new_table;
298 new_table =
dynamic_cast< const ScheduleMultiDim< TABLE >*
>(new_args[0]);
299 }
catch (std::bad_cast&) {
301 "The type of the argument passed to "
302 <<
"ScheduleProjection::updateArgs does not match what "
303 <<
"the ScheduleOperator expects");
307 if (new_table->variablesSequence().empty() && !new_table->isAbstract()) {
308 *_result_ = std::move(ScheduleMultiDim< TABLE >(new_table->multiDim(),
true, _result_->id()));
311 Sequence< const DiscreteVariable* > vars = new_table->variablesSequence();
312 for (
const auto var: _del_vars_) {
313 if (vars.exists(var)) vars.erase(var);
316 *_result_ = std::move(ScheduleMultiDim< TABLE >(vars, _result_->id()));
325 template <
typename TABLE >
326 bool ScheduleProjection< TABLE >::isExecuted()
const {
327 return !_result_->isAbstract();
331 template <
typename TABLE >
332 void ScheduleProjection< TABLE >::execute() {
333 if (_result_->isAbstract()) {
334 const TABLE& tab = _arg_->multiDim();
335 if (_arg_->domainSize() > 1) {
336 TABLE res = _project_(tab, _del_vars_);
337 _result_->setMultiDim(std::move(res));
339 _result_->setMultiDim(tab,
true);
345 template <
typename TABLE >
346 void ScheduleProjection< TABLE >::undo() {
347 _result_->makeAbstract();
352 template <
typename TABLE >
353 double ScheduleProjection< TABLE >::nbOperations()
const {
354 return double(_arg_->domainSize());
358 template <
typename TABLE >
359 std::pair< double, double > ScheduleProjection< TABLE >::memoryUsage()
const {
361 =
double(_result_->domainSize()) * _result_->sizeOfContent() +
sizeof(TABLE);
362 return {domsize, domsize};
366 template <
typename TABLE >
367 std::string ScheduleProjection< TABLE >::toString()
const {
368 return _result_->toString() +
" = project ( " + _arg_->toString() +
" , "
369 + _del_vars_.toString() +
" )";
373 template <
typename TABLE >
374 void ScheduleProjection< TABLE >::setProjectionFunction(
377 _result_->makeAbstract();
a Wrapper for multi-dimensional tables used for scheduling inferences
the base class for "low-level" operators used to schedule inferences
ScheduleProjection(const ScheduleMultiDim< TABLE > &table, const gum::VariableSet &del_vars, TABLE(*project)(const TABLE &, const gum::VariableSet &), const bool is_result_persistent=false)
default constructor
Exception : problem with size.
Exception : wrong type for this operation.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities
Set< const DiscreteVariable * > VariableSet
ScheduleOperatorType
the currently supported types of "low-level" operators
@ PROJECT_MULTIDIM
project a ScheduleMultiDim over a subset of its variables
a Projection operator class used for scheduling inferences