aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::MultiDimSparse< GUM_SCALAR > Class Template Reference

Multidimensional matrix stored as a sparse array in memory. More...

#include <agrum/tools/multidim/multiDimSparse.h>

+ Inheritance diagram for gum::MultiDimSparse< GUM_SCALAR >:
+ Collaboration diagram for gum::MultiDimSparse< GUM_SCALAR >:

Public Member Functions

Constructors / Destructors
 MultiDimSparse (const GUM_SCALAR &default_value)
 Default constructor: creates an empty null dimensional matrix. More...
 
 MultiDimSparse (const MultiDimSparse< GUM_SCALAR > &from)
 Copy constructor. More...
 
virtual ~MultiDimSparse ()
 Copy operator. More...
 
Accessors and modifiers
virtual std::string toString (const Instantiation *i) const
 Display the internal representation of i. More...
 
virtual void changeNotification (const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)
 Listen to changes in a given Instantiation. More...
 
virtual void setChangeNotification (const Instantiation &i)
 Listen to an assignment of a value in a Instantiation. More...
 
virtual void setFirstNotification (const Instantiation &i)
 Listen to setFirst in a given Instantiation. More...
 
virtual void setLastNotification (const Instantiation &i)
 Listen to setLast in a given Instantiation. More...
 
void setIncNotification (const Instantiation &i)
 Listen to increment in a given Instantiation. More...
 
void setDecNotification (const Instantiation &i)
 Listen to increment in each recorded Instantiation. More...
 
virtual bool registerSlave (Instantiation &i)
 Register i as a slave of this MultiDimAdressable. More...
 
virtual bool unregisterSlave (Instantiation &i)
 Unregister i as a slave of this MultiDimAdressable. More...
 
Various methods.
virtual std::string toString () const
 Returns a representation of this MultiDimContainer. More...
 
bool operator== (const MultiDimContainer< GUM_SCALAR > &p) const
 Test if this MultiDimContainer is equal to p. More...
 
bool operator!= (const MultiDimContainer< GUM_SCALAR > &p) const
 Test if this MultiDimContainer is different of p. More...
 
virtual void apply (std::function< GUM_SCALAR(GUM_SCALAR) > f) const
 Apply a function on every element of the container. More...
 
virtual GUM_SCALAR reduce (std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const
 compute lfold for this container More...
 
Access to offset in MultiDimWithOffset
Size toOffset (const Instantiation &i) const
 Compute offset from an Instantiation (in this array). More...
 
InstantiationfromOffset (Instantiation &i, Size offset) const
 Set the Instantiation to the values corresponding to the offset (in this array). More...
 
Accessors / Modifiers
const std::string & basename () const
 Returns the base class name of this MultiDimImplementation. More...
 
float compressionRate () const
 The compression ratio of the table (depending on the type of implementation). More...
 
MultiDimInterface implementation
virtual Idx nbrDim () const override
 Returns the number of vars in the multidimensional container. More...
 
virtual Size domainSize () const override
 Returns the product of the variables domain size. More...
 
virtual const Sequence< const DiscreteVariable *> & variablesSequence () const override
 Returns a const ref to the sequence of DiscreteVariable*. More...
 
const DiscreteVariablevariable (Idx i) const override
 Returns a const ref to the ith var. More...
 
const DiscreteVariablevariable (const std::string &name) const override
 Returns the variable with the name. More...
 
virtual Idx pos (const DiscreteVariable &v) const override
 Returns the index of a variable. More...
 
virtual bool contains (const DiscreteVariable &v) const override
 Returns true if var is in *this. More...
 
virtual bool empty () const override
 Returns true if no var is in *this. More...
 
MultiDimContainer implementation
void beginMultipleChanges () override
 Call this method before doing important changes in this MultiDimContainer. More...
 
void endMultipleChanges () override
 Call this method after doing important changes in this MultiDimContainer. More...
 
void endMultipleChanges (const GUM_SCALAR &) override
 Call this method after doing important changes in this MultiDimContainer. More...
 
Accessors / Modifiers
GUM_SCALAR operator[] (const Instantiation &i) const
 An [] operator using a Instantiation as argument. More...
 
virtual void populate (const std::vector< GUM_SCALAR > &v) const
 Automatically fills this MultiDimContainer with the values in v. More...
 
virtual void populate (std::initializer_list< GUM_SCALAR > l) const
 Automatically fills this MultiDimContainer with the values in l. More...
 
Copy methods.
virtual void copyFrom (const MultiDimContainer< GUM_SCALAR > &src) const
 Basic copy of a MultiDimContainer. More...
 
virtual void copyFrom (const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i) const
 Basic copy of a MultiDimContainer. More...
 
virtual void extractFrom (const MultiDimContainer< GUM_SCALAR > &src, const Instantiation &mask)
 Basic extraction of a MultiDimContainer. More...
 
virtual MultiDimAdressablegetMasterRef ()
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer. More...
 
virtual const MultiDimAdressablegetMasterRef () const
 In order to insure the dereference for decorators, we need to virtualize the access to master pointer. More...
 
virtual void copy (const MultiDimContainer< GUM_SCALAR > &src)
 Removes all variables in this MultiDimContainer and copy the content of src, variables included. More...
 
Accessors / Modifiers
void replace (const DiscreteVariable &x, const DiscreteVariable &y)
 Replace variables in this multidim. More...
 

Protected Attributes

HashTable< Size, GUM_SCALAR > params_
 The true data : the values is mutable since we can change the value in a const multiDimArray. More...
 
GUM_SCALAR default_
 The default value. More...
 
HashTable< const DiscreteVariable *, Sizegaps_
 The gaps between consecutive values of a given variable. More...
 
HashTable< const Instantiation *, Sizeoffsets_
 The position in the array of each slave Instantiation. More...
 

Protected Member Functions

virtual void commitMultipleChanges_ ()
 Synchronise content after MultipleChanges. More...
 
virtual GUM_SCALAR & get_ (const Instantiation &i) const
 
Size getOffs_ (const Instantiation &i) const
 Compute the offset of a Instantiation. More...
 
void computeInstantiationValue_ (Instantiation &result, Size indice) const
 For a given index of a value in the vector values, this method computes the corresponding instantiation. More...
 
virtual const MultiDimImplementation< GUM_SCALAR > * content () const final
 Returns the implementation for this object (may be *this). More...
 
virtual MultiDimImplementation< GUM_SCALAR > * content () final
 Returns the implementation for this object (may be *this). More...
 
Fast large modifications in structures
virtual void commitMultipleChanges_ (const GUM_SCALAR &value)
 Synchronize content after MultipleChanges. More...
 
bool isInMultipleChangeMethod_ () const
 Get the actual change method of this MultiDimImplementation. More...
 
bool isCommitNeeded_ () const
 Get the actual state of *this. More...
 
const List< Instantiation *> & slaves_ () const
 Returns a constant reference over the list of slaved instantiations. More...
 
virtual void invert_ (Idx p1, Idx p2)
 Inverts variables at position p1 and p2. More...
 

Inherited methods

virtual MultiDimContainer< GUM_SCALAR > * newFactory () const
 Class constructor. More...
 
void add (const DiscreteVariable &v)
 Adds a new var to the variables of the multidimensional matrix. More...
 
void erase (const DiscreteVariable &v)
 Removes a var from the variables of the multidimensional matrix. More...
 
virtual void fill (const GUM_SCALAR &d) const
 Fill the table with d. More...
 
virtual GUM_SCALAR get (const Instantiation &i) const
 Returns the value pointed by i. More...
 
virtual void set (const Instantiation &i, const GUM_SCALAR &value) const
 Changes the value pointed by i. More...
 
virtual const std::string & name () const
 Returns the real name of the multiDim implementation. More...
 
virtual Size realSize () const
 Returns the real number of parameters used for this table. More...
 
virtual void replace_ (const DiscreteVariable *x, const DiscreteVariable *y)
 Replace variable x by y. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::MultiDimSparse< GUM_SCALAR >

Multidimensional matrix stored as a sparse array in memory.

Template Parameters
GUM_SCALARThe type of scalars stored in the multidimensional table.

Definition at line 50 of file multiDimSparse.h.

Constructor & Destructor Documentation

◆ MultiDimSparse() [1/2]

template<typename GUM_SCALAR >
gum::MultiDimSparse< GUM_SCALAR >::MultiDimSparse ( const GUM_SCALAR &  default_value)
explicit

Default constructor: creates an empty null dimensional matrix.

Parameters
default_valueThe default value of this MultiDimSparse.

Definition at line 34 of file multiDimSparse_tpl.h.

34  :
35  MultiDimWithOffset< GUM_SCALAR >(), default_(default_value) {
36  // for debugging purposes
37  GUM_CONSTRUCTOR(MultiDimSparse);
38  }
GUM_SCALAR default_
The default value.
MultiDimSparse(const GUM_SCALAR &default_value)
Default constructor: creates an empty null dimensional matrix.

◆ MultiDimSparse() [2/2]

template<typename GUM_SCALAR >
gum::MultiDimSparse< GUM_SCALAR >::MultiDimSparse ( const MultiDimSparse< GUM_SCALAR > &  from)

Copy constructor.

The newly created matrix contains the same variables and the same values as from, but no instantiation is associated to it.

Parameters
fromThe MultiDimSparse to copy.

Definition at line 43 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

43  :
44  MultiDimWithOffset< GUM_SCALAR >(from), params_(from.params_), default_(from.default_) {
45  // for debugging purposes
46  GUM_CONS_CPY(MultiDimSparse);
47  }
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
GUM_SCALAR default_
The default value.
MultiDimSparse(const GUM_SCALAR &default_value)
Default constructor: creates an empty null dimensional matrix.
+ Here is the call graph for this function:

◆ ~MultiDimSparse()

template<typename GUM_SCALAR >
gum::MultiDimSparse< GUM_SCALAR >::~MultiDimSparse ( )
virtual

Copy operator.

Todo:
operator=
Parameters
fromThe MultiDimSparse to copy. MultiDimSparse<GUM_SCALAR>& operator=( const MultiDimSparse<GUM_SCALAR>& from ); Class destructor.

Definition at line 52 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

52  {
53  // for debugging purposes
54  GUM_DESTRUCTOR(MultiDimSparse);
55  // no need to unregister all slaves as it will be done by MultiDimWithOffset
56  }
MultiDimSparse(const GUM_SCALAR &default_value)
Default constructor: creates an empty null dimensional matrix.
+ Here is the call graph for this function:

Member Function Documentation

◆ add()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::add ( const DiscreteVariable v)
virtual

Adds a new var to the variables of the multidimensional matrix.

See also
operator<<(MultiDimInterface& c, const DiscreteVariable& v)
Warning
Note that the variable passed in argument is not duplicated, that is, only a pointer toward the variable is kept by the MultiDimInterface.
Parameters
vThe new var.
Exceptions
DuplicateElementRaised if the variable already belongs to the sequence of variables.
OperationNotAllowedRaised if this object is non mutable.

Reimplemented from gum::MultiDimWithOffset< GUM_SCALAR >.

Definition at line 94 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

94  {
96  fill(default_);
97  }
GUM_SCALAR default_
The default value.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual void fill(const GUM_SCALAR &d) const
Fill the table with d.
+ Here is the call graph for this function:

◆ apply()

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::apply ( std::function< GUM_SCALAR(GUM_SCALAR) >  f) const
virtualinherited

Apply a function on every element of the container.

Parameters
fthe function to apply

Reimplemented in gum::MultiDimDecorator< GUM_SCALAR >, and gum::MultiDimArray< GUM_SCALAR >.

Definition at line 188 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

188  {
189  Instantiation i(*this);
190  for (i.setFirst(); !i.end(); ++i) {
191  set(i, f(get(i)));
192  }
193  }
+ Here is the call graph for this function:

◆ basename()

template<typename GUM_SCALAR >
const std::string & gum::MultiDimImplementation< GUM_SCALAR >::basename ( ) const
inherited

Returns the base class name of this MultiDimImplementation.

This method is used for chosing a proposer operator when no specialized operator have been defined.

Definition at line 273 of file multiDimImplementation_tpl.h.

273  {
274  static const std::string str = "MultiDimImplementation";
275  return str;
276  }

◆ beginMultipleChanges()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimImplementation< GUM_SCALAR >::beginMultipleChanges ( )
overridevirtualinherited

Call this method before doing important changes in this MultiDimContainer.

Warning
Remember to call endMultipleChanges() when you finish your changes.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 207 of file multiDimImplementation_tpl.h.

◆ changeNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::changeNotification ( const Instantiation i,
const DiscreteVariable *const  var,
Idx  oldval,
Idx  newval 
)
virtualinherited

Listen to changes in a given Instantiation.

Parameters
iThe Instantiation to listen.
varThe changed dim.
oldvalThe old value.
newvalThe changed value.

Implements gum::MultiDimAdressable.

Definition at line 106 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

109  {
110  GUM_ASSERT(offsets_.exists(&i));
111  GUM_ASSERT(offsets_[&i] < this->domainSize());
112  GUM_ASSERT(newval < var->domainSize());
113  GUM_ASSERT(oldval < var->domainSize());
114 
115  if (newval >= oldval) {
116  offsets_[&i] += gaps_[var] * (newval - oldval);
117  GUM_ASSERT(offsets_[&i] < this->domainSize());
118  } else {
119  GUM_ASSERT(offsets_[&i] >= gaps_[var] * (oldval - newval));
120  offsets_[&i] -= gaps_[var] * (oldval - newval);
121  }
122  }
HashTable< const DiscreteVariable *, Size > gaps_
The gaps between consecutive values of a given variable.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual Size domainSize() const override
Returns the product of the variables domain size.
+ Here is the call graph for this function:

◆ commitMultipleChanges_() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::commitMultipleChanges_ ( )
protectedvirtual

Synchronise content after MultipleChanges.

Reimplemented from gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 109 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

109  {
110  fill(default_);
111  }
GUM_SCALAR default_
The default value.
virtual void fill(const GUM_SCALAR &d) const
Fill the table with d.
+ Here is the call graph for this function:

◆ commitMultipleChanges_() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::MultiDimImplementation< GUM_SCALAR >::commitMultipleChanges_ ( const GUM_SCALAR &  value)
protectedvirtualinherited

Synchronize content after MultipleChanges.

Parameters
valueDefault value for uninitialized values.

Reimplemented in gum::MultiDimArray< GUM_SCALAR >.

Definition at line 237 of file multiDimImplementation_tpl.h.

237  {
238  // empty!
239  }

◆ compressionRate()

template<typename GUM_SCALAR >
INLINE float gum::MultiDimImplementation< GUM_SCALAR >::compressionRate ( ) const
inherited

The compression ratio of the table (depending on the type of implementation).

This method uses domainSize() and realSize() to compute the ration, both methods are virtual and should be surcharge if a subclass has a special policies about memory management.

Warning
This compression ratio is not exactly the memory compression ratio. It is computed in terms of number of parameters.
Returns
Returns the compression ration of the table.

Definition at line 267 of file multiDimImplementation_tpl.h.

267  {
268  return ((float)1) - (float)realSize() / (float)domainSize();
269  }
virtual Size realSize() const =0
Returns the real number of parameters used for this table.
virtual Size domainSize() const override
Returns the product of the variables domain size.

◆ computeInstantiationValue_()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::computeInstantiationValue_ ( Instantiation result,
Size  indice 
) const
protectedinherited

For a given index of a value in the vector values, this method computes the corresponding instantiation.

Parameters
resultthe result of this methods, we assume that the given instantiation already contains all the variables contained in the MultiDimArray (if V is the set of variables of this tab, V must be a subset of variables in result or the exact set)
indiceThe index in the vector values
resultthe result of this methods, we assume that the given instantiation already contains all the variables contained in the multidimarray (if V is the set of variables of this tab, V must be a subset of variables in result or the exact set)
indiceindice in the vector values_

Definition at line 223 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

224  {
225  for (Idx i = 0; i < this->nbrDim(); ++i) {
226  const DiscreteVariable& var = this->variable(i);
227  Idx domainSize = var.domainSize();
228  result.chgVal(var, indice % domainSize);
229  indice = indice / domainSize;
230  }
231 
232  GUM_ASSERT(indice == 0);
233  }
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const override
Returns the product of the variables domain size.
+ Here is the call graph for this function:

◆ contains()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimImplementation< GUM_SCALAR >::contains ( const DiscreteVariable v) const
overridevirtualinherited

Returns true if var is in *this.

Parameters
vA DiscreteVariable.
Returns
Returns true if var is in *this.

Implements gum::MultiDimInterface.

Reimplemented in gum::MultiDimBucket< GUM_SCALAR >.

Definition at line 187 of file multiDimImplementation_tpl.h.

187  {
188  return _vars_.exists(&v);
189  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

◆ content() [1/2]

template<typename GUM_SCALAR >
INLINE const MultiDimImplementation< GUM_SCALAR > * gum::MultiDimImplementation< GUM_SCALAR >::content ( ) const
finalprotectedvirtualinherited

Returns the implementation for this object (may be *this).

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 312 of file multiDimImplementation_tpl.h.

312  {
313  return this;
314  }

◆ content() [2/2]

template<typename GUM_SCALAR >
INLINE MultiDimImplementation< GUM_SCALAR > * gum::MultiDimImplementation< GUM_SCALAR >::content ( )
finalprotectedvirtualinherited

Returns the implementation for this object (may be *this).

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 305 of file multiDimImplementation_tpl.h.

305  {
306  return this;
307  }

◆ copy()

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copy ( const MultiDimContainer< GUM_SCALAR > &  src)
virtualinherited

Removes all variables in this MultiDimContainer and copy the content of src, variables included.

Parameters
srcThe MultiDimContainer to copy.

Reimplemented in gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 272 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

272  {
273  this->beginMultipleChanges();
274 
275  Size nbr = this->nbrDim();
276 
277  for (Idx i = 0; i < nbr; i++) {
278  this->erase(this->variable(0));
279  }
280 
281  for (Idx i = 0; i < src.nbrDim(); i++) {
282  this->add(src.variable(i));
283  }
284 
285  this->endMultipleChanges();
286  this->copyFrom(src);
287  }
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ copyFrom() [1/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copyFrom ( const MultiDimContainer< GUM_SCALAR > &  src) const
virtualinherited

Basic copy of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which values are copied. This is a full copy with no verification of dimensions.
Exceptions
OperationNotAllowedRaised if src does not have the same domain size than this MultiDimContainer.

Reimplemented in gum::aggregator::MultiDimAggregator< GUM_SCALAR >, gum::MultiDimICIModel< GUM_SCALAR >, and gum::MultiDimArray< GUM_SCALAR >.

Definition at line 255 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

255  {
256  if (src.domainSize() != domainSize()) {
257  GUM_ERROR(OperationNotAllowed,
258  "Domain sizes do not fit : " << src.domainSize() << "!=" << domainSize());
259  }
260 
261  Instantiation i_dest(*this);
262  Instantiation i_src(src);
263 
264  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); ++i_dest, ++i_src) {
265  set(i_dest, src[i_src]);
266  }
267  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ copyFrom() [2/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::copyFrom ( const MultiDimContainer< GUM_SCALAR > &  src,
Instantiation p_i 
) const
virtualinherited

Basic copy of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which values are copied.
p_iGive the order to iterate in this MultiDimContainer during the copy (nullptr will correctly copy if this is a reorganization of src).
Exceptions
OperationNotAllowedRaised if src does not have the same domain size than this MultiDimContainer.

Reimplemented in gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 209 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

210  {
211  if (src.domainSize() != domainSize()) {
212  GUM_ERROR(OperationNotAllowed,
213  "Domain sizes do not fit : " << src.domainSize() << "!=" << domainSize());
214  }
215 
216  if (p_i == nullptr) { // if null, we just follow the same order
217  Instantiation i(src);
218  for (i.setFirst(); !i.end(); ++i) {
219  set(i, src[i]);
220  }
221  } else {
222  Instantiation i_dest(*this);
223  Instantiation i_src(src);
224  for (i_dest.setFirst(), i_src.setFirst(); !i_dest.end(); i_dest.incIn(*p_i), ++i_src) {
225  set(i_dest, src[i_src]);
226  }
227  }
228  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ domainSize()

template<typename GUM_SCALAR >
INLINE Size gum::MultiDimImplementation< GUM_SCALAR >::domainSize ( ) const
overridevirtualinherited

Returns the product of the variables domain size.

Returns
Returns the product of the variables domain size.

Implements gum::MultiDimInterface.

Definition at line 162 of file multiDimImplementation_tpl.h.

162  {
163  return _domainSize_;
164  }
Size _domainSize_
This MultiDimImplementation domain size.

◆ empty()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimImplementation< GUM_SCALAR >::empty ( ) const
overridevirtualinherited

Returns true if no var is in *this.

Returns
Returns true if no var is in *this.

Implements gum::MultiDimInterface.

Definition at line 201 of file multiDimImplementation_tpl.h.

201  {
202  GUM_ASSERT(!this->isCommitNeeded_());
203  return _vars_.empty();
204  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).
bool isCommitNeeded_() const
Get the actual state of *this.

◆ endMultipleChanges() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::MultiDimImplementation< GUM_SCALAR >::endMultipleChanges ( )
overridevirtualinherited

◆ endMultipleChanges() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::MultiDimImplementation< GUM_SCALAR >::endMultipleChanges ( const GUM_SCALAR &  v)
overridevirtualinherited

◆ erase()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::erase ( const DiscreteVariable v)
virtual

Removes a var from the variables of the multidimensional matrix.

See also
operator>>(MultiDimInterface& c, const DiscreteVariable& v)
Exceptions
OperationNotAllowedRaised if this object is non mutable.
NotFoundRaised if v does not belong to this.

Reimplemented from gum::MultiDimWithOffset< GUM_SCALAR >.

Definition at line 102 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

102  {
104  fill(default_);
105  }
GUM_SCALAR default_
The default value.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
virtual void fill(const GUM_SCALAR &d) const
Fill the table with d.
+ Here is the call graph for this function:

◆ extractFrom()

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::extractFrom ( const MultiDimContainer< GUM_SCALAR > &  src,
const Instantiation mask 
)
virtualinherited

Basic extraction of a MultiDimContainer.

This method is virtual because it should be optimized in certain MultiDimContainer.

Parameters
srcThe MultiDimContainer src which datas are copied.
maskpartial instantiation of variables of the Potential : the

extraction will concern every variable not in the instantiation and the copy of data will use the (relevant) values in this instantiation.

Definition at line 231 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

232  {
233  this->beginMultipleChanges();
234 
235  Size nbr = this->nbrDim();
236  for (Idx i = 0; i < nbr; i++) {
237  this->erase(this->variable(0));
238  }
239 
240  for (Idx i = 0; i < src.nbrDim(); i++) {
241  if (!imask.contains(src.variable(i))) this->add(src.variable(i));
242  }
243 
244  this->endMultipleChanges();
245 
246  if (this->nbrDim() == 0) { GUM_ERROR(FatalError, "Empty potential") }
247 
248  Instantiation inst(src);
249  inst.setVals(imask);
250  for (inst.setFirstOut(imask); !inst.end(); inst.incOut(imask))
251  set(inst, src[inst]);
252  }
virtual void endMultipleChanges()=0
Call this method after doing important changes in this MultiDimContainer.
virtual void add(const DiscreteVariable &v)=0
Adds a new var to the variables of the multidimensional matrix.
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual const DiscreteVariable & variable(Idx i) const =0
Returns a const ref to the ith var.
virtual void beginMultipleChanges()=0
Call this method before doing important changes in this MultiDimContainer.
virtual void erase(const DiscreteVariable &v)=0
Removes a var from the variables of the multidimensional matrix.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ fill()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::fill ( const GUM_SCALAR &  d) const
virtual

Fill the table with d.

Parameters
dThe value used to fill this MultiDimContainer.

Implements gum::MultiDimWithOffset< GUM_SCALAR >.

Definition at line 115 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

115  {
116  params_.clear();
117  default_ = d;
118  }
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
GUM_SCALAR default_
The default value.
void clear()
Removes all the elements in the hash table.
+ Here is the call graph for this function:

◆ fromOffset()

template<typename GUM_SCALAR >
INLINE Instantiation & gum::MultiDimWithOffset< GUM_SCALAR >::fromOffset ( Instantiation i,
Size  offset 
) const
inherited

Set the Instantiation to the values corresponding to the offset (in this array).

We assume that the given instantiation already contains all the variables contained in the multidimarray (if V is the set of variables of this tab, V must be a subset of variables in result or the exact set).

Parameters
iThe instantiation i modified giving the offset.
offsetThe offset used to compute the value of i.
Returns
Returns a reference over i.

Definition at line 257 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

258  {
259  this->computeInstantiationValue_(i, offset);
260  return i;
261  }
void computeInstantiationValue_(Instantiation &result, Size indice) const
For a given index of a value in the vector values, this method computes the corresponding instantiati...
+ Here is the call graph for this function:

◆ get()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MultiDimSparse< GUM_SCALAR >::get ( const Instantiation i) const
virtual

Returns the value pointed by i.

Warning
If i variables set is disjoint with this MultiDimContainer then 0 is assumed for dimensions (i.e. variables) not present in the instantiation.
Parameters
iAn Instantiation of this MultiDimContainer.
Returns
Returns the value pointe by i.

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 61 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

61  {
62  Size key;
63 
64  if (i.isMaster(this)) {
65  key = this->offsets_[&i];
66  } else {
67  key = this->getOffs_(i);
68  }
69 
70  return params_.exists(key) ? params_[key] : default_;
71  }
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
GUM_SCALAR default_
The default value.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ get_()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR & gum::MultiDimSparse< GUM_SCALAR >::get_ ( const Instantiation i) const
protectedvirtual
Warning
Will raise an OperationNotAllowed as this use its own internal datastructure.

Implements gum::MultiDimWithOffset< GUM_SCALAR >.

Definition at line 144 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

144  {
145  GUM_ERROR(OperationNotAllowed, "Do not use this with the MultiDimSparse class.")
146  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ getMasterRef() [1/2]

template<typename GUM_SCALAR >
INLINE MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( )
virtualinherited

In order to insure the dereference for decorators, we need to virtualize the access to master pointer.

Returns
Returns the ref to content as MultiDimAdressable&

Implements gum::MultiDimAdressable.

Reimplemented in gum::MultiDimBucket< GUM_SCALAR >.

Definition at line 290 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

290  {
291  return static_cast< MultiDimAdressable& >(*content());
292  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
MultiDimAdressable()
Default constructor.
+ Here is the call graph for this function:

◆ getMasterRef() [2/2]

template<typename GUM_SCALAR >
INLINE const MultiDimAdressable & gum::MultiDimContainer< GUM_SCALAR >::getMasterRef ( ) const
virtualinherited

In order to insure the dereference for decorators, we need to virtualize the access to master pointer.

Returns
Returns the master of this MultiDimAdressable.

Implements gum::MultiDimAdressable.

Reimplemented in gum::MultiDimBucket< GUM_SCALAR >.

Definition at line 295 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

295  {
296  return static_cast< const MultiDimAdressable& >(*content());
297  }
virtual const MultiDimImplementation< GUM_SCALAR > * content() const =0
Returns the implementation for this object (may be *this).
MultiDimAdressable()
Default constructor.
+ Here is the call graph for this function:

◆ getOffs_()

template<typename GUM_SCALAR >
INLINE Size gum::MultiDimWithOffset< GUM_SCALAR >::getOffs_ ( const Instantiation i) const
protectedinherited

Compute the offset of a Instantiation.

If the instantiation is not fully compatible with the MultiDimWithOffset, no exception thrown but 0 is assumed for dimensions not present in the instantiation.

If the instantiation is not fully compatible with the MultiDimWithOffset, no exception thrown but 0 is assumed for dimensions not present in the instantiation.

for instance : M<<a<<b<<c; with i=b:1|c:2|d:1 then M.getOffs_(i) give the offset of a:0|b:1|c:2.

Parameters
iAn instantiation for which the offset is computed.
Returns
The offset of i.

for instance : M<<a<<b<<c; with i=b:1|c:2|d:1 then M.getOffs_(i) give the offset of a:0|b:1|c:2.

Definition at line 197 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

197  {
198  Idx off = 0;
199 
200  for (HashTableConstIteratorSafe< const DiscreteVariable*, Size > iter = gaps_.beginSafe();
201  iter != gaps_.endSafe();
202  ++iter)
203  if (i.contains(iter.key()))
204  off += iter.val() * i.valFromPtr(iter.key());
205  else
206  GUM_ERROR(InvalidArgument, iter.key()->name() << " not present in the instantiation " << i)
207 
208  return off;
209  }
HashTable< const DiscreteVariable *, Size > gaps_
The gaps between consecutive values of a given variable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ invert_()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimImplementation< GUM_SCALAR >::invert_ ( Idx  p1,
Idx  p2 
)
protectedvirtualinherited

Inverts variables at position p1 and p2.

Call this from subclass when you want to invert position of two variables in the MultiDimImplementation.

Parameters
p1The first position.
p2The second position.

Definition at line 291 of file multiDimImplementation_tpl.h.

291  {
292  _vars_.swap(p1, p2);
293  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

◆ isCommitNeeded_()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimImplementation< GUM_SCALAR >::isCommitNeeded_ ( ) const
protectedinherited

Get the actual state of *this.

Returns
Returns true if a commit is needed.

Definition at line 249 of file multiDimImplementation_tpl.h.

◆ isInMultipleChangeMethod_()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimImplementation< GUM_SCALAR >::isInMultipleChangeMethod_ ( ) const
protectedinherited

Get the actual change method of this MultiDimImplementation.

Returns
Returns true if in multiple changes.

Definition at line 243 of file multiDimImplementation_tpl.h.

◆ name()

template<typename GUM_SCALAR >
INLINE const std::string & gum::MultiDimSparse< GUM_SCALAR >::name ( ) const
virtual

Returns the real name of the multiDim implementation.

In aGrUM, all the types of multi-dimensional arrays/functionals have a name that describes what they are in reality. For instance, a table stored in extension is a "MultiDimArray", one that stores only non zero elements is a "MultiDimSparseArray", and so on. These names are unique for each type of implementation and is used by the system to determine which is the best functions to use, say, when we wish to use operators such as operator+ on two MultiDimImplementations.

Returns
Returns the real name of the multiDim implementation

Implements gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 132 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

132  {
133  static const std::string str = "MultiDimSparse";
134  return str;
135  }
+ Here is the call graph for this function:

◆ nbrDim()

template<typename GUM_SCALAR >
INLINE Idx gum::MultiDimImplementation< GUM_SCALAR >::nbrDim ( ) const
overridevirtualinherited

Returns the number of vars in the multidimensional container.

Returns
Returns the number of vars in the multidimensional container.

Implements gum::MultiDimInterface.

Definition at line 157 of file multiDimImplementation_tpl.h.

157  {
158  return _vars_.size();
159  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

◆ newFactory()

template<typename GUM_SCALAR >
INLINE MultiDimContainer< GUM_SCALAR > * gum::MultiDimSparse< GUM_SCALAR >::newFactory ( ) const
virtual

Class constructor.

Implements gum::MultiDimWithOffset< GUM_SCALAR >.

Definition at line 126 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

126  {
127  return new MultiDimSparse< GUM_SCALAR >(default_);
128  }
GUM_SCALAR default_
The default value.
+ Here is the call graph for this function:

◆ operator!=()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimContainer< GUM_SCALAR >::operator!= ( const MultiDimContainer< GUM_SCALAR > &  p) const
inherited

Test if this MultiDimContainer is different of p.

Parameters
pThe MultiDimContainer to test for inequality.
Returns
Returns true if this MultiDimContainer is different of p.

Definition at line 154 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

154  {
155  return !operator==(p);
156  }
bool operator==(const MultiDimContainer< GUM_SCALAR > &p) const
Test if this MultiDimContainer is equal to p.
+ Here is the call graph for this function:

◆ operator==()

template<typename GUM_SCALAR >
bool gum::MultiDimContainer< GUM_SCALAR >::operator== ( const MultiDimContainer< GUM_SCALAR > &  p) const
inherited

Test if this MultiDimContainer is equal to p.

Parameters
pThe MultiDimContainer to test for equality.
Returns
Returns true if this MultiDimContainer is equal to p.

Definition at line 124 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

124  {
125  if ((nbrDim() == p.nbrDim()) && (domainSize() == p.domainSize())) {
126  if (nbrDim() == 0) return true;
127 
129 
130  for (var_iterator iter = variablesSequence().beginSafe();
131  iter != variablesSequence().endSafe();
132  ++iter) {
133  if (!p.variablesSequence().exists(*iter)) { return false; }
134  }
135  } else {
136  return false;
137  }
138 
139  Instantiation i(*this);
140 
141  AlmostDifferent< GUM_SCALAR > cmp;
142 
143  for (i.setFirst(); !i.end(); ++i) {
144  if (cmp(get(i), p.get(i))) { return false; }
145  }
146 
147  return true;
148  }
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const =0
Returns the product of the variables domain size.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition: sequence.h:1032
+ Here is the call graph for this function:

◆ operator[]()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MultiDimContainer< GUM_SCALAR >::operator[] ( const Instantiation i) const
inherited

An [] operator using a Instantiation as argument.

Warning
If i variables set is disjoint with this MultiDimContainer then 0 is assumed for dimensions (i.e. variables) not prensent in the instantiation.
Parameters
iAn Instantiation.
Returns
Returns the adressed (GUM_SCALAR) value.

Definition at line 79 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

79  {
80  return get(i);
81  }
+ Here is the call graph for this function:

◆ populate() [1/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::populate ( const std::vector< GUM_SCALAR > &  v) const
virtualinherited

Automatically fills this MultiDimContainer with the values in v.

The order used to fill this MultiDimContainer is the same as with an instantiation over it.

Size cpt = 0;
Instantiation i( *this );
for (i.setFirst(); !i.end(); ++i, ++cpt) {
set(i, v[cpt]);
}
Parameters
vVector of values.
Exceptions
SizeErrorRaised if v size's does not matches this MultiDimContainer domain size.

Reimplemented in gum::MultiDimDecorator< GUM_SCALAR >, gum::MultiDimBijArray< GUM_SCALAR >, gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 160 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

160  {
161  if (domainSize() != v.size()) {
162  GUM_ERROR(SizeError, "Sizes do not match : " << domainSize() << "!=" << v.size())
163  }
164 
165  Size cpt = 0;
166 
167  Instantiation i(*this);
168 
169  for (i.setFirst(); !i.end(); ++i, ++cpt)
170  set(i, v[cpt]);
171  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ populate() [2/2]

template<typename GUM_SCALAR >
void gum::MultiDimContainer< GUM_SCALAR >::populate ( std::initializer_list< GUM_SCALAR >  l) const
virtualinherited

Automatically fills this MultiDimContainer with the values in l.

The order used to fill this MultiDimContainer is the same as with an instantiation over it.

Size cpt = 0;
Instantiation i( *this );
for (i.setFirst(); !i.end(); ++i, ++cpt) {
set(i, v[cpt]);
}
Parameters
lcontains the data.
Exceptions
SizeErrorRaised if l size's does not matches this MultiDimContainer domain size.

Reimplemented in gum::MultiDimBijArray< GUM_SCALAR >, gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >, gum::MultiDimFunctionGraph< GUM_SCALAR >, and gum::MultiDimFunctionGraph< GUM_SCALAR, ExactTerminalNodePolicy >.

Definition at line 174 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

174  {
175  if (domainSize() != l.size()) {
176  GUM_ERROR(SizeError, "Sizes do not match : " << domainSize() << "!=" << l.size())
177  }
178 
179  Instantiation i(*this);
180  // insert all the elements
181  for (const auto& elt: l) {
182  set(i, elt);
183  ++i;
184  }
185  }
virtual Size domainSize() const =0
Returns the product of the variables domain size.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ pos()

template<typename GUM_SCALAR >
INLINE Idx gum::MultiDimImplementation< GUM_SCALAR >::pos ( const DiscreteVariable v) const
overridevirtualinherited

Returns the index of a variable.

Parameters
vThe variable for which the index is returned.
Returns
Returns the index of a variable.
Exceptions
NotFoundRaised if v is not in this multidimensional matrix.

Implements gum::MultiDimInterface.

Definition at line 182 of file multiDimImplementation_tpl.h.

182  {
183  return _vars_.pos(&v);
184  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

◆ realSize()

template<typename GUM_SCALAR >
INLINE Size gum::MultiDimSparse< GUM_SCALAR >::realSize ( ) const
virtual

Returns the real number of parameters used for this table.

This function is used by the MultiDimImplementation::compressionRate() method.

See also
MultiDimImplementation::compressionRate()
Returns
Returns the real number of parameters used for this table.

Implements gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 121 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

121  {
122  return params_.size();
123  }
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
+ Here is the call graph for this function:

◆ reduce()

template<typename GUM_SCALAR >
GUM_SCALAR gum::MultiDimContainer< GUM_SCALAR >::reduce ( std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) >  f,
GUM_SCALAR  base 
) const
virtualinherited

compute lfold for this container

Parameters
fthe function to apply
basethe initial value

Reimplemented in gum::MultiDimDecorator< GUM_SCALAR >, and gum::MultiDimArray< GUM_SCALAR >.

Definition at line 197 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

198  {
199  GUM_SCALAR tmp = base;
200  Instantiation i(*this);
201  for (i.setFirst(); !i.end(); ++i) {
202  tmp = f(tmp, get(i));
203  }
204  return tmp;
205  }
+ Here is the call graph for this function:

◆ registerSlave()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimWithOffset< GUM_SCALAR >::registerSlave ( Instantiation i)
virtualinherited

Register i as a slave of this MultiDimAdressable.

Parameters
iThe Instantiation to enslave.
Returns
Returns true if i becomes a slave of this MultiDimAdressable.

Reimplemented from gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 169 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

169  {
171  GUM_ASSERT(!offsets_.exists(&i));
172  offsets_.insert(&i, getOffs_(i));
173  return true;
174  }
175 
176  return false;
177  }
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual bool registerSlave(Instantiation &slave) override
Register i as a slave of this MultiDimAdressable.
+ Here is the call graph for this function:

◆ replace()

INLINE void gum::MultiDimInterface::replace ( const DiscreteVariable x,
const DiscreteVariable y 
)
inherited

Replace variables in this multidim.

If x is in this MultiDim and y has the same domain size, then x will be replace by y in this MultiDim.

Parameters
xThe variable in this which will be replaced.
yThe variable replacing y.
Exceptions
NotFoundRaised if x does not belong to this MultiDim.
OperationNotAllowedIf y and x are not interchangeable.
DuplicateElementIf y is already in this MultiDim.

Definition at line 35 of file multiDimInterface_inl.h.

References gum::Set< Key, Alloc >::emplace().

35  {
36  if (!contains(x)) { GUM_ERROR(NotFound, "could not find the variable") }
37 
38  if (contains(y)) { GUM_ERROR(DuplicateElement, "variable " << y << " already in MultiDim") }
39 
40  if (x.domainSize() != y.domainSize()) {
41  GUM_ERROR(OperationNotAllowed, "incompatible variables")
42  }
43 
44  replace_(&x, &y);
45  }
virtual bool contains(const DiscreteVariable &v) const =0
Returns true if var is in *this.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)=0
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ replace_()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::replace_ ( const DiscreteVariable x,
const DiscreteVariable y 
)
protectedvirtual

Replace variable x by y.

Technically this should be call by any subclass overloading this method to proceed with the changes in this class containers.

Parameters
xThe first variable to swap.
yThe second variable to swap.

Reimplemented from gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 138 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

139  {
141  }
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
+ Here is the call graph for this function:

◆ set()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimSparse< GUM_SCALAR >::set ( const Instantiation i,
const GUM_SCALAR &  value 
) const
virtual

Changes the value pointed by i.

Warning
If i variables set is disjoint with this MultiDimContainer then 0 is assumed for dimensions (i.e. variables) not prensent in the instantiation.
Parameters
iAn Instantiation of this MultiDimContainer.
valueThe new value.

Reimplemented from gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 74 of file multiDimSparse_tpl.h.

References gum::Set< Key, Alloc >::emplace().

75  {
76  Size key;
77 
78  if (i.isMaster(this)) {
79  key = this->offsets_[&i];
80  } else {
81  key = this->getOffs_(i);
82  }
83 
84  if (value == default_) {
85  params_.reset(key);
86  } else {
87  params_.set(key, value);
88  }
89  }
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
GUM_SCALAR default_
The default value.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
void reset(const Key &key)
Removes a property (i.e., remove an element).
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ setChangeNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::setChangeNotification ( const Instantiation i)
virtualinherited

Listen to an assignment of a value in a Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 127 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

127  {
128  GUM_ASSERT(offsets_.exists(&i));
129  offsets_[&i] = getOffs_(i);
130  }
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
+ Here is the call graph for this function:

◆ setDecNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::setDecNotification ( const Instantiation i)
virtualinherited

Listen to increment in each recorded Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 160 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

160  {
161  GUM_ASSERT(offsets_.exists(&i));
162  GUM_ASSERT(offsets_[&i] != 0);
163  --offsets_[&i];
164  }
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
+ Here is the call graph for this function:

◆ setFirstNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::setFirstNotification ( const Instantiation i)
virtualinherited

Listen to setFirst in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 135 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

135  {
136  GUM_ASSERT(offsets_.exists(&i));
137  offsets_[&i] = 0;
138  }
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
+ Here is the call graph for this function:

◆ setIncNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::setIncNotification ( const Instantiation i)
virtualinherited

Listen to increment in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 151 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

151  {
152  GUM_ASSERT(offsets_.exists(&i));
153  GUM_ASSERT(offsets_[&i] != this->domainSize() - 1);
154  ++offsets_[&i];
155  }
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual Size domainSize() const override
Returns the product of the variables domain size.
+ Here is the call graph for this function:

◆ setLastNotification()

template<typename GUM_SCALAR >
INLINE void gum::MultiDimWithOffset< GUM_SCALAR >::setLastNotification ( const Instantiation i)
virtualinherited

Listen to setLast in a given Instantiation.

Parameters
iThe Instantiation to listen.

Implements gum::MultiDimAdressable.

Definition at line 143 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

143  {
144  GUM_ASSERT(offsets_.exists(&i));
145  offsets_[&i] = this->domainSize() - 1;
146  }
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual Size domainSize() const override
Returns the product of the variables domain size.
+ Here is the call graph for this function:

◆ slaves_()

template<typename GUM_SCALAR >
INLINE const List< Instantiation *> & gum::MultiDimImplementation< GUM_SCALAR >::slaves_ ( ) const
protectedinherited

Returns a constant reference over the list of slaved instantiations.

Returns
Returns a constant reference over the list of slaved instantiations.

Definition at line 255 of file multiDimImplementation_tpl.h.

255  {
256  return _slaveInstantiations_;
257  }
List< Instantiation *> _slaveInstantiations_
List of instantiations of the tuples (sequences) of variables.

◆ toOffset()

template<typename GUM_SCALAR >
INLINE Size gum::MultiDimWithOffset< GUM_SCALAR >::toOffset ( const Instantiation i) const
inherited

Compute offset from an Instantiation (in this array).

If the instantiation is not fully compatible with the MultiDimWithOffset, no exception will be thrown but 0 is assumed for dimensions not present in the instantiation. for instance : M<<a<<b<<c; with i=b:1|c:2|d:1 then M.toOffset(i) give the offset of a:0|b:1|c:2.

Definition at line 250 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

250  {
251  return getOffs_(i);
252  }
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
+ Here is the call graph for this function:

◆ toString() [1/2]

template<typename GUM_SCALAR >
INLINE std::string gum::MultiDimWithOffset< GUM_SCALAR >::toString ( const Instantiation i) const
virtualinherited

Display the internal representation of i.

Returns
Returns an internal representation of i.

Implements gum::MultiDimContainer< GUM_SCALAR >.

Definition at line 237 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

237  {
238  if (i->isMaster(this)) {
239  std::stringstream s;
240  s << offsets_[i];
241  std::string res;
242  s >> res;
243  return res;
244  } else {
245  return "--";
246  }
247  }
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
+ Here is the call graph for this function:

◆ toString() [2/2]

template<typename GUM_SCALAR >
std::string gum::MultiDimContainer< GUM_SCALAR >::toString ( ) const
virtualinherited

Returns a representation of this MultiDimContainer.

Returns
Returns a representation of this MultiDimContainer.

Reimplemented in gum::Potential< GUM_SCALAR >, gum::MultiDimDecorator< GUM_SCALAR >, gum::aggregator::MultiDimAggregator< GUM_SCALAR >, gum::MultiDimNoisyAND< GUM_SCALAR >, gum::MultiDimNoisyORCompound< GUM_SCALAR >, gum::MultiDimNoisyORNet< GUM_SCALAR >, gum::MultiDimLogit< GUM_SCALAR >, and gum::MultiDimICIModel< GUM_SCALAR >.

Definition at line 101 of file multiDimContainer_tpl.h.

References gum::Set< Key, Alloc >::emplace().

101  {
102  // we create a new instantiation and iterate over it to display the whole
103  // content of the array
104  if (this->nbrDim() == 0) { return "[]"; }
105 
106  std::stringstream ss;
107  Instantiation inst(const_cast< MultiDimContainer* >(this));
108 
109  bool first = true;
110 
111  for (inst.setFirst(); !inst.end(); ++inst) {
112  if (!first) { ss << " /"; }
113  first = false;
114 
115  ss << inst << " :: " << get(inst);
116  }
117 
118  return ss.str();
119  }
virtual Idx nbrDim() const =0
Returns the number of vars in the multidimensional container.
+ Here is the call graph for this function:

◆ unregisterSlave()

template<typename GUM_SCALAR >
INLINE bool gum::MultiDimWithOffset< GUM_SCALAR >::unregisterSlave ( Instantiation i)
virtualinherited

Unregister i as a slave of this MultiDimAdressable.

Parameters
iThe Instantiation to free.
Returns
Returns true, whatever happens.

Reimplemented from gum::MultiDimImplementation< GUM_SCALAR >.

Definition at line 182 of file multiDimWithOffset_tpl.h.

References gum::Set< Key, Alloc >::emplace().

182  {
184  offsets_.erase(&i);
185  return true;
186  }
virtual bool unregisterSlave(Instantiation &slave) override
Unregister i as a slave of this MultiDimAdressable.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
+ Here is the call graph for this function:

◆ variable() [1/2]

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::MultiDimImplementation< GUM_SCALAR >::variable ( Idx  i) const
overridevirtualinherited

Returns a const ref to the ith var.

param i An index of this multidimensional matrix.

Returns
Returns a const ref to the ith var.
Exceptions
NotFoundRaised if i does not reference a variable in this multidimensional matrix.

Implements gum::MultiDimInterface.

Definition at line 167 of file multiDimImplementation_tpl.h.

167  {
168  return *(_vars_.atPos(i));
169  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

◆ variable() [2/2]

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::MultiDimImplementation< GUM_SCALAR >::variable ( const std::string &  name) const
overridevirtualinherited

Returns the variable with the name.

Parameters
nameThe index of the variable
Returns
Returns the variable qith the name in the tuple. This function is not O(1)
Exceptions
NotFoundRaised if the element cannot be found.

Implements gum::MultiDimInterface.

Definition at line 173 of file multiDimImplementation_tpl.h.

173  {
174  for (const auto& v: _vars_) {
175  if (v->name() == name) return *v;
176  }
177 
178  GUM_ERROR(NotFound, "'" << name << "' can not be found in the multidim structure.")
179  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).
virtual const std::string & name() const =0
Returns the real name of the multiDim implementation.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ variablesSequence()

template<typename GUM_SCALAR >
INLINE const Sequence< const DiscreteVariable *> & gum::MultiDimImplementation< GUM_SCALAR >::variablesSequence ( ) const
overridevirtualinherited

Returns a const ref to the sequence of DiscreteVariable*.

Returns
Returns a const ref to the sequence of DiscreteVariable*.

Implements gum::MultiDimInterface.

Definition at line 195 of file multiDimImplementation_tpl.h.

195  {
196  return _vars_;
197  }
Sequence< const DiscreteVariable *> _vars_
List of discrete variables (dimensions).

Member Data Documentation

◆ default_

template<typename GUM_SCALAR >
GUM_SCALAR gum::MultiDimSparse< GUM_SCALAR >::default_
mutableprotected

The default value.

Definition at line 118 of file multiDimSparse.h.

◆ gaps_

template<typename GUM_SCALAR >
HashTable< const DiscreteVariable*, Size > gum::MultiDimWithOffset< GUM_SCALAR >::gaps_
protectedinherited

The gaps between consecutive values of a given variable.

For each variable, we keep track of the interval between two consecutive values of the variable in vector values. Thus, shifting from one value to the next one is equivalent to incrementing/decrementing by gaps the current offset w.r.t. vector values.

Definition at line 178 of file multiDimWithOffset.h.

◆ offsets_

template<typename GUM_SCALAR >
HashTable< const Instantiation*, Size > gum::MultiDimWithOffset< GUM_SCALAR >::offsets_
protectedinherited

The position in the array of each slave Instantiation.

Definition at line 181 of file multiDimWithOffset.h.

◆ params_

template<typename GUM_SCALAR >
HashTable< Size, GUM_SCALAR > gum::MultiDimSparse< GUM_SCALAR >::params_
mutableprotected

The true data : the values is mutable since we can change the value in a const multiDimArray.

Definition at line 115 of file multiDimSparse.h.


The documentation for this class was generated from the following files: