aGrUM  0.14.2
multiDimArray_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  ***************************************************************************/
29 
30 namespace gum {
31 
32  // Default constructor: creates an empty null dimensional matrix
33  template < typename GUM_SCALAR >
35  MultiDimWithOffset< GUM_SCALAR >() {
36  // for debugging purposes
37  GUM_CONSTRUCTOR(MultiDimArray);
38  }
39 
40  // copy constructor
41  template < typename GUM_SCALAR >
43  const MultiDimArray< GUM_SCALAR >& src) :
44  MultiDimWithOffset< GUM_SCALAR >(src),
45  _values(src._values) {
46  // for debugging purposes
47  GUM_CONS_CPY(MultiDimArray);
48  }
49 
50  // destructor
51  template < typename GUM_SCALAR >
53  // for debugging purposes
54  GUM_DESTRUCTOR(MultiDimArray);
55  // no need to unregister all slaves as it will be done by MultiDimWithOffset
56  }
57 
58  template < typename GUM_SCALAR >
60  const MultiDimContainer< GUM_SCALAR >& src) const {
61  auto mda = dynamic_cast< const MultiDimArray< GUM_SCALAR >* >(&src);
62 
63  if (mda == nullptr) {
65  } else {
66  _values = mda->_values;
67  }
68  }
69 
70  template < typename GUM_SCALAR >
72  std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
73  std::transform(_values.begin(), _values.end(), _values.begin(), f);
74  }
75 
76  template < typename GUM_SCALAR >
78  std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
79  GUM_SCALAR base) const {
80  return std::accumulate(_values.begin(), _values.end(), base, f);
81  }
82 
83 
84  // data access operator
85  template < typename GUM_SCALAR >
86  INLINE GUM_SCALAR&
88  if (i.isMaster(this)) {
89  return _values[this->_offsets[&i]];
90  } else {
91  return _values[this->_getOffs(i)];
92  }
93  }
94 
95  // add a new dimension, needed for updating the _offsets & _gaps
96  template < typename GUM_SCALAR >
100 
101  if (!this->_isInMultipleChangeMethod()) {
102  _values.resize(lg * v.domainSize());
103  }
104  }
105 
106  // removes a dimension, needed for updating the _offsets & _gaps
107  template < typename GUM_SCALAR >
110  Idx pos = variables.pos(&v); // throw a NotFound if necessary
111 
112  if (variables.size() == 1) {
113  if (!this->_isInMultipleChangeMethod()) _values.clear();
114  } else {
115  Size v_size = v.domainSize();
116  Size size = this->domainSize();
117  // here, the variable does belong to the array.
118  // => if pos = variables.size() - 1 then we just have to extract the
119  // beginning of the array (actually the first gap of variable v)
120  // if pos = 0, then copy every element whose index is a multiple of |v|
121  // Assume now that pos != 0 and pos != variables.size() - 1, then
122  // let w be the next variable in the set of variables of the array.
123  // Then we must copy |gap(v)| elements every |gap(w)| elements
124 
125  if (!this->_isInMultipleChangeMethod()) {
126  if (pos != variables.size() - 1) {
127  Size gap_v = this->_gaps[variables[pos]];
128  Size gap_w = this->_gaps[variables[pos + 1]];
129 
130  for (Idx i = 0, j = 0; i < size; i += gap_w) {
131  Idx last = i + gap_v;
132 
133  for (Idx k = i; k < last; ++k, ++j)
134  _values[j] = _values[k];
135  }
136  }
137 
138  // shrink _values
139  _values.resize(size / v_size);
140  }
141  }
142 
144  }
145 
146  template < typename GUM_SCALAR >
148  return this->domainSize();
149  }
150 
151  // synchronise content after MultipleChanges
152  template < typename GUM_SCALAR >
156  }
157  }
158 
159  // synchronise content after MultipleChanges
160  template < typename GUM_SCALAR >
161  INLINE void
165  }
166  }
167 
168  // fill the array with the arg
169  template < typename GUM_SCALAR >
170  INLINE void MultiDimArray< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
171  if (!this->empty()) std::fill(_values.begin(), _values.end(), d);
172  }
173 
174  // virtual constructor
175  template < typename GUM_SCALAR >
178  return new MultiDimArray< GUM_SCALAR >;
179  }
180 
181  // returns the element stored in the multidimArray at a given offset
182  template < typename GUM_SCALAR >
183  INLINE const GUM_SCALAR&
185  return _values[offset];
186  }
187 
188  template < typename GUM_SCALAR >
190  const GUM_SCALAR& data) {
191  _values[offset] = data;
192  }
193 
194  // returns the element stored in the multidimArray at a given offset
195  template < typename GUM_SCALAR >
196  INLINE const GUM_SCALAR&
198  if (offset >= _values.size()) { GUM_ERROR(OutOfBounds, "offset too large"); }
199 
200  return _values[offset];
201  }
202 
203  template < typename GUM_SCALAR >
205  const GUM_SCALAR& data) {
206  if (offset >= _values.size()) { GUM_ERROR(OutOfBounds, "offset too large"); }
207 
208  _values[offset] = data;
209  }
210 
211  // returns the name of the implementation
212  template < typename GUM_SCALAR >
213  INLINE const std::string& MultiDimArray< GUM_SCALAR >::name() const {
214  static const std::string str = "MultiDimArray";
215  return str;
216  }
217 
218  template < typename GUM_SCALAR >
220  const DiscreteVariable* y) {
222  }
223 } /* 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:515
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:35
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
Headers of the MultiDimWithOffset class.
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.
gum is the global namespace for all aGrUM entities
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:51
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:80
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: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
Header of the MultiDimArray class.
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:52
virtual void add(const DiscreteVariable &v)
Adds a variable.