aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimSparse_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
48
49namespace gum {
50
51 // Default constructor: creates an empty null dimensional matrix
52
53 template < typename GUM_SCALAR >
54 MultiDimSparse< GUM_SCALAR >::MultiDimSparse(const GUM_SCALAR& default_value) :
55 MultiDimWithOffset< GUM_SCALAR >(), default_(default_value) {
56 // for debugging purposes
57 GUM_CONSTRUCTOR(MultiDimSparse);
58 }
59
60 // copy constructor
61
62 template < typename GUM_SCALAR >
64 MultiDimWithOffset< GUM_SCALAR >(from), params_(from.params_), default_(from.default_) {
65 // for debugging purposes
66 GUM_CONS_CPY(MultiDimSparse);
67 }
68
69 // destructor
70
71 template < typename GUM_SCALAR >
73 // for debugging purposes
74 GUM_DESTRUCTOR(MultiDimSparse);
75 // no need to unregister all slaves as it will be done by MultiDimWithOffset
76 }
77
78 // data access operator
79
80 template < typename GUM_SCALAR >
81 INLINE GUM_SCALAR MultiDimSparse< GUM_SCALAR >::get(const Instantiation& i) const {
82 Size key;
83
84 if (i.isMaster(this)) {
85 key = this->offsets_[&i];
86 } else {
87 key = this->getOffs_(i);
88 }
89
90 return params_.exists(key) ? params_[key] : default_;
91 }
92
93 template < typename GUM_SCALAR >
95 const GUM_SCALAR& value) const {
96 Size key;
97
98 if (i.isMaster(this)) {
99 key = this->offsets_[&i];
100 } else {
101 key = this->getOffs_(i);
102 }
103
104 if (value == default_) {
105 params_.reset(key);
106 } else {
107 params_.set(key, value);
108 }
109 }
110
111 // add a new dimension, needed for updating the offsets_ & gaps_
112
113 template < typename GUM_SCALAR >
118
119 // removes a dimension, needed for updating the offsets_ & gaps_
120
121 template < typename GUM_SCALAR >
126
127 // synchronise content after MultipleChanges
128 template < typename GUM_SCALAR >
132
133 // fill the array with the arg
134 template < typename GUM_SCALAR >
135 INLINE void MultiDimSparse< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
136 params_.clear();
137 default_ = d;
138 }
139
140 template < typename GUM_SCALAR >
142 return params_.size();
143 }
144
145 template < typename GUM_SCALAR >
149
150 // returns the name of the implementation
151 template < typename GUM_SCALAR >
152 INLINE const std::string& MultiDimSparse< GUM_SCALAR >::name() const {
153 static const std::string str = "MultiDimSparse";
154 return str;
155 }
156
157 template < typename GUM_SCALAR >
162
163 template < typename GUM_SCALAR >
164 INLINE GUM_SCALAR& MultiDimSparse< GUM_SCALAR >::get_(const Instantiation& i) const {
165 GUM_ERROR(OperationNotAllowed, "Do not use this with the MultiDimSparse class.")
166 }
167
168} /* namespace gum */
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
Abstract base class for all multi dimensionnal containers.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual Size realSize() const
Returns the real number of parameters used for this table.
MultiDimSparse(const GUM_SCALAR &default_value)
Default constructor: creates an empty null dimensional matrix.
void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
virtual ~MultiDimSparse()
Copy operator.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
Changes the value pointed by i.
virtual void fill(const GUM_SCALAR &d) const
Fill the table with d.
GUM_SCALAR default_
The default value.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Class constructor.
virtual GUM_SCALAR & get_(const Instantiation &i) const
virtual void commitMultipleChanges_()
Synchronise content after MultipleChanges.
virtual const std::string & name() const
Returns the real name of the multiDim implementation.
HashTable< Size, GUM_SCALAR > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
Replace variable x by y.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
MultiDimWithOffset()
Class constructor.
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46