aGrUM  0.14.2
multiDimWithOffset_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  ***************************************************************************/
27 #include <limits>
28 
29 // to ease IDE parsers...
32 
33 namespace gum {
34 
35  // Default constructor: creates an empty null dimensional matrix
36 
37  template < typename GUM_SCALAR >
39  MultiDimImplementation< GUM_SCALAR >() {
40  // for debugging purposes
41  GUM_CONSTRUCTOR(MultiDimWithOffset);
42  }
43 
44  // copy constructor
45 
46  template < typename GUM_SCALAR >
49  MultiDimImplementation< GUM_SCALAR >(from),
50  _gaps(from._gaps) {
51  // for debugging purposes
52  GUM_CONS_CPY(MultiDimWithOffset);
53  }
54 
55  // destructor
56 
57  template < typename GUM_SCALAR >
59  // for debugging purposes
60  GUM_DESTRUCTOR(MultiDimWithOffset);
61  // no need to unregister all slaves as it will be done by
62  // MultiDimImplementation
63  }
64 
65  // add a new dimension, needed for updating the _offsets & _gaps
66 
67  template < typename GUM_SCALAR >
69  Size lg = this->domainSize();
70 
71  if (lg > std::numeric_limits< Idx >::max() / v.domainSize()) {
72  GUM_ERROR(OutOfBounds, "Out of bounds !");
73  }
74 
76  _gaps.insert(&v, lg);
77  }
78 
79  // removes a dimension, needed for updating the _offsets & _gaps
80 
81  template < typename GUM_SCALAR >
84  Idx pos = variables.pos(&v); // throw a NotFound if necessary
85 
86  if (variables.size() == 1) {
87  _gaps.clear();
88  } else {
89  // update the _gaps
90  Size v_size = v.domainSize();
91  _gaps.erase(variables[pos]);
92 
93  for (Idx i = pos + 1; i < variables.size(); ++i) {
94  _gaps[variables[i]] /= v_size;
95  }
96  }
97 
99  }
100 
101  // listen to change in each recorded Instantiation.
102 
103  template < typename GUM_SCALAR >
105  const Instantiation& i,
106  const DiscreteVariable* const var,
107  Idx oldval,
108  Idx newval) {
109  GUM_ASSERT(_offsets.exists(&i));
110  GUM_ASSERT(_offsets[&i] < this->domainSize());
111  GUM_ASSERT(newval < var->domainSize());
112  GUM_ASSERT(oldval < var->domainSize());
113 
114  if (newval >= oldval) {
115  _offsets[&i] += _gaps[var] * (newval - oldval);
116  GUM_ASSERT(_offsets[&i] < this->domainSize());
117  } else {
118  GUM_ASSERT(_offsets[&i] >= _gaps[var] * (oldval - newval));
119  _offsets[&i] -= _gaps[var] * (oldval - newval);
120  }
121  }
122 
123  // listen to an assignment of a value in a Instantiation
124 
125  template < typename GUM_SCALAR >
127  const Instantiation& i) {
128  GUM_ASSERT(_offsets.exists(&i));
129  _offsets[&i] = _getOffs(i);
130  }
131 
132  // listen to setFirst in each recorded Instantiation.
133 
134  template < typename GUM_SCALAR >
136  const Instantiation& i) {
137  GUM_ASSERT(_offsets.exists(&i));
138  _offsets[&i] = 0;
139  }
140 
141  // listen to setLast in each recorded Instantiation.
142 
143  template < typename GUM_SCALAR >
145  const Instantiation& i) {
146  GUM_ASSERT(_offsets.exists(&i));
147  _offsets[&i] = this->domainSize() - 1;
148  }
149 
150  // listen to increment in each recorded Instantiation.
151 
152  template < typename GUM_SCALAR >
153  INLINE void
155  GUM_ASSERT(_offsets.exists(&i));
156  GUM_ASSERT(_offsets[&i] != this->domainSize() - 1);
157  ++_offsets[&i];
158  }
159 
160  // listen to increment in each recorded Instantiation.
161 
162  template < typename GUM_SCALAR >
163  INLINE void
165  GUM_ASSERT(_offsets.exists(&i));
166  GUM_ASSERT(_offsets[&i] != 0);
167  --_offsets[&i];
168  }
169 
170  // add a Instantiation as a slave
171 
172  template < typename GUM_SCALAR >
175  GUM_ASSERT(!_offsets.exists(&i));
176  _offsets.insert(&i, _getOffs(i));
177  return true;
178  }
179 
180  return false;
181  }
182 
183  // remove a registered slave instantiation
184 
185  template < typename GUM_SCALAR >
188  _offsets.erase(&i);
189  return true;
190  }
191 
192  // Compute the offset of a Instantiation
200  template < typename GUM_SCALAR >
201  INLINE Size
203  Idx off = 0;
204 
206  _gaps.beginSafe();
207  iter != _gaps.endSafe();
208  ++iter)
209  if (i.contains(iter.key()))
210  off += iter.val() * i.valFromPtr(iter.key());
211  else
213  iter.key()->name() << " not present in the instantiation " << i);
214 
215  return off;
216  }
217 
218  // For a given indice of a value in the vector _values, this method computes
219  // the corresponding instantiation
229  template < typename GUM_SCALAR >
231  Instantiation& result, Size indice) const {
232  for (Idx i = 0; i < this->nbrDim(); ++i) {
233  const DiscreteVariable& var = this->variable(i);
234  Idx domainSize = var.domainSize();
235  result.chgVal(var, indice % domainSize);
236  indice = indice / domainSize;
237  }
238 
239  GUM_ASSERT(indice == 0);
240  }
241 
242  // string representation of internal data about i in this.
243  template < typename GUM_SCALAR >
244  INLINE const std::string
246  if (i->isMaster(this)) {
247  std::stringstream s;
248  s << _offsets[i];
249  std::string res;
250  s >> res;
251  return res;
252  } else {
253  return "--";
254  }
255  }
256 
257  template < typename GUM_SCALAR >
258  INLINE Size
260  return _getOffs(i);
261  }
262 
263  // set the Instantiation to the values corresponding to the offset (in this
264  // array)
265  template < typename GUM_SCALAR >
266  INLINE Instantiation&
268  Size offset) const {
269  this->_computeInstantiationValue(i, offset);
270  return i;
271  }
272 
273 } /* namespace gum */
virtual void setLastNotification(const Instantiation &i)
Listen to setLast in a given Instantiation.
Instantiation & fromOffset(Instantiation &i, Size offset) const
Set the Instantiation to the values corresponding to the offset (in this array).
void _computeInstantiationValue(Instantiation &result, Size indice) const
For a given index of a value in the vector values, this method computes the corresponding instantiati...
void setDecNotification(const Instantiation &i)
Listen to increment in each recorded Instantiation.
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:515
Size toOffset(const Instantiation &i) const
Compute offset from an Instantiation (in this array).
virtual bool unregisterSlave(Instantiation &i)
Unregister i as a slave of this MultiDimAdressable.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
HashTable< const Instantiation *, Size > _offsets
The position in the array of each slave Instantiation.
virtual void setChangeNotification(const Instantiation &i)
Listen to an assignment of a value in a Instantiation.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:35
virtual void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
Safe Const Iterators for hashtables.
Definition: hashTable.h:1915
Headers of the MultiDimWithOffset class.
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual const std::string toString() const
Returns a representation of this MultiDimContainer.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)
Listen to changes in a given Instantiation.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
HashTable< const DiscreteVariable *, Size > _gaps
The gaps between consecutive values of a given variable.
Base class for discrete random variable.
virtual bool unregisterSlave(Instantiation &slave) override
Unregister i as a slave of this MultiDimAdressable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Headers of gum::MultiDimImplementation.
virtual ~MultiDimWithOffset()
Class destrucor.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual Size domainSize() const =0
Abstract class for Multidimensional matrix stored as an array in memory and with an offset associated...
MultiDimWithOffset()
Class constructor.
void setIncNotification(const Instantiation &i)
Listen to increment in a given Instantiation.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
Size _getOffs(const Instantiation &i) const
Compute the offset of a Instantiation.
virtual void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual Size domainSize() const override
Returns the product of the variables domain size.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
virtual void setFirstNotification(const Instantiation &i)
Listen to setFirst in a given Instantiation.
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
virtual Idx pos(const DiscreteVariable &v) const override
Returns the index of a variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
virtual bool registerSlave(Instantiation &i)
Register i as a slave of this MultiDimAdressable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52