aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
multiDimDecorator.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
48#ifndef GUM_MULTI_DIM_DECORATOR_H
49#define GUM_MULTI_DIM_DECORATOR_H
50
51#include <mutex>
52
53#include <agrum/agrum.h>
54
56
57namespace gum {
58 class DiscreteVariable;
59
60 // ===========================================================================
61 // === MULTIDIM DECORATOR ===
62 // ===========================================================================
63
80 template < typename GUM_ELEMENT >
81 class MultiDimDecorator: public MultiDimContainer< GUM_ELEMENT > {
82 public:
83 // =========================================================================
85 // =========================================================================
87
93 GUM_ELEMENT empty_value = (GUM_ELEMENT)0);
94
99
104
109
114
118 ~MultiDimDecorator() override;
119
121 // =========================================================================
123 // =========================================================================
125
126 Idx nbrDim() const final;
127
128 Size domainSize() const final;
129
130 void add(const DiscreteVariable& v) final;
131
132 void erase(const DiscreteVariable& var) final;
133 virtual void erase(std::string_view name) final;
134
136 const DiscreteVariable& variable(Idx) const final;
137 const DiscreteVariable& variable(std::string_view name) const final;
138
139 Idx pos(const DiscreteVariable& var) const final;
140
141 bool contains(const DiscreteVariable& var) const final;
142
143 bool empty() const final;
144
146 // ========================================================================
148 // ========================================================================
150
151 bool unregisterSlave(Instantiation& i) final;
152
153 bool registerSlave(Instantiation& i) final;
154
155 void changeNotification(const Instantiation& i,
156 const DiscreteVariable* const var,
157 Idx oldval,
158 Idx newval) final;
159
160 void setChangeNotification(const Instantiation& i) final;
161
162 void setFirstNotification(const Instantiation& i) final;
163
164 void setLastNotification(const Instantiation& i) final;
165
166 void setIncNotification(const Instantiation& i) final;
167
168 void setDecNotification(const Instantiation& i) final;
169
170 virtual void notifyChange() const final;
171
173 // =========================================================================
175 // =========================================================================
177
183 void set(const Instantiation& i, const GUM_ELEMENT& value) const final;
184
190 GUM_ELEMENT get(const Instantiation& i) const final;
191
192 void fill(const GUM_ELEMENT& d) const final;
193
212 void populate(const std::vector< GUM_ELEMENT >& v) const final;
213
218 void apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const final;
219
225 GUM_ELEMENT reduce(std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f,
226 GUM_ELEMENT base) const final;
227
228 [[nodiscard]] MultiDimDecorator< GUM_ELEMENT >* newFactory() const override = 0;
229
230 void beginMultipleChanges() final;
231
232 void endMultipleChanges() final;
233
234 void endMultipleChanges(const GUM_ELEMENT&) final;
235
236 std::string toString(const Instantiation* i) const override;
237
238 std::string toString() const override;
239
241 // =========================================================================
243 // =========================================================================
245
246
250 const MultiDimImplementation< GUM_ELEMENT >* content() const final;
251
255 MultiDimImplementation< GUM_ELEMENT >* content() final;
256
258
259 protected:
260 void replace_(const DiscreteVariable* x, const DiscreteVariable* y) override;
261
266 void swapContent_(MultiDimImplementation< GUM_ELEMENT >* aContent) const;
267
271 mutable MultiDimImplementation< GUM_ELEMENT >* content_;
272
279 GUM_ELEMENT& get_(const Instantiation& i) const final;
280
285 mutable GUM_ELEMENT empty_value_;
286 };
287
288} /* namespace gum */
289
290#include <agrum/base/multidim/implementations/multiDimDecorator_tpl.h>
291
292#endif /* GUM_MULTI_DIM_DECORATOR_H */
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
MultiDimContainer()
Default constructor.
void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
void populate(const std::vector< GUM_ELEMENT > &v) const final
Automatically fills this MultiDimContainer with the values in v.
MultiDimDecorator< GUM_ELEMENT > * newFactory() const override=0
Default implementation of MultiDimContainer::set().
Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
const MultiDimImplementation< GUM_ELEMENT > * content() const final
Returns the implementation for this object (may be *this).
void fill(const GUM_ELEMENT &d) const final
Default implementation of MultiDimContainer::set().
bool empty() const final
Returns true if no var is in *this.
void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.
const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
MultiDimDecorator(MultiDimImplementation< GUM_ELEMENT > *aContent=nullptr, GUM_ELEMENT empty_value=(GUM_ELEMENT) 0)
Class constructor.
std::string toString(const Instantiation *i) const override
Default implementation of MultiDimContainer::set().
const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
void swapContent_(MultiDimImplementation< GUM_ELEMENT > *aContent) const
protected method to swap the implementation behind the Tensor
void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.
MultiDimDecorator< GUM_ELEMENT > & operator=(const MultiDimDecorator &from) noexcept
copy operator
void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.
GUM_ELEMENT & get_(const Instantiation &i) const final
Return a data, given a Instantiation - final method.
virtual void notifyChange() const final
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.
GUM_ELEMENT empty_value_
value of the MultiDimDecorator if no dimension.
bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
GUM_ELEMENT reduce(std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f, GUM_ELEMENT base) const final
compute lfold for this container
~MultiDimDecorator() override
Class destructor.
void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.
void set(const Instantiation &i, const GUM_ELEMENT &value) const final
Default implementation of MultiDimContainer::set().
void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.
MultiDimImplementation< GUM_ELEMENT > * content_
The true container.
void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
Size domainSize() const final
Returns the product of the variables domain size.
bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.
void apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const final
Apply a function on every element of the container.
GUM_ELEMENT get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.
<agrum/base/multidim/multiDimImplementation.h>
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Header of the MultiDimArray class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.