aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimDecorator_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
42
49
50#include <mutex>
51
52// include the operators that will be used by the decorators
58
59namespace gum {
60 // instrumental and non-API function
61 template < typename GUM_SCALAR >
63 // ensure that only one thread will register the projections
64 static std::once_flag first;
65 std::call_once(first, []() {
66 // register the operators that will be used by the decorator
68 op.init();
69
70 // register the projectors that will be used by the decorator
72 proj.init();
73
74 // register the projectors that will be used by the decorator
76 comp_proj.init();
77
78 // register the partial instantiators that will be used by the decorator
80 inst.init();
81 });
82 }
83
84 // constructors
85 template < typename GUM_SCALAR >
88 GUM_SCALAR empty_value) : content_(aContent), empty_value_(empty_value) {
90 GUM_CONSTRUCTOR(MultiDimDecorator);
91 }
92
93 template < typename GUM_SCALAR >
101
102 template < typename GUM_SCALAR >
106 std::forward< MultiDimContainer< GUM_SCALAR > >(from));
107 GUM_OP_MOV(MultiDimDecorator);
108
109 if (this != &from) {
110 if (content_ != nullptr) delete (content_); // should be the case
111 empty_value_ = from.empty_value_;
112 content_ = from.content_;
113 from.content_ = nullptr;
114 }
115
116 return *this;
117 }
118
119 template < typename GUM_SCALAR >
121 MultiDimDecorator< GUM_SCALAR >&& from) noexcept :
123 GUM_CONS_MOV(MultiDimDecorator);
124
125 empty_value_ = from.empty_value_;
126 content_ = from.content_;
127 from.content_ = nullptr;
128 }
129
130 template < typename GUM_SCALAR >
132 const MultiDimDecorator< GUM_SCALAR >& from) noexcept {
133 GUM_OP_CPY(MultiDimDecorator);
136 empty_value_ = from.empty_value_;
137 if (content_ == nullptr)
138 content_ = static_cast< MultiDimImplementation< GUM_SCALAR >* >(from.content()->newFactory());
139 MultiDimDecorator< GUM_SCALAR >::content()->copy(*from.content());
140 return *this;
141 }
142
143 // destructor
144
145 template < typename GUM_SCALAR >
147 if (content_ != nullptr) { delete (content_); }
148
149 GUM_DESTRUCTOR(MultiDimDecorator);
150 }
151
152 // return a data, given a Instantiation - final method
153
154 template < typename GUM_SCALAR >
155 INLINE GUM_SCALAR& MultiDimDecorator< GUM_SCALAR >::get_(const Instantiation& i) const {
156 GUM_ERROR(OperationNotAllowed, "_get in the implementation !")
157 }
158
159 template < typename GUM_SCALAR >
160 INLINE GUM_SCALAR MultiDimDecorator< GUM_SCALAR >::get(const Instantiation& i) const {
161 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
162 return empty_value_;
163 } else {
164 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->get(i);
165 }
166 }
167
168 template < typename GUM_SCALAR >
170 const GUM_SCALAR& value) const {
171 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim() == 0) {
172 empty_value_ = value;
173 } else {
174 static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->set(i, value);
175 }
176 }
177
178 // get the size of domains - final method
179
180 template < typename GUM_SCALAR >
184
185 // add a new var to the sequence of vars - final method
186
187 template < typename GUM_SCALAR >
189 if (v.domainSize() < 1) {
190 GUM_ERROR(InvalidArgument, "Empty variable " << v << " cannot be added in a Tensor")
191 }
192 static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->add(v);
193 }
194
195 // listen to change in each recorded Instantiation. final method
196
197 template < typename GUM_SCALAR >
199 const DiscreteVariable* const var,
200 Idx oldval,
201 Idx newval) {
203 var,
204 oldval,
205 newval);
206 }
207
208 // listen to an assignment of a value in a Instantiation
209
210 template < typename GUM_SCALAR >
214
215 // listen to setFirst in each recorded Instantiation. final method.
216
217 template < typename GUM_SCALAR >
221
222 // listen to setLast in each recorded Instantiation. final method.
223
224 template < typename GUM_SCALAR >
228
229 // listen to increment in each recorded Instantiation. final method.
230
231 template < typename GUM_SCALAR >
235
236 // listen to increment in each recorded Instantiation. final method.
237
238 template < typename GUM_SCALAR >
242
243 // add a Instantiation as a slave of this
244
245 template < typename GUM_SCALAR >
249
250 template < typename GUM_SCALAR >
254
255 template < typename GUM_SCALAR >
256 INLINE void MultiDimDecorator< GUM_SCALAR >::erase(const std::string& name) {
257 erase(variable(name));
258 }
259
260 template < typename GUM_SCALAR >
264
265 template < typename GUM_SCALAR >
266 INLINE const DiscreteVariable&
267 MultiDimDecorator< GUM_SCALAR >::variable(const std::string& name) const {
268 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->variable(name);
269 }
270
271 template < typename GUM_SCALAR >
273 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->pos(var);
274 }
275
276 template < typename GUM_SCALAR >
278 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->contains(var);
279 }
280
281 template < typename GUM_SCALAR >
283 if (content_ == nullptr) return true;
284 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty();
285 }
286
287 template < typename GUM_SCALAR >
291
292 template < typename GUM_SCALAR >
293 INLINE void MultiDimDecorator< GUM_SCALAR >::fill(const GUM_SCALAR& d) const {
294 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
295 empty_value_ = d;
296 } else {
297 static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->fill(d);
298 }
299 }
300
301 // notification modification on vars to all Instantiation listeners.
302
303 template < typename GUM_SCALAR >
305 /*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
306 GUM_ERROR(OperationNotAllowed, "Not implemented yet")
307 }
308
309 // give a const ref to the sequence of DiscreteVariable*. final method.
310
311 template < typename GUM_SCALAR >
316
317 // get the nbr of vars in the sequence. final method
318
319 template < typename GUM_SCALAR >
321 return static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->nbrDim();
322 }
323
324 template < typename GUM_SCALAR >
325 void MultiDimDecorator< GUM_SCALAR >::populate(const std::vector< GUM_SCALAR >& v) const {
326 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
327 if (v.size() == 1) {
328 empty_value_ = v[0];
329 } else {
330 GUM_ERROR(SizeError, "Size do not match in populate")
331 }
332 } else {
333 content_->populate(v);
334 }
335 }
336
337 template < typename GUM_SCALAR >
338 void MultiDimDecorator< GUM_SCALAR >::apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const {
339 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
341 } else {
342 content_->apply(f);
343 }
344 }
345
346 template < typename GUM_SCALAR >
347 GUM_SCALAR
348 MultiDimDecorator< GUM_SCALAR >::reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f,
349 GUM_SCALAR base) const {
350 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
351 return base;
352 } else {
353 return content_->reduce(f, base);
354 }
355 }
356
357 // protected access to content_
358 template < typename GUM_SCALAR >
362
363 // protected access to content_
364 template < typename GUM_SCALAR >
369
370 template < typename GUM_SCALAR >
374
375 template < typename GUM_SCALAR >
379
380 template < typename GUM_SCALAR >
384
385 template < typename GUM_SCALAR >
387 MultiDimImplementation< GUM_SCALAR >* aContent) const {
388 if (aContent != nullptr) {
389 // TODO : frees all slave instantiations
390 // TODO : control the dimensions ?
392 content_ = aContent;
393 // registers all instantiations
394 delete (tmp);
395 }
396 }
397
398 // string representation of internal data about i in this.
399 template < typename GUM_SCALAR >
400 INLINE std::string MultiDimDecorator< GUM_SCALAR >::toString(const Instantiation* i) const {
401 return content_->toString(i);
402 }
403
404 template < typename GUM_SCALAR >
406 const DiscreteVariable* y) {
407 this->content()->replace(*x, *y);
408 }
409
410 template < typename GUM_SCALAR >
411 INLINE std::string MultiDimDecorator< GUM_SCALAR >::toString() const {
412 if (static_cast< MultiDimContainer< GUM_SCALAR >* >(content_)->empty()) {
413 std::stringstream ss;
414 ss << "<> :: " << empty_value_;
415 return ss.str();
416 } else {
417 return content_->toString();
418 }
419 }
420
421 //@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
422} /* namespace gum */
Base class for discrete random variable.
virtual Size domainSize() const =0
Class for assigning/browsing values to tuples of discrete variables.
Exception: at least one argument passed to a function is not what was expected.
MultiDimContainer & operator=(const MultiDimContainer< GUM_SCALAR > &src)
Default constructor.
MultiDimContainer()
Default constructor.
Decorator design pattern in order to separate implementations from multidimensional matrix concepts.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
virtual void setLastNotification(const Instantiation &i) final
Listen to setLast in a given Instantiation.
virtual void setDecNotification(const Instantiation &i) final
Listen to increment in each recorded Instantiation.
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
MultiDimDecorator< GUM_SCALAR > & operator=(const MultiDimDecorator &from) noexcept
copy operator
~MultiDimDecorator()
Class destructor.
virtual GUM_SCALAR reduce(std::function< GUM_SCALAR(GUM_SCALAR, GUM_SCALAR) > f, GUM_SCALAR base) const final
compute lfold for this container
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
virtual bool empty() const final
Returns true if no var is in *this.
virtual std::string toString() const
Default implementation of MultiDimContainer::set().
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
MultiDimDecorator(MultiDimImplementation< GUM_SCALAR > *aContent=nullptr, GUM_SCALAR empty_value=(GUM_SCALAR) 0)
Class constructor.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
virtual void populate(const std::vector< GUM_SCALAR > &v) const final
Automatically fills this MultiDimContainer with the values in v.
virtual bool unregisterSlave(Instantiation &i) final
Unregister i as a slave of this MultiDimAdressable.
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
virtual bool registerSlave(Instantiation &i) final
Register i as a slave of this MultiDimAdressable.
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
virtual void setFirstNotification(const Instantiation &i) final
Listen to setFirst in a given Instantiation.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) final
Listen to changes in a given Instantiation.
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
void swapContent_(MultiDimImplementation< GUM_SCALAR > *aContent) const
protected method to swap the implementation behind the Tensor
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual void fill(const GUM_SCALAR &d) const final
Default implementation of MultiDimContainer::set().
virtual void setChangeNotification(const Instantiation &i) final
Listen to an assignment of a value in a Instantiation.
virtual Size domainSize() const final
Returns the product of the variables domain size.
virtual void notifyChange() const final
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
virtual MultiDimDecorator< GUM_SCALAR > * newFactory() const =0
Default implementation of MultiDimContainer::set().
virtual void setIncNotification(const Instantiation &i) final
Listen to increment in a given Instantiation.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
GUM_SCALAR & get_(const Instantiation &i) const final
Return a data, given a Instantiation - final method.
<agrum/base/multidim/multiDimImplementation.h>
Exception : operation not allowed.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Exception : problem with size.
Efficient functionals for projecting multidim tables over all their variables.
#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
Size Idx
Type for indexes.
Definition types.h:79
Headers for MultiDimDecorator.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void _initTensorOperators__()
Efficient functionals for combining multiDims.
Headers for partial instantiation functions.
Efficient functionals for projecting multiDimensional tables.
a class used to register complete projections over non-pointers types
a class used to register operators over non-pointers types
A class used to register instantiation functions over non-pointers types.
void init()
Initialize the partial instantiation functions.
a class used to register projections over non-pointers types