aGrUM 3.1.1
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-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
43
50
51#include <mutex>
52
53// include the operators that will be used by the decorators
59
60namespace gum {
61 // instrumental and non-API function
62 template < typename GUM_ELEMENT >
64 // ensure that only one thread will register the projections
65 static std::once_flag first;
66 std::call_once(first, []() {
67 // register the operators that will be used by the decorator
69 op.init();
70
71 // register the projectors that will be used by the decorator
73 proj.init();
74
75 // register the projectors that will be used by the decorator
77 comp_proj.init();
78
79 // register the partial instantiators that will be used by the decorator
81 inst.init();
82 });
83 }
84
85 // constructors
86 template < typename GUM_ELEMENT >
93
94 template < typename GUM_ELEMENT >
102
103 template < typename GUM_ELEMENT >
107 std::forward< MultiDimContainer< GUM_ELEMENT > >(from));
108 GUM_OP_MOV(MultiDimDecorator);
109
110 if (this != &from) {
111 if (content_ != nullptr) delete (content_); // should be the case
112 empty_value_ = from.empty_value_;
113 content_ = from.content_;
114 from.content_ = nullptr;
115 }
116
117 return *this;
119
120 template < typename GUM_ELEMENT >
122 MultiDimDecorator< GUM_ELEMENT >&& from) noexcept :
124 GUM_CONS_MOV(MultiDimDecorator);
125
126 empty_value_ = from.empty_value_;
127 content_ = from.content_;
128 from.content_ = nullptr;
129 }
131 template < typename GUM_ELEMENT >
133 const MultiDimDecorator< GUM_ELEMENT >& from) noexcept {
134 GUM_OP_CPY(MultiDimDecorator);
137 empty_value_ = from.empty_value_;
138 if (content_ == nullptr)
140 = static_cast< MultiDimImplementation< GUM_ELEMENT >* >(from.content()->newFactory());
142 return *this;
144
145 // destructor
146
147 template < typename GUM_ELEMENT >
149 if (content_ != nullptr) { delete (content_); }
150
151 GUM_DESTRUCTOR(MultiDimDecorator);
152 }
154 // return a data, given a Instantiation - final method
156 template < typename GUM_ELEMENT >
158 GUM_ERROR(OperationNotAllowed, "_get in the implementation !")
159 }
161 template < typename GUM_ELEMENT >
163 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
165 } else {
166 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->get(i);
167 }
169
170 template < typename GUM_ELEMENT >
172 const GUM_ELEMENT& value) const {
173 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->nbrDim() == 0) {
174 empty_value_ = value;
175 } else {
176 static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->set(i, value);
177 }
178 }
179
180 // get the size of domains - final method
181
182 template < typename GUM_ELEMENT >
186
187 // add a new var to the sequence of vars - final method
188
189 template < typename GUM_ELEMENT >
191 if (v.domainSize() < 1) {
192 GUM_ERROR(InvalidArgument, "Empty variable " << v << " cannot be added in a Tensor")
193 }
194 static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->add(v);
195 }
196
197 // listen to change in each recorded Instantiation. final method
198
199 template < typename GUM_ELEMENT >
201 const DiscreteVariable* const var,
202 Idx oldval,
203 Idx newval) {
205 var,
206 oldval,
207 newval);
208 }
209
210 // listen to an assignment of a value in a Instantiation
211
212 template < typename GUM_ELEMENT >
216
217 // listen to setFirst in each recorded Instantiation. final method.
219 template < typename GUM_ELEMENT >
223
224 // listen to setLast in each recorded Instantiation. final method.
226 template < typename GUM_ELEMENT >
231 // listen to increment in each recorded Instantiation. final method.
233 template < typename GUM_ELEMENT >
237
238 // listen to increment in each recorded Instantiation. final method.
239
240 template < typename GUM_ELEMENT >
244
245 // add a Instantiation as a slave of this
246
247 template < typename GUM_ELEMENT >
251
252 template < typename GUM_ELEMENT >
256
257 template < typename GUM_ELEMENT >
258 void MultiDimDecorator< GUM_ELEMENT >::erase(std::string_view name) {
259 erase(variable(name));
261
262 template < typename GUM_ELEMENT >
267 template < typename GUM_ELEMENT >
269 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->variable(name);
270 }
271
272 template < typename GUM_ELEMENT >
276
277 template < typename GUM_ELEMENT >
280 }
281
282 template < typename GUM_ELEMENT >
284 if (content_ == nullptr) return true;
285 return static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty();
286 }
287
288 template < typename GUM_ELEMENT >
292
293 template < typename GUM_ELEMENT >
294 void MultiDimDecorator< GUM_ELEMENT >::fill(const GUM_ELEMENT& d) const {
295 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
296 empty_value_ = d;
297 } else {
299 }
300 }
301
302 // notification modification on vars to all Instantiation listeners.
303
304 template < typename GUM_ELEMENT >
306 /*( (MultiDimContainer<GUM_ELEMENT> *) content_)->notifyChange();*/
307 GUM_ERROR(OperationNotAllowed, "Not implemented yet")
308 }
309
310 // give a const ref to the sequence of DiscreteVariable*. final method.
311
312 template < typename GUM_ELEMENT >
317
318 // get the nbr of vars in the sequence. final method
319
320 template < typename GUM_ELEMENT >
324
325 template < typename GUM_ELEMENT >
326 void MultiDimDecorator< GUM_ELEMENT >::populate(const std::vector< GUM_ELEMENT >& v) const {
327 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
328 if (v.size() == 1) {
329 empty_value_ = v[0];
330 } else {
331 GUM_ERROR(SizeError, "Size do not match in populate")
332 }
333 } else {
334 content_->populate(v);
335 }
336 }
337
338 template < typename GUM_ELEMENT >
339 void MultiDimDecorator< GUM_ELEMENT >::apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const {
340 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
342 } else {
343 content_->apply(f);
344 }
345 }
346
347 template < typename GUM_ELEMENT >
349 std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f,
350 GUM_ELEMENT base) const {
351 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(content_)->empty()) {
352 return base;
353 } else {
354 return content_->reduce(f, base);
355 }
356 }
357
358 // protected access to content_
359 template < typename GUM_ELEMENT >
363
364 // protected access to content_
365 template < typename GUM_ELEMENT >
369
370 template < typename GUM_ELEMENT >
374
375 template < typename GUM_ELEMENT >
379
380 template < typename GUM_ELEMENT >
384
385 template < typename GUM_ELEMENT >
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_ELEMENT >
401 return content_->toString(i);
402 }
403
404 template < typename GUM_ELEMENT >
406 const DiscreteVariable* y) {
407 this->content()->replace(*x, *y);
408 }
409
410 template < typename GUM_ELEMENT >
412 if (static_cast< MultiDimContainer< GUM_ELEMENT >* >(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_ELEMENT 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.
Abstract base class for all multi dimensionnal containers.
MultiDimContainer & operator=(const MultiDimContainer< GUM_ELEMENT > &src)
Default constructor.
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_SCALAR > * newFactory() const override=0
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.
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.
std::string toString() const override
Default implementation of MultiDimContainer::set().
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>
Exception : operation not allowed.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Exception : problem with size.
Efficient functionals for projecting multidim tables over all their variables.
#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
Size Idx
Type for indexes.
Definition types.h:79
bool contains(std::string_view s, std::string_view needle)
true if needle in s
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