aGrUM  0.16.0
multiDimSparse_tpl.h
Go to the documentation of this file.
1 
30 namespace gum {
31 
32  // Default constructor: creates an empty null dimensional matrix
33 
34  template < typename GUM_SCALAR >
35  MultiDimSparse< GUM_SCALAR >::MultiDimSparse(const GUM_SCALAR& default_value) :
36  MultiDimWithOffset< GUM_SCALAR >(), _default(default_value) {
37  // for debugging purposes
38  GUM_CONSTRUCTOR(MultiDimSparse);
39  }
40 
41  // copy constructor
42 
43  template < typename GUM_SCALAR >
45  const MultiDimSparse< GUM_SCALAR >& from) :
46  MultiDimWithOffset< GUM_SCALAR >(from),
47  _params(from._params), _default(from._default) {
48  // for debugging purposes
49  GUM_CONS_CPY(MultiDimSparse);
50  }
51 
52  // destructor
53 
54  template < typename GUM_SCALAR >
56  // for debugging purposes
57  GUM_DESTRUCTOR(MultiDimSparse);
58  // no need to unregister all slaves as it will be done by MultiDimWithOffset
59  }
60 
61  // data access operator
62 
63  template < typename GUM_SCALAR >
64  INLINE GUM_SCALAR
66  Size key;
67 
68  if (i.isMaster(this)) {
69  key = this->_offsets[&i];
70  } else {
71  key = this->_getOffs(i);
72  }
73 
74  return _params.exists(key) ? _params[key] : _default;
75  }
76 
77  template < typename GUM_SCALAR >
79  const GUM_SCALAR& value) const {
80  Size key;
81 
82  if (i.isMaster(this)) {
83  key = this->_offsets[&i];
84  } else {
85  key = this->_getOffs(i);
86  }
87 
88  if (value == _default) {
89  _params.reset(key);
90  } else {
91  _params.set(key, value);
92  }
93  }
94 
95  // add a new dimension, needed for updating the _offsets & _gaps
96 
97  template < typename GUM_SCALAR >
100 
101  //@todo : anything better than that !
102  fill(_default);
103  }
104 
105  // removes a dimension, needed for updating the _offsets & _gaps
106 
107  template < typename GUM_SCALAR >
110 
111  //@todo : anything better than that !
112  fill(_default);
113  }
114 
115  // synchronise content after MultipleChanges
116  template < typename GUM_SCALAR >
118  //@todo : anything better than that !
119  fill(_default);
120  }
121 
122  // fill the array with the arg
123  template < typename GUM_SCALAR >
124  INLINE void MultiDimSparse< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
125  _params.clear();
126  _default = d;
127  }
128 
129  template < typename GUM_SCALAR >
131  return _params.size();
132  }
133 
134  template < typename GUM_SCALAR >
138  }
139 
140  // returns the name of the implementation
141  template < typename GUM_SCALAR >
142  INLINE const std::string& MultiDimSparse< GUM_SCALAR >::name() const {
143  static const std::string str = "MultiDimSparse";
144  return str;
145  }
146 
147  template < typename GUM_SCALAR >
149  const DiscreteVariable* y) {
151  }
152 
153  template < typename GUM_SCALAR >
154  INLINE GUM_SCALAR&
157  "Do not use this with the MultiDimSparse class.");
158  }
159 
160 } /* namespace gum */
GUM_SCALAR _default
The default value.
void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
virtual ~MultiDimSparse()
Copy operator.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
HashTable< const Instantiation *, Size > _offsets
The position in the array of each slave Instantiation.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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 void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Changes the value pointed by i.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Class constructor.
Abstract class for Multidimensional matrix stored as an array in memory and with an offset associated...
void reset(const Key &key)
Removes a property (i.e., remove an element).
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
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 _commitMultipleChanges()
Synchronise content after MultipleChanges.
void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual const std::string & name() const
Returns the real name of the multiDim implementation.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
MultiDimSparse(const GUM_SCALAR &default_value)
Default constructor: creates an empty null dimensional matrix.
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
void clear()
Removes all the elements in the hash table.
virtual GUM_SCALAR & _get(const Instantiation &i) const
HashTable< Size, GUM_SCALAR > _params
The true data : the values is mutable since we can change the value in a const multiDimArray.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y)
Replace variable x by y.
virtual void fill(const GUM_SCALAR &d) const
Fill the table with d.
virtual Size realSize() const
Returns the real number of parameters used for this table.
Multidimensional matrix stored as a sparse array in memory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55