aGrUM  0.16.0
multiDimArray_tpl.h
Go to the documentation of this file.
1 
32 
33 namespace gum {
34 
35  // Default constructor: creates an empty null dimensional matrix
36  template < typename GUM_SCALAR >
38  MultiDimWithOffset< GUM_SCALAR >() {
39  // for debugging purposes
40  GUM_CONSTRUCTOR(MultiDimArray);
41  }
42 
43  // copy constructor
44  template < typename GUM_SCALAR >
46  const MultiDimArray< GUM_SCALAR >& src) :
47  MultiDimWithOffset< GUM_SCALAR >(src),
48  _values(src._values) {
49  // for debugging purposes
50  GUM_CONS_CPY(MultiDimArray);
51  }
52 
53  // destructor
54  template < typename GUM_SCALAR >
56  // for debugging purposes
57  GUM_DESTRUCTOR(MultiDimArray);
58  // no need to unregister all slaves as it will be done by MultiDimWithOffset
59  }
60 
61  template < typename GUM_SCALAR >
63  const MultiDimContainer< GUM_SCALAR >& src) const {
64  auto mda = dynamic_cast< const MultiDimArray< GUM_SCALAR >* >(&src);
65 
66  if (mda == nullptr) {
68  } else {
69  _values = mda->_values;
70  }
71  }
72 
73  template < typename GUM_SCALAR >
75  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
76  std::transform(_values.begin(), _values.end(), _values.begin(), f);
77  }
78 
79  template < typename GUM_SCALAR >
81  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
82  GUM_SCALAR base) const {
83  return std::accumulate(_values.begin(), _values.end(), base, f);
84  }
85 
86 
87  // data access operator
88  template < typename GUM_SCALAR >
89  INLINE GUM_SCALAR&
91  if (i.isMaster(this)) {
92  return _values[this->_offsets[&i]];
93  } else {
94  return _values[this->_getOffs(i)];
95  }
96  }
97 
98  // add a new dimension, needed for updating the _offsets & _gaps
99  template < typename GUM_SCALAR >
103 
104  if (!this->_isInMultipleChangeMethod()) {
105  _values.resize(lg * v.domainSize());
106  }
107  }
108 
109  // removes a dimension, needed for updating the _offsets & _gaps
110  template < typename GUM_SCALAR >
113  Idx pos = variables.pos(&v); // throw a NotFound if necessary
114 
115  if (variables.size() == 1) {
116  if (!this->_isInMultipleChangeMethod()) _values.clear();
117  } else {
118  Size v_size = v.domainSize();
119  Size size = this->domainSize();
120  // here, the variable does belong to the array.
121  // => if pos = variables.size() - 1 then we just have to extract the
122  // beginning of the array (actually the first gap of variable v)
123  // if pos = 0, then copy every element whose index is a multiple of |v|
124  // Assume now that pos != 0 and pos != variables.size() - 1, then
125  // let w be the next variable in the set of variables of the array.
126  // Then we must copy |gap(v)| elements every |gap(w)| elements
127 
128  if (!this->_isInMultipleChangeMethod()) {
129  if (pos != variables.size() - 1) {
130  Size gap_v = this->_gaps[variables[pos]];
131  Size gap_w = this->_gaps[variables[pos + 1]];
132 
133  for (Idx i = 0, j = 0; i < size; i += gap_w) {
134  Idx last = i + gap_v;
135 
136  for (Idx k = i; k < last; ++k, ++j)
137  _values[j] = _values[k];
138  }
139  }
140 
141  // shrink _values
142  _values.resize(size / v_size);
143  }
144  }
145 
147  }
148 
149  template < typename GUM_SCALAR >
151  return this->domainSize();
152  }
153 
154  // synchronise content after MultipleChanges
155  template < typename GUM_SCALAR >
159  }
160  }
161 
162  // synchronise content after MultipleChanges
163  template < typename GUM_SCALAR >
164  INLINE void
168  }
169  }
170 
171  // fill the array with the arg
172  template < typename GUM_SCALAR >
173  INLINE void MultiDimArray< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
174  if (!this->empty()) std::fill(_values.begin(), _values.end(), d);
175  }
176 
177  // virtual constructor
178  template < typename GUM_SCALAR >
181  return new MultiDimArray< GUM_SCALAR >;
182  }
183 
184  // returns the element stored in the multidimArray at a given offset
185  template < typename GUM_SCALAR >
186  INLINE const GUM_SCALAR&
188  return _values[offset];
189  }
190 
191  template < typename GUM_SCALAR >
193  const GUM_SCALAR& data) {
194  _values[offset] = data;
195  }
196 
197  // returns the element stored in the multidimArray at a given offset
198  template < typename GUM_SCALAR >
199  INLINE const GUM_SCALAR&
201  if (offset >= _values.size()) { GUM_ERROR(OutOfBounds, "offset too large"); }
202 
203  return _values[offset];
204  }
205 
206  template < typename GUM_SCALAR >
208  const GUM_SCALAR& data) {
209  if (offset >= _values.size()) { GUM_ERROR(OutOfBounds, "offset too large"); }
210 
211  _values[offset] = data;
212  }
213 
214  // returns the name of the implementation
215  template < typename GUM_SCALAR >
216  INLINE const std::string& MultiDimArray< GUM_SCALAR >::name() const {
217  static const std::string str = "MultiDimArray";
218  return str;
219  }
220 
221  template < typename GUM_SCALAR >
223  const DiscreteVariable* y) {
225  }
226 } /* namespace gum */
std::vector< GUM_SCALAR > _values
The true data : the values is mutable since we can change the value / in a const multiDimArray.
virtual Size realSize() const
Returns the real size of this MultiDimArray.
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:518
virtual void erase(const DiscreteVariable &v)
Removes a variable.
HashTable< const Instantiation *, Size > _offsets
The position in the array of each slave Instantiation.
virtual GUM_SCALAR & _get(const Instantiation &i) const
Return a data, given a Instantiation.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Copy from a other MultiDimContainer.
HashTable< const DiscreteVariable *, Size > _gaps
The gaps between consecutive values of a given variable.
Base class for discrete random variable.
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.
virtual bool empty() const override
Returns true if no var is in *this.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const
Apply a function on every element of the container.
void unsafeSet(Idx offset, const GUM_SCALAR &val)
Modifies the element stored in the multidimArray at a given offset.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual ~MultiDimArray()
Copy operator.
virtual void _commitMultipleChanges()
Synchronize content after MultipleChanges.
bool _isInMultipleChangeMethod() const
Get the actual change method of this MultiDimImplementation.
void setByOffset(Idx offset, const GUM_SCALAR &val)
Modifies the element stored in the multidimArray at a given offset.
virtual Size domainSize() const =0
Abstract class for Multidimensional matrix stored as an array in memory and with an offset associated...
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
compute lfold for this container
virtual void fill(const GUM_SCALAR &d) const
Fills the MultiDimArray with the given value.
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:54
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Default constructor.
Size _getOffs(const Instantiation &i) const
Compute the offset of a Instantiation.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y)
Replace variable x by y.
virtual const std::string & name() const
Returns the MultiDimArray name.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
const GUM_SCALAR & getByOffset(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
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 isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
MultiDimArray()
Default constructor.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const GUM_SCALAR & unsafeGet(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual void add(const DiscreteVariable &v)
Adds a variable.