aGrUM  0.16.0
multiDimContainer_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/agrum.h>
31 #include <algorithm>
32 
33 
34 namespace gum {
35 
36  template < typename GUM_SCALAR >
39  MultiDimAdressable(std::forward< MultiDimAdressable >(from)) {
40  GUM_CONS_MOV(MultiDimContainer);
41  }
42 
43  // Default constructor
44  template < typename GUM_SCALAR >
47  GUM_CONSTRUCTOR(MultiDimContainer);
48  }
49 
50  // Copy constructor
51  template < typename GUM_SCALAR >
54  MultiDimAdressable(src) {
55  GUM_CONS_CPY(MultiDimContainer);
56  }
57  template < typename GUM_SCALAR >
61  return *this;
62  }
63  template < typename GUM_SCALAR >
66  GUM_OP_MOV(MultiDimContainer);
67  MultiDimAdressable::operator=(std::forward< MultiDimAdressable >(from));
68  return *this;
69  }
70 
71  // destructor
72 
73  template < typename GUM_SCALAR >
75  GUM_DESTRUCTOR(MultiDimContainer);
76  }
77 
78  // an [] operator using a Instantiation as argument
79 
80  template < typename GUM_SCALAR >
81  INLINE GUM_SCALAR MultiDimContainer< GUM_SCALAR >::
82  operator[](const Instantiation& i) const {
83  return get(i);
84  }
85 
86  // an [] operator using a Instantiation as argument
87 
88  template < typename GUM_SCALAR >
90  const GUM_SCALAR& value) const {
91  _get(i) = value;
92  }
93 
94  // an [] operator using a Instantiation as argument
95 
96  template < typename GUM_SCALAR >
97  INLINE GUM_SCALAR
99  return _get(i);
100  }
101 
102  // display the content of an array
103 
104  template < typename GUM_SCALAR >
105  const std::string MultiDimContainer< GUM_SCALAR >::toString() const {
106  // we create a new instantiation and iterate over it to display the whole
107  // content of the array
108  if (this->nbrDim() == 0) { return "[]"; }
109 
110  std::stringstream ss;
111  Instantiation inst(const_cast< MultiDimContainer* >(this));
112 
113  bool first = true;
114 
115  for (inst.setFirst(); !inst.end(); ++inst) {
116  if (!first) { ss << " /"; }
117  first = false;
118 
119  ss << inst << " :: " << get(inst);
120  }
121 
122  return ss.str();
123  }
124 
125  // Test if this potential is equal to p.
126 
127  template < typename GUM_SCALAR >
130  if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
131  if (nbrDim() == 0) return true;
132 
134  var_iterator;
135 
136  for (var_iterator iter = variablesSequence().beginSafe();
137  iter != variablesSequence().endSafe();
138  ++iter) {
139  if (!p.variablesSequence().exists(*iter)) { return false; }
140  }
141  } else {
142  return false;
143  }
144 
145  Instantiation i(*this);
146 
148 
149  for (i.setFirst(); !i.end(); ++i) {
150  if (cmp(get(i), p.get(i))) { return false; }
151  }
152 
153  return true;
154  }
155 
156  // Test if this potential is different of p.
157 
158  template < typename GUM_SCALAR >
161  return !operator==(p);
162  }
163 
164  // automation fill with vector.
165  template < typename GUM_SCALAR >
167  const std::vector< GUM_SCALAR >& v) const {
168  if (domainSize() != v.size()) {
169  GUM_ERROR(SizeError, "Sizes do not match in populate");
170  }
171 
172  Size cpt = 0;
173 
174  Instantiation i(*this);
175 
176  for (i.setFirst(); !i.end(); ++i, ++cpt)
177  set(i, v[cpt]);
178  }
179 
180  template < typename GUM_SCALAR >
182  std::initializer_list< GUM_SCALAR > l) const {
183  if (domainSize() != l.size()) {
184  GUM_ERROR(SizeError, "Sizes do not match in populate");
185  }
186 
187  Instantiation i(*this);
188  // insert all the elements
189  for (const auto& elt : l) {
190  set(i, elt);
191  ++i;
192  }
193  }
194 
195  template < typename GUM_SCALAR >
197  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
198  Instantiation i(*this);
199  for (i.setFirst(); !i.end(); ++i) {
200  set(i, f(get(i)));
201  }
202  }
203 
204  template < typename GUM_SCALAR >
206  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
207  GUM_SCALAR base) const {
208  GUM_SCALAR tmp = base;
209  Instantiation i(*this);
210  for (i.setFirst(); !i.end(); ++i) {
211  tmp = f(tmp, get(i));
212  }
213  return tmp;
214  }
215 
216 
217  template < typename GUM_SCALAR >
219  const MultiDimContainer< GUM_SCALAR >& src, Instantiation* p_i) const {
220  if (src.domainSize() != domainSize()) {
221  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
222  }
223 
224  if (p_i == nullptr) { // if null, we just follow the same order
225  Instantiation i(src);
226  for (i.setFirst(); !i.end(); ++i) {
227  set(i, src[i]);
228  }
229  } else {
230  Instantiation i_dest(*this);
231  Instantiation i_src(src);
232  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end();
233  i_dest.incIn(*p_i), ++i_src) {
234  set(i_dest, src[i_src]);
235  }
236  }
237  }
238 
239  template < typename GUM_SCALAR >
241  const MultiDimContainer< GUM_SCALAR >& src, const Instantiation& imask) {
242  this->beginMultipleChanges();
243 
244  Size nbr = this->nbrDim();
245 
246  for (Idx i = 0; i < nbr; i++) {
247  this->erase(this->variable(0));
248  }
249 
250  for (Idx i = 0; i < src.nbrDim(); i++) {
251  if (!imask.contains(src.variable(i))) this->add(src.variable(i));
252  }
253 
254  if (this->nbrDim() == 0) { GUM_ERROR(FatalError, "Empty potential"); }
255 
256  this->endMultipleChanges();
257 
258  Instantiation inst(src);
259  inst.setVals(imask);
260  for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
261  set(inst, src[inst]);
262  }
263 
264  template < typename GUM_SCALAR >
266  const MultiDimContainer< GUM_SCALAR >& src) const {
267  if (src.domainSize() != domainSize()) {
268  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
269  }
270 
271  Instantiation i_dest(*this);
272  Instantiation i_src(src);
273 
274  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
275  set(i_dest, src[i_src]);
276  }
277  }
278 
279  // copy
280 
281  template < typename GUM_SCALAR >
283  const MultiDimContainer< GUM_SCALAR >& src) {
284  this->beginMultipleChanges();
285 
286  Size nbr = this->nbrDim();
287 
288  for (Idx i = 0; i < nbr; i++) {
289  this->erase(this->variable(0));
290  }
291 
292  for (Idx i = 0; i < src.nbrDim(); i++) {
293  this->add(src.variable(i));
294  }
295 
296  this->endMultipleChanges();
297  this->copyFrom(src);
298  }
299 
300  template < typename GUM_SCALAR >
302  return static_cast< MultiDimAdressable& >(*content());
303  }
304 
305  template < typename GUM_SCALAR >
306  INLINE const MultiDimAdressable&
308  return static_cast< const MultiDimAdressable& >(*content());
309  }
310 
311  // display the content of an array
312 
313  template < typename GUM_SCALAR >
314  std::ostream& operator<<(std::ostream& out,
315  const MultiDimContainer< GUM_SCALAR >& array) {
316  out << array.toString();
317  return out;
318  }
319 
320 } /* namespace gum */
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
Safe iterators for Sequence.
Definition: sequence.h:1206
virtual ~MultiDimContainer()
Destructor.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
STL namespace.
virtual GUM_SCALAR & _get(const Instantiation &i) const =0
Return a data, given a Instantiation.
virtual const std::string toString() const
Returns a representation of this MultiDimContainer.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
Indicate whether two elements are (almost) different or not.
Definition: utils_misc.h:155
virtual void copy(const MultiDimContainer< GUM_SCALAR > &src)
Removes all variables in this MultiDimContainer and copy the content of src, variables included...
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Changes the value pointed by i.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
compute lfold for this container
virtual Size domainSize() const =0
Returns the product of the variables domain size.
MultiDimAdressable & operator=(const MultiDimAdressable &from)
Default constructor.
bool operator!=(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is different of p.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
GUM_SCALAR operator[](const Instantiation &i) const
An [] operator using a Instantiation as argument.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
MultiDimContainer()
Default constructor.
void setFirst()
Assign the first values to the tuple of the Instantiation.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const
Apply a function on every element of the container.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
bool operator==(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is equal to p.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual MultiDimAdressable & getMasterRef()
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
virtual void populate(const std::vector< GUM_SCALAR > &v) const
Automatically fills this MultiDimContainer with the values in v.
bool end() const
Returns true if the Instantiation reached the end.
virtual void extractFrom(const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
Basic extraction of a MultiDimContainer.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
MultiDimContainer & operator=(const MultiDimContainer< GUM_SCALAR > &src)
Default constructor.