aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
49
50#include <agrum/base/multidim/implementations/multiDimSparse.h> // to ease IDE parser
51
52namespace gum {
53
54 // Default constructor: creates an empty null dimensional matrix
55
56 template < typename GUM_ELEMENT >
57 MultiDimSparse< GUM_ELEMENT >::MultiDimSparse(const GUM_ELEMENT& default_value) :
58 MultiDimWithOffset< GUM_ELEMENT >(), default_(default_value) {
59 // for debugging purposes
60 GUM_CONSTRUCTOR(MultiDimSparse);
61 }
62
63 // copy constructor
64
65 template < typename GUM_ELEMENT >
67 MultiDimWithOffset< GUM_ELEMENT >(from), params_(from.params_), default_(from.default_) {
68 // for debugging purposes
69 GUM_CONS_CPY(MultiDimSparse);
70 }
71
72 // destructor
73
74 template < typename GUM_ELEMENT >
76 // for debugging purposes
77 GUM_DESTRUCTOR(MultiDimSparse);
78 // no need to unregister all slaves as it will be done by MultiDimWithOffset
79 }
80
81 // data access operator
82
83 template < typename GUM_ELEMENT >
85 Size key;
86
87 if (i.isMaster(this)) {
88 key = this->offsets_[&i];
89 } else {
90 key = this->getOffs_(i);
91 }
92
93 return params_.exists(key) ? params_[key] : default_;
94 }
95
96 template < typename GUM_ELEMENT >
97 void MultiDimSparse< GUM_ELEMENT >::set(const Instantiation& i, const GUM_ELEMENT& value) const {
98 Size key;
99
100 if (i.isMaster(this)) {
101 key = this->offsets_[&i];
102 } else {
103 key = this->getOffs_(i);
104 }
105
106 if (value == default_) {
107 params_.reset(key);
108 } else {
109 params_.set(key, value);
110 }
111 }
112
113 // add a new dimension, needed for updating the offsets_ & gaps_
114
115 template < typename GUM_ELEMENT >
120
121 // removes a dimension, needed for updating the offsets_ & gaps_
122
123 template < typename GUM_ELEMENT >
128
129 // synchronise content after MultipleChanges
130 template < typename GUM_ELEMENT >
134
135 // fill the array with the arg
136 template < typename GUM_ELEMENT >
137 void MultiDimSparse< GUM_ELEMENT >::fill(const GUM_ELEMENT& d) const {
138 params_.clear();
139 default_ = d;
140 }
141
142 template < typename GUM_ELEMENT >
144 return params_.size();
145 }
146
147 template < typename GUM_ELEMENT >
151
152 // returns the name of the implementation
153 template < typename GUM_ELEMENT >
154 const std::string& MultiDimSparse< GUM_ELEMENT >::name() const {
155 static const std::string str = "MultiDimSparse";
156 return str;
157 }
158
159 template < typename GUM_ELEMENT >
164
165 template < typename GUM_ELEMENT >
167 GUM_ERROR(OperationNotAllowed, "Do not use this with the MultiDimSparse class.")
168 }
169
170} /* 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.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
void fill(const GUM_ELEMENT &d) const override
Fill the table with d.
MultiDimContainer< GUM_ELEMENT > * newFactory() const override
Class constructor.
~MultiDimSparse() override
Copy operator.
const std::string & name() const override
Returns the real name of the multiDim implementation.
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
GUM_ELEMENT get(const Instantiation &i) const override
Returns the value pointed by i.
void set(const Instantiation &i, const GUM_ELEMENT &value) const override
Changes the value pointed by i.
HashTable< Size, GUM_ELEMENT > params_
The true data : the values is mutable since we can change the value in a const multiDimArray.
GUM_ELEMENT & get_(const Instantiation &i) const override
void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
GUM_ELEMENT default_
The default value.
Size realSize() const override
Returns the real number of parameters used for this table.
MultiDimSparse(const GUM_ELEMENT &default_value)
Default constructor: creates an empty null dimensional matrix.
void commitMultipleChanges_() override
Synchronise content after MultipleChanges.
Size getOffs_(const Instantiation &i) const
Compute the offset of a Instantiation.
void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
MultiDimWithOffset()
Class constructor.
HashTable< const Instantiation *, Size > offsets_
The position in the array of each slave Instantiation.
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Headers of MultiDimSparse.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46