aGrUM  0.14.2
multiDimProjection_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  ***************************************************************************/
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/agrum.h>
31 # include <limits>
32 
33 namespace gum {
34 
35  // constructor
36  template < typename GUM_SCALAR, template < typename > class TABLE >
38  TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
39  const Set< const DiscreteVariable* >&)) :
40  _proj(proj) {
41  // for debugging purposes
42  GUM_CONSTRUCTOR(MultiDimProjection);
43  }
44 
45  // copy constructor
46  template < typename GUM_SCALAR, template < typename > class TABLE >
48  const MultiDimProjection< GUM_SCALAR, TABLE >& from) :
49  _proj(from._proj) {
50  // for debugging purposes
51  GUM_CONS_CPY(MultiDimProjection);
52  }
53 
54  // destructor
55  template < typename GUM_SCALAR, template < typename > class TABLE >
57  // for debugging purposes
58  GUM_DESTRUCTOR(MultiDimProjection);
59  }
60 
61  // virtual constructor
62  template < typename GUM_SCALAR, template < typename > class TABLE >
63  MultiDimProjection< GUM_SCALAR, TABLE >*
65  return new MultiDimProjection< GUM_SCALAR, TABLE >(*this);
66  }
67 
68  // creates and returns the projection of the table over a subset of its vars
69  template < typename GUM_SCALAR, template < typename > class TABLE >
70  INLINE TABLE< GUM_SCALAR >* MultiDimProjection< GUM_SCALAR, TABLE >::project(
71  const TABLE< GUM_SCALAR >& table,
72  const Set< const DiscreteVariable* >& del_vars) {
73  return _proj(table, del_vars);
74  }
75 
76  // creates and returns the projection of the table over a subset of its vars
77  template < typename GUM_SCALAR, template < typename > class TABLE >
79  TABLE< GUM_SCALAR >& container,
80  const TABLE< GUM_SCALAR >& table,
81  const Set< const TABLE< GUM_SCALAR >* >& del_vars) {
82  TABLE< GUM_SCALAR >* res = project(table, del_vars);
83  container = *res;
84  delete res;
85  }
86 
87  // changes the function used for projecting TABLES
88  template < typename GUM_SCALAR, template < typename > class TABLE >
90  TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
91  const Set< const DiscreteVariable* >&)) {
92  _proj = proj;
93  }
94 
95  // returns the projection function currently used by the projector
96  template < typename GUM_SCALAR, template < typename > class TABLE >
97  INLINE TABLE< GUM_SCALAR >* (
99  const TABLE< GUM_SCALAR >&, const Set< const DiscreteVariable* >&) {
100  return _proj;
101  }
102 
105  template < typename GUM_SCALAR, template < typename > class TABLE >
107  const TABLE< GUM_SCALAR >& table,
108  const Set< const DiscreteVariable* >& del_vars) const {
109  return float(table.domainSize());
110  }
111 
114  template < typename GUM_SCALAR, template < typename > class TABLE >
116  const Sequence< const DiscreteVariable* >& vars,
117  const Set< const DiscreteVariable* >& del_vars) const {
118  float res = 1.0f;
119 
121  vars.beginSafe();
122  iter != vars.endSafe();
123  ++iter) {
124  res *= (*iter)->domainSize();
125  }
126 
127  return res;
128  }
129 
130  // returns the memory consumption used during the projection
131  template < typename GUM_SCALAR, template < typename > class TABLE >
133  const Sequence< const DiscreteVariable* >& vars,
134  const Set< const DiscreteVariable* >& del_vars) const {
135  long res = 1;
136 
138  vars.beginSafe();
139  iter != vars.endSafe();
140  ++iter) {
141  if (!del_vars.contains(*iter)) {
142  if (std::numeric_limits< long >::max() / (long)(*iter)->domainSize()
143  < res) {
144  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
145  }
146 
147  res *= long((*iter)->domainSize());
148  }
149  }
150 
151  return std::pair< long, long >(res, res);
152  }
153 
154  // returns the memory consumption used during the projection
155  template < typename GUM_SCALAR, template < typename > class TABLE >
156  INLINE std::pair< long, long >
158  const TABLE< GUM_SCALAR >& table,
159  const Set< const DiscreteVariable* >& del_vars) const {
160  return memoryUsage(table.variablesSequence(), del_vars);
161  }
162 
163 } /* namespace gum */
164 
165 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
TABLE< GUM_SCALAR > *(*)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable *> &) projectFunction()
Returns the projection function currently used by the projector.
MultiDimProjection(TABLE< GUM_SCALAR > *(*proj)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
Default constructor.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::pair< long, long > memoryUsage(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars) const
Returns the memory consumption used during the projection.
virtual MultiDimProjection< GUM_SCALAR, TABLE > * newFactory() const
virtual constructor
TABLE< GUM_SCALAR > *(* _proj)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable * > &)
The projection function actually used.
virtual ~MultiDimProjection()
Destructor.
float nbOperations(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars) const
returns a rough estimate of the number of operations that will be performed to compute the projection...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
void setProjectFunction(TABLE< GUM_SCALAR > *(*proj)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
Changes the function used for projecting TABLES.
TABLE< GUM_SCALAR > * project(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars)
Creates and returns the projection of the table over a subset of its vars.
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition: sequence.h:1035