aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scheduleProject_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief a Projection operator class used for scheduling inferences
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/agrum.h>
31 # include <limits>
32 
33 namespace gum {
34 
35  /// default constructor
36  template < typename GUM_SCALAR >
37  ScheduleProject< GUM_SCALAR >::ScheduleProject(
38  const ScheduleMultiDim< GUM_SCALAR >& table,
39  const Set< const DiscreteVariable* >& del_vars,
40  MultiDimImplementation< GUM_SCALAR >* (*project)(const MultiDimImplementation< GUM_SCALAR >&,
41  const Set< const DiscreteVariable* >&)) :
42  ScheduleOperation< GUM_SCALAR >(ScheduleOperation< GUM_SCALAR >::Type::PROJECT_MULTIDIM),
43  _table_(table), _del_vars_(del_vars), _args_(0), _results_(0), _project_(project) {
44  // for debugging purposes
45  GUM_CONSTRUCTOR(ScheduleProject);
46 
47  // compute the variables that shall belong to the result of the projection
48  Sequence< const DiscreteVariable* > vars = _table_.variablesSequence();
49 
50  for (const auto var: del_vars)
51  vars.erase(var);
52 
53  // create the scheduleMultiDim that should result from the combination of
54  // table1 and table2
55  _result_ = new ScheduleMultiDim< GUM_SCALAR >(vars);
56  }
57 
58  /// copy constructor
59  template < typename GUM_SCALAR >
64  // for debugging purposes
66  }
67 
68  /// virtual copy constructor: creates a clone of the operation
69  template < typename GUM_SCALAR >
71  return new ScheduleProject< GUM_SCALAR >(*this);
72  }
73 
74  /// destructor
75  template < typename GUM_SCALAR >
77  // for debugging purposes
79  delete _result_;
80 
81  if (_args_) delete _args_;
82 
83  if (_results_) delete _results_;
84  }
85 
86  /// copy operator
87  template < typename GUM_SCALAR >
90  // avoid self assignment
91  if (this != &from) {
95  *_result_ = *(from._result_);
97 
98  // update _args_ and _results_ if they were already created
99  if (_args_) {
100  _args_->clear();
101  _args_->insert(&_table_);
102  }
103 
104  if (_results_) {
105  _results_->clear();
107  }
108  }
109 
110  return *this;
111  }
112 
113  /// operator ==
114  template < typename GUM_SCALAR >
115  bool ScheduleProject< GUM_SCALAR >::operator==(const ScheduleOperation< GUM_SCALAR >& op) const {
116  if (this->type() != op.type()) return false;
117 
119  = static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
120  return ((_table_ == real_op._table_) && (_del_vars_ == real_op._del_vars_)
121  && (_project_ == real_op._project_));
122  }
123 
124  /// operator !=
125  template < typename GUM_SCALAR >
126  bool ScheduleProject< GUM_SCALAR >::operator!=(const ScheduleOperation< GUM_SCALAR >& op) const {
127  if (this->type() != op.type()) return true;
128 
130  = static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
131  return ((_table_ != real_op._table_) || (_del_vars_ != real_op._del_vars_)
132  || (_project_ != real_op._project_));
133  }
134 
135  /// executes the operation
136  template < typename GUM_SCALAR >
137  void ScheduleProject< GUM_SCALAR >::execute() {
138  if (_result_->isAbstract()) {
142  }
143  }
144 
145  /** @brief returns an estimation of the number of elementary operations
146  * needed to perform the ScheduleOperation */
147  template < typename GUM_SCALAR >
148  INLINE float ScheduleProject< GUM_SCALAR >::nbOperations() const {
149  return float(_table_.domainSize());
150  }
151 
152  /// returns the memory consumption used during the operation
153  template < typename GUM_SCALAR >
154  std::pair< long, long > ScheduleProject< GUM_SCALAR >::memoryUsage() const {
155  long size = 1;
157 
158  for (const auto var: seq)
159  if (!_del_vars_.contains(var)) {
160  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
161  GUM_ERROR(OutOfBounds, "memory usage out of long int range")
162  }
163 
164  size *= long(var->domainSize());
165  }
166 
167  return std::pair< long, long >(size, size);
168  }
169 
170  /// returns the scheduleMultidim resulting from the execution of the operation
171  template < typename GUM_SCALAR >
173  return *_result_;
174  }
175 
176  /// returns the set of multidims passed in argument to the operation
177  template < typename GUM_SCALAR >
178  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
180  if (!_args_) {
181  _args_ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
182  _args_->insert(&_table_);
183  }
184 
185  return *_args_;
186  }
187 
188  /// returns the set of multidims that should be the result of the operation
189  template < typename GUM_SCALAR >
190  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
192  if (!_results_) {
193  _results_ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
195  }
196 
197  return *_results_;
198  }
199 
200  /// displays the content of the operation
201  template < typename GUM_SCALAR >
203  return _result_->toString() + " = project ( " + _table_.toString() + " , "
204  + _del_vars_.toString() + " )";
205  }
206 
207 } /* namespace gum */
208 
209 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643