49#ifndef DOXYGEN_SHOULD_SKIP_THIS
58 template <
typename TABLE >
61 TABLE (*project)(
const TABLE&,
63 const bool is_result_persistent) :
65 _arg_(&table), _del_vars_(del_vars), _project_(project) {
67 Sequence< const DiscreteVariable* > vars = table.variablesSequence();
68 for (
const auto var: del_vars) {
69 if (vars.exists(var)) vars.erase(var);
74 if (table.variablesSequence().empty() && !table.isAbstract())
75 _result_ =
new ScheduleMultiDim< TABLE >(table.multiDim(),
true, Idx(0));
76 else _result_ =
new ScheduleMultiDim< TABLE >(vars, Idx(0));
80 _results_ << _result_;
83 GUM_CONSTRUCTOR(ScheduleProjection);
87 template <
typename TABLE >
88 ScheduleProjection< TABLE >::ScheduleProjection(
const ScheduleProjection< TABLE >& from) :
89 ScheduleOperator(from), _arg_(from._arg_), _del_vars_(from._del_vars_),
90 _project_(from._project_) {
92 _result_ =
new ScheduleMultiDim< TABLE >(*(from._result_));
96 _results_ << _result_;
99 GUM_CONS_CPY(ScheduleProjection);
103 template <
typename TABLE >
104 ScheduleProjection< TABLE >::ScheduleProjection(ScheduleProjection< TABLE >&& from) :
105 ScheduleOperator(
std::move(from)), _arg_(from._arg_), _result_(from._result_),
106 _del_vars_(
std::move(from._del_vars_)), _project_(from._project_) {
108 from.makeResultsPersistent(
true);
109 from._result_ =
nullptr;
113 _results_ << _result_;
116 GUM_CONS_MOV(ScheduleProjection);
120 template <
typename TABLE >
121 INLINE ScheduleProjection< TABLE >* ScheduleProjection< TABLE >::clone()
const {
122 return new ScheduleProjection< TABLE >(*
this);
126 template <
typename TABLE >
127 ScheduleProjection< TABLE >::~ScheduleProjection() {
128 if (!this->hasPersistentResults()) {
delete _result_; }
131 GUM_DESTRUCTOR(ScheduleProjection);
135 template <
typename TABLE >
136 ScheduleProjection< TABLE >&
137 ScheduleProjection< TABLE >::operator=(
const ScheduleProjection< TABLE >& from) {
145 *_result_ = *(from._result_);
146 ScheduleOperator::operator=(from);
148 _del_vars_ = std::move(new_del_vars);
152 _project_ = from._project_;
158 template <
typename TABLE >
159 ScheduleProjection< TABLE >&
160 ScheduleProjection< TABLE >::operator=(ScheduleProjection< TABLE >&& from) {
163 if (!this->hasPersistentResults())
delete _result_;
164 _result_ = from._result_;
165 ScheduleOperator::operator=(std::move(from));
167 _del_vars_ = std::move(from._del_vars_);
171 _project_ = from._project_;
173 from.makeResultsPersistent(
true);
174 from._result_ =
nullptr;
180 template <
typename TABLE >
181 INLINE
bool ScheduleProjection< TABLE >::operator==(
const ScheduleProjection< TABLE >& op)
const {
182 return (_project_ == op._project_) && (*_arg_ == *op._arg_) && (_del_vars_ == op._del_vars_);
186 template <
typename TABLE >
187 bool ScheduleProjection< TABLE >::operator==(
const ScheduleOperator& op)
const {
188 if (ScheduleOperator::operator!=(op))
return false;
191 const ScheduleProjection< TABLE >& real_op
192 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
193 return ScheduleProjection< TABLE >::operator==(real_op);
194 }
catch (std::bad_cast&) {
return false; }
198 template <
typename TABLE >
199 INLINE
bool ScheduleProjection< TABLE >::operator!=(
const ScheduleOperator& op)
const {
200 return !ScheduleProjection< TABLE >::operator==(op);
204 template <
typename TABLE >
205 INLINE
bool ScheduleProjection< TABLE >::operator!=(
const ScheduleProjection< TABLE >& op)
const {
206 return !ScheduleProjection< TABLE >::operator==(op);
210 template <
typename TABLE >
211 INLINE
bool ScheduleProjection< TABLE >::hasSimilarArguments(
212 const ScheduleProjection< TABLE >& op)
const {
213 return (_arg_->hasSameVariables(*op._arg_) && (_del_vars_ == op._del_vars_));
217 template <
typename TABLE >
218 bool ScheduleProjection< TABLE >::hasSimilarArguments(
const ScheduleOperator& op)
const {
220 const ScheduleProjection< TABLE >& real_op
221 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
222 return ScheduleProjection< TABLE >::hasSimilarArguments(real_op);
223 }
catch (std::bad_cast&) {
return false; }
227 template <
typename TABLE >
229 ScheduleProjection< TABLE >::hasSameArguments(
const ScheduleProjection< TABLE >& op)
const {
230 return (_arg_->hasSameVariables(*op._arg_) && _arg_->hasSameContent(*op._arg_)
231 && (_del_vars_ == op._del_vars_));
235 template <
typename TABLE >
236 bool ScheduleProjection< TABLE >::hasSameArguments(
const ScheduleOperator& op)
const {
238 const ScheduleProjection< TABLE >& real_op
239 =
dynamic_cast< const ScheduleProjection< TABLE >&
>(op);
240 return ScheduleProjection< TABLE >::hasSameArguments(real_op);
241 }
catch (std::bad_cast&) {
return false; }
245 template <
typename TABLE >
247 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 INLINE
const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::arg()
const {
268 template <
typename TABLE >
269 INLINE
const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::args()
const {
274 template <
typename TABLE >
275 INLINE
const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::result()
const {
280 template <
typename TABLE >
281 INLINE
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 INLINE
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