aGrUM  0.14.2
scheduleProject_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>
29 # include <limits>
30 
31 namespace gum {
32 
34  template < typename GUM_SCALAR >
36  const ScheduleMultiDim< GUM_SCALAR >& table,
37  const Set< const DiscreteVariable* >& del_vars,
38  MultiDimImplementation< GUM_SCALAR >* (*project)(
39  const MultiDimImplementation< GUM_SCALAR >&,
40  const Set< const DiscreteVariable* >&)) :
41  ScheduleOperation< GUM_SCALAR >(
42  ScheduleOperation< GUM_SCALAR >::Type::PROJECT_MULTIDIM),
43  __table(table), __del_vars(del_vars), __args(0), __results(0),
44  __project(project) {
45  // for debugging purposes
46  GUM_CONSTRUCTOR(ScheduleProject);
47 
48  // compute the variables that shall belong to the result of the projection
49  Sequence< const DiscreteVariable* > vars = __table.variablesSequence();
50 
51  for (const auto var : del_vars)
52  vars.erase(var);
53 
54  // create the scheduleMultiDim that should result from the combination of
55  // table1 and table2
56  __result = new ScheduleMultiDim< GUM_SCALAR >(vars);
57  }
58 
60  template < typename GUM_SCALAR >
62  const ScheduleProject< GUM_SCALAR >& f) :
63  ScheduleOperation< GUM_SCALAR >(f),
65  __result(new ScheduleMultiDim< GUM_SCALAR >(*(f.__result))), __args(0),
67  // for debugging purposes
68  GUM_CONS_CPY(ScheduleProject);
69  }
70 
72  template < typename GUM_SCALAR >
73  ScheduleProject< GUM_SCALAR >*
75  return new ScheduleProject< GUM_SCALAR >(*this);
76  }
77 
79  template < typename GUM_SCALAR >
81  // for debugging purposes
82  GUM_DESTRUCTOR(ScheduleProject);
83  delete __result;
84 
85  if (__args) delete __args;
86 
87  if (__results) delete __results;
88  }
89 
91  template < typename GUM_SCALAR >
92  ScheduleProject< GUM_SCALAR >& ScheduleProject< GUM_SCALAR >::
93  operator=(const ScheduleProject< GUM_SCALAR >& from) {
94  // avoid self assignment
95  if (this != &from) {
97  __table = from.__table;
98  __del_vars = from.__del_vars;
99  *__result = *(from.__result);
100  __project = from.__project;
101 
102  // update __args and __results if they were already created
103  if (__args) {
104  __args->clear();
105  __args->insert(&__table);
106  }
107 
108  if (__results) {
109  __results->clear();
110  __results->insert(__result);
111  }
112  }
113 
114  return *this;
115  }
116 
118  template < typename GUM_SCALAR >
120  operator==(const ScheduleOperation< GUM_SCALAR >& op) const {
121  if (this->type() != op.type()) return false;
122 
123  const ScheduleProject< GUM_SCALAR >& real_op =
124  static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
125  return ((__table == real_op.__table) && (__del_vars == real_op.__del_vars)
126  && (__project == real_op.__project));
127  }
128 
130  template < typename GUM_SCALAR >
132  operator!=(const ScheduleOperation< GUM_SCALAR >& op) const {
133  if (this->type() != op.type()) return true;
134 
135  const ScheduleProject< GUM_SCALAR >& real_op =
136  static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
137  return ((__table != real_op.__table) || (__del_vars != real_op.__del_vars)
138  || (__project != real_op.__project));
139  }
140 
142  template < typename GUM_SCALAR >
144  if (__result->isAbstract()) {
145  const MultiDimImplementation< GUM_SCALAR >& t = __table.multiDim();
146  MultiDimImplementation< GUM_SCALAR >* res = __project(t, __del_vars);
147  __result->setMultiDim(*res);
148  }
149  }
150 
153  template < typename GUM_SCALAR >
154  INLINE float ScheduleProject< GUM_SCALAR >::nbOperations() const {
155  return float(__table.domainSize());
156  }
157 
159  template < typename GUM_SCALAR >
160  std::pair< long, long > ScheduleProject< GUM_SCALAR >::memoryUsage() const {
161  long size = 1;
162  const Sequence< const DiscreteVariable* >& seq = __table.variablesSequence();
163 
164  for (const auto var : seq)
165  if (!__del_vars.contains(var)) {
166  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
167  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
168  }
169 
170  size *= long(var->domainSize());
171  }
172 
173  return std::pair< long, long >(size, size);
174  }
175 
177  template < typename GUM_SCALAR >
178  INLINE const ScheduleMultiDim< GUM_SCALAR >&
180  return *__result;
181  }
182 
184  template < typename GUM_SCALAR >
185  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
187  if (!__args) {
188  __args = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
189  __args->insert(&__table);
190  }
191 
192  return *__args;
193  }
194 
196  template < typename GUM_SCALAR >
197  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
199  if (!__results) {
200  __results = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
201  __results->insert(__result);
202  }
203 
204  return *__results;
205  }
206 
208  template < typename GUM_SCALAR >
209  std::string ScheduleProject< GUM_SCALAR >::toString() const {
210  return __result->toString() + " = project ( " + __table.toString() + " , "
211  + __del_vars.toString() + " )";
212  }
213 
214 } /* namespace gum */
215 
216 #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
gum is the global namespace for all aGrUM entities
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:52