aGrUM  0.16.0
multiDimWithOffset_tpl.h
Go to the documentation of this file.
1 
30 #include <limits>
31 
32 // to ease IDE parsers...
35 
36 namespace gum {
37 
38  // Default constructor: creates an empty null dimensional matrix
39 
40  template < typename GUM_SCALAR >
42  MultiDimImplementation< GUM_SCALAR >() {
43  // for debugging purposes
44  GUM_CONSTRUCTOR(MultiDimWithOffset);
45  }
46 
47  // copy constructor
48 
49  template < typename GUM_SCALAR >
52  MultiDimImplementation< GUM_SCALAR >(from),
53  _gaps(from._gaps) {
54  // for debugging purposes
55  GUM_CONS_CPY(MultiDimWithOffset);
56  }
57 
58  // destructor
59 
60  template < typename GUM_SCALAR >
62  // for debugging purposes
63  GUM_DESTRUCTOR(MultiDimWithOffset);
64  // no need to unregister all slaves as it will be done by
65  // MultiDimImplementation
66  }
67 
68  // add a new dimension, needed for updating the _offsets & _gaps
69 
70  template < typename GUM_SCALAR >
72  Size lg = this->domainSize();
73 
74  if (lg > std::numeric_limits< Idx >::max() / v.domainSize()) {
75  GUM_ERROR(OutOfBounds, "Out of bounds !");
76  }
77 
79  _gaps.insert(&v, lg);
80  }
81 
82  // removes a dimension, needed for updating the _offsets & _gaps
83 
84  template < typename GUM_SCALAR >
87  Idx pos = variables.pos(&v); // throw a NotFound if necessary
88 
89  if (variables.size() == 1) {
90  _gaps.clear();
91  } else {
92  // update the _gaps
93  Size v_size = v.domainSize();
94  _gaps.erase(variables[pos]);
95 
96  for (Idx i = pos + 1; i < variables.size(); ++i) {
97  _gaps[variables[i]] /= v_size;
98  }
99  }
100 
102  }
103 
104  // listen to change in each recorded Instantiation.
105 
106  template < typename GUM_SCALAR >
108  const Instantiation& i,
109  const DiscreteVariable* const var,
110  Idx oldval,
111  Idx newval) {
112  GUM_ASSERT(_offsets.exists(&i));
113  GUM_ASSERT(_offsets[&i] < this->domainSize());
114  GUM_ASSERT(newval < var->domainSize());
115  GUM_ASSERT(oldval < var->domainSize());
116 
117  if (newval >= oldval) {
118  _offsets[&i] += _gaps[var] * (newval - oldval);
119  GUM_ASSERT(_offsets[&i] < this->domainSize());
120  } else {
121  GUM_ASSERT(_offsets[&i] >= _gaps[var] * (oldval - newval));
122  _offsets[&i] -= _gaps[var] * (oldval - newval);
123  }
124  }
125 
126  // listen to an assignment of a value in a Instantiation
127 
128  template < typename GUM_SCALAR >
130  const Instantiation& i) {
131  GUM_ASSERT(_offsets.exists(&i));
132  _offsets[&i] = _getOffs(i);
133  }
134 
135  // listen to setFirst in each recorded Instantiation.
136 
137  template < typename GUM_SCALAR >
139  const Instantiation& i) {
140  GUM_ASSERT(_offsets.exists(&i));
141  _offsets[&i] = 0;
142  }
143 
144  // listen to setLast in each recorded Instantiation.
145 
146  template < typename GUM_SCALAR >
148  const Instantiation& i) {
149  GUM_ASSERT(_offsets.exists(&i));
150  _offsets[&i] = this->domainSize() - 1;
151  }
152 
153  // listen to increment in each recorded Instantiation.
154 
155  template < typename GUM_SCALAR >
156  INLINE void
158  GUM_ASSERT(_offsets.exists(&i));
159  GUM_ASSERT(_offsets[&i] != this->domainSize() - 1);
160  ++_offsets[&i];
161  }
162 
163  // listen to increment in each recorded Instantiation.
164 
165  template < typename GUM_SCALAR >
166  INLINE void
168  GUM_ASSERT(_offsets.exists(&i));
169  GUM_ASSERT(_offsets[&i] != 0);
170  --_offsets[&i];
171  }
172 
173  // add a Instantiation as a slave
174 
175  template < typename GUM_SCALAR >
178  GUM_ASSERT(!_offsets.exists(&i));
179  _offsets.insert(&i, _getOffs(i));
180  return true;
181  }
182 
183  return false;
184  }
185 
186  // remove a registered slave instantiation
187 
188  template < typename GUM_SCALAR >
191  _offsets.erase(&i);
192  return true;
193  }
194 
195  // Compute the offset of a Instantiation
203  template < typename GUM_SCALAR >
204  INLINE Size
206  Idx off = 0;
207 
209  _gaps.beginSafe();
210  iter != _gaps.endSafe();
211  ++iter)
212  if (i.contains(iter.key()))
213  off += iter.val() * i.valFromPtr(iter.key());
214  else
216  iter.key()->name() << " not present in the instantiation " << i);
217 
218  return off;
219  }
220 
221  // For a given indice of a value in the vector _values, this method computes
222  // the corresponding instantiation
232  template < typename GUM_SCALAR >
234  Instantiation& result, Size indice) const {
235  for (Idx i = 0; i < this->nbrDim(); ++i) {
236  const DiscreteVariable& var = this->variable(i);
237  Idx domainSize = var.domainSize();
238  result.chgVal(var, indice % domainSize);
239  indice = indice / domainSize;
240  }
241 
242  GUM_ASSERT(indice == 0);
243  }
244 
245  // string representation of internal data about i in this.
246  template < typename GUM_SCALAR >
247  INLINE const std::string
249  if (i->isMaster(this)) {
250  std::stringstream s;
251  s << _offsets[i];
252  std::string res;
253  s >> res;
254  return res;
255  } else {
256  return "--";
257  }
258  }
259 
260  template < typename GUM_SCALAR >
261  INLINE Size
263  return _getOffs(i);
264  }
265 
266  // set the Instantiation to the values corresponding to the offset (in this
267  // array)
268  template < typename GUM_SCALAR >
269  INLINE Instantiation&
271  Size offset) const {
272  this->_computeInstantiationValue(i, offset);
273  return i;
274  }
275 
276 } /* 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:518
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:38
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:1022
Safe Const Iterators for hashtables.
Definition: hashTable.h:1918
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:83
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:53
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:48
virtual bool registerSlave(Instantiation &i)
Register i as a slave of this MultiDimAdressable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55