aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimProjection_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 /**
23  * @file
24  * @brief A generic interface to project efficiently a MultiDim table over a
25  * subset of its variables
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 # include <agrum/agrum.h>
33 # include <limits>
34 
35 namespace gum {
36 
37  // constructor
38  template < typename GUM_SCALAR, template < typename > class TABLE >
39  MultiDimProjection< GUM_SCALAR, TABLE >::MultiDimProjection(
40  TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
41  const Set< const DiscreteVariable* >&)) :
42  proj_(proj) {
43  // for debugging purposes
44  GUM_CONSTRUCTOR(MultiDimProjection);
45  }
46 
47  // copy constructor
48  template < typename GUM_SCALAR, template < typename > class TABLE >
51  proj_(from.proj_) {
52  // for debugging purposes
54  }
55 
56  // destructor
57  template < typename GUM_SCALAR, template < typename > class TABLE >
59  // for debugging purposes
61  }
62 
63  // virtual constructor
64  template < typename GUM_SCALAR, template < typename > class TABLE >
67  return new MultiDimProjection< GUM_SCALAR, TABLE >(*this);
68  }
69 
70  // creates and returns the projection of the table over a subset of its vars
71  template < typename GUM_SCALAR, template < typename > class TABLE >
73  const TABLE< GUM_SCALAR >& table,
74  const Set< const DiscreteVariable* >& del_vars) {
75  return proj_(table, del_vars);
76  }
77 
78  // creates and returns the projection of the table over a subset of its vars
79  template < typename GUM_SCALAR, template < typename > class TABLE >
82  const TABLE< GUM_SCALAR >& table,
83  const Set< const TABLE< GUM_SCALAR >* >& del_vars) {
85  container = std::move(*res);
86  delete res;
87  }
88 
89  // changes the function used for projecting TABLES
90  template < typename GUM_SCALAR, template < typename > class TABLE >
92  TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
93  const Set< const DiscreteVariable* >&)) {
94  proj_ = proj;
95  }
96 
97  // returns the projection function currently used by the projector
98  template < typename GUM_SCALAR, template < typename > class TABLE >
99  INLINE TABLE< GUM_SCALAR >* (
101  const TABLE< GUM_SCALAR >&,
102  const Set< const DiscreteVariable* >&) {
103  return proj_;
104  }
105 
106  /** @brief returns a rough estimate of the number of operations that will be
107  * performed to compute the projection */
108  template < typename GUM_SCALAR, template < typename > class TABLE >
110  const TABLE< GUM_SCALAR >& table,
111  const Set< const DiscreteVariable* >& del_vars) const {
112  return float(table.domainSize());
113  }
114 
115  /** @brief returns a rough estimate of the number of operations that will be
116  * performed to compute the projection */
117  template < typename GUM_SCALAR, template < typename > class TABLE >
119  const Sequence< const DiscreteVariable* >& vars,
120  const Set< const DiscreteVariable* >& del_vars) const {
121  float res = 1.0f;
122 
123  for (typename Sequence< const DiscreteVariable* >::const_iterator_safe iter
124  = vars.beginSafe();
125  iter != vars.endSafe();
126  ++iter) {
127  res *= (*iter)->domainSize();
128  }
129 
130  return res;
131  }
132 
133  // returns the memory consumption used during the projection
134  template < typename GUM_SCALAR, template < typename > class TABLE >
136  const Sequence< const DiscreteVariable* >& vars,
137  const Set< const DiscreteVariable* >& del_vars) const {
138  long res = 1;
139 
140  for (typename Sequence< const DiscreteVariable* >::const_iterator_safe iter
141  = vars.beginSafe();
142  iter != vars.endSafe();
143  ++iter) {
144  if (!del_vars.contains(*iter)) {
145  if (std::numeric_limits< long >::max() / (long)(*iter)->domainSize()
146  < res) {
147  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
148  }
149 
150  res *= long((*iter)->domainSize());
151  }
152  }
153 
154  return std::pair< long, long >(res, res);
155  }
156 
157  // returns the memory consumption used during the projection
158  template < typename GUM_SCALAR, template < typename > class TABLE >
159  INLINE std::pair< long, long >
161  const TABLE< GUM_SCALAR >& table,
162  const Set< const DiscreteVariable* >& del_vars) const {
164  }
165 
166 } /* namespace gum */
167 
168 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669