aGrUM  0.14.2
multiDimSparse_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 namespace gum {
28 
29  // Default constructor: creates an empty null dimensional matrix
30 
31  template < typename GUM_SCALAR >
32  MultiDimSparse< GUM_SCALAR >::MultiDimSparse(const GUM_SCALAR& default_value) :
33  MultiDimWithOffset< GUM_SCALAR >(), _default(default_value) {
34  // for debugging purposes
35  GUM_CONSTRUCTOR(MultiDimSparse);
36  }
37 
38  // copy constructor
39 
40  template < typename GUM_SCALAR >
42  const MultiDimSparse< GUM_SCALAR >& from) :
43  MultiDimWithOffset< GUM_SCALAR >(from),
44  _params(from._params), _default(from._default) {
45  // for debugging purposes
46  GUM_CONS_CPY(MultiDimSparse);
47  }
48 
49  // destructor
50 
51  template < typename GUM_SCALAR >
53  // for debugging purposes
54  GUM_DESTRUCTOR(MultiDimSparse);
55  // no need to unregister all slaves as it will be done by MultiDimWithOffset
56  }
57 
58  // data access operator
59 
60  template < typename GUM_SCALAR >
61  INLINE GUM_SCALAR
63  Size key;
64 
65  if (i.isMaster(this)) {
66  key = this->_offsets[&i];
67  } else {
68  key = this->_getOffs(i);
69  }
70 
71  return _params.exists(key) ? _params[key] : _default;
72  }
73 
74  template < typename GUM_SCALAR >
76  const GUM_SCALAR& value) const {
77  Size key;
78 
79  if (i.isMaster(this)) {
80  key = this->_offsets[&i];
81  } else {
82  key = this->_getOffs(i);
83  }
84 
85  if (value == _default) {
86  _params.reset(key);
87  } else {
88  _params.set(key, value);
89  }
90  }
91 
92  // add a new dimension, needed for updating the _offsets & _gaps
93 
94  template < typename GUM_SCALAR >
97 
98  //@todo : anything better than that !
99  fill(_default);
100  }
101 
102  // removes a dimension, needed for updating the _offsets & _gaps
103 
104  template < typename GUM_SCALAR >
107 
108  //@todo : anything better than that !
109  fill(_default);
110  }
111 
112  // synchronise content after MultipleChanges
113  template < typename GUM_SCALAR >
115  //@todo : anything better than that !
116  fill(_default);
117  }
118 
119  // fill the array with the arg
120  template < typename GUM_SCALAR >
121  INLINE void MultiDimSparse< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
122  _params.clear();
123  _default = d;
124  }
125 
126  template < typename GUM_SCALAR >
128  return _params.size();
129  }
130 
131  template < typename GUM_SCALAR >
135  }
136 
137  // returns the name of the implementation
138  template < typename GUM_SCALAR >
139  INLINE const std::string& MultiDimSparse< GUM_SCALAR >::name() const {
140  static const std::string str = "MultiDimSparse";
141  return str;
142  }
143 
144  template < typename GUM_SCALAR >
146  const DiscreteVariable* y) {
148  }
149 
150  template < typename GUM_SCALAR >
151  INLINE GUM_SCALAR&
154  "Do not use this with the MultiDimSparse class.");
155  }
156 
157 } /* 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.
gum is the global namespace for all aGrUM entities
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:80
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:45
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:52