aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimImplementation_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// to ease IDE parser
52
53namespace gum {
54
55 // Default constructor
56
57 template < typename GUM_SCALAR >
65
66 // Copy constructor
67
68 template < typename GUM_SCALAR >
77
78 // destructor
79
80 template < typename GUM_SCALAR >
82 GUM_DESTRUCTOR(MultiDimImplementation);
83 // unregister all remaining slave instantiations
84
86 iter != _slaveInstantiations_.endSafe();
87 ++iter)
88 (*iter)->forgetMaster();
89 }
90
91 // add a new var to the sequence of _vars_.
92
93 template < typename GUM_SCALAR >
95 // check if the variable already belongs to the tuple of variables
96 // of the Instantiation
97 if (_vars_.exists(&v)) {
98 GUM_ERROR(DuplicateElement, "Var " << v.name() << " already exists in this instantiation")
99 }
100 for (const auto& w: _vars_) {
101 if (w->name() == v.name())
103 "A var with name '" << v.name() << "' already exists in this instantiation");
104 }
105
107
108 _vars_.insert(&v);
109
110 // informs all the slaves that they have to update themselves
112 iter != _slaveInstantiations_.endSafe();
113 ++iter) {
114 (*iter)->addWithMaster(this, v);
115 }
116
117 if (isInMultipleChangeMethod_()) _setNotCommitedChange_();
118 }
119
120 // removes a var from the variables of the multidimensional matrix
121
122 template < typename GUM_SCALAR >
124 // check that the variable does actually belong to the
125 // MultiDimImplementation
126 if (!_vars_.exists(&v)) { GUM_ERROR(NotFound, "Var does not exist in this implementation") }
127
129
130 _vars_.erase(&v);
131
132 // informs all the slaves that they have to update themselves
134 iter != _slaveInstantiations_.endSafe();
135 ++iter) {
136 (*iter)->eraseWithMaster(this, v);
137 }
138
139 if (isInMultipleChangeMethod_()) _setNotCommitedChange_();
140 }
141
142 // adds a new var to the sequence of _vars_
143 template < typename GUM_SCALAR >
146 array.add(v);
147 return array;
148 }
149
150 // add a Instantiation to the list of slave instantiations
151
152 template < typename GUM_SCALAR >
154 // check that the Instantiation has the same variables as this
155 if (slave.nbrDim() != _vars_.size()) return false;
156
158 iter != _vars_.endSafe();
159 ++iter)
160 if (!slave.contains(*iter)) return false;
161
163
164 _slaveInstantiations_ += (&slave);
165
166 return true;
167 }
168
169 // removes a Instantiation from the list of slave instantiations
170
171 template < typename GUM_SCALAR >
173 _slaveInstantiations_.eraseByVal(&slave);
174 return true;
176
177 template < typename GUM_SCALAR >
179 return _vars_.size();
181
182 template < typename GUM_SCALAR >
185 }
187 template < typename GUM_SCALAR >
189 return *(_vars_.atPos(i));
190 }
191
192 template < typename GUM_SCALAR >
193 INLINE const DiscreteVariable&
195 for (const auto& v: _vars_) {
196 if (v->name() == name) return *v;
197 }
198
199 GUM_ERROR(NotFound, "'" << name << "' can not be found in the multidim structure.")
200 }
201
202 template < typename GUM_SCALAR >
204 return _vars_.pos(&v);
205 }
206
207 template < typename GUM_SCALAR >
209 return _vars_.exists(&v);
211
212 // returns a const ref to the sequence of DiscreteVariable*
213
214 template < typename GUM_SCALAR >
219
220 // is this empty ?
221 template < typename GUM_SCALAR >
223 GUM_ASSERT(!this->isCommitNeeded_());
224 return _vars_.empty();
226
227 template < typename GUM_SCALAR >
232 template < typename GUM_SCALAR >
238
239 _internalChangeMethod_ = _InternalChangeMethod_::DIRECT_CHANGE;
240 }
241
242 template < typename GUM_SCALAR >
252 template < typename GUM_SCALAR >
256
257 template < typename GUM_SCALAR >
259 // empty!
260 }
261
262 // get the actual change method of *this
263 template < typename GUM_SCALAR >
267
268 // get the actual state of *this
269 template < typename GUM_SCALAR >
274 // Returns a constant reference over the list of slaved instantiations.
275 template < typename GUM_SCALAR >
279
280 // get the actual state of *this
281 template < typename GUM_SCALAR >
286 // get the actual state of *this
287 template < typename GUM_SCALAR >
289 return ((float)1) - (float)realSize() / (float)domainSize();
290 }
291
292 // returns a basename to be used for default operators
293 template < typename GUM_SCALAR >
295 static const std::string str = "MultiDimImplementation";
296 return str;
297 }
298
299 template < typename GUM_SCALAR >
301 const DiscreteVariable* y) {
302 _vars_.setAtPos(_vars_.pos(x), y);
303
305 iter != _slaveInstantiations_.endSafe();
306 ++iter) {
307 (**iter).replace(*x, *y);
308 }
309 }
310
311 template < typename GUM_SCALAR >
313 _vars_.swap(p1, p2);
314 }
315
316 // for friendly displaying the content of the array
317 template < typename GUM_SCALAR >
318 INLINE std::ostream& operator<<(std::ostream& out,
320 return out << static_cast< const MultiDimContainer< GUM_SCALAR >& >(array);
321 }
322
323 // protected access to content_
324 template < typename GUM_SCALAR >
328
329 // protected access to content_
330 template < typename GUM_SCALAR >
333 return this;
334 }
335} /* namespace gum */
Base class for discrete random variable.
virtual Size domainSize() const =0
Exception : a similar element already exists.
Class for assigning/browsing values to tuples of discrete variables.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Generic doubly linked lists.
Definition list.h:379
ListIteratorSafe< Val > iterator_safe
Types for STL compliance.
Definition list.h:392
MultiDimContainer()
Default constructor.
<agrum/base/multidim/multiDimImplementation.h>
virtual bool empty() const override
Returns true if no var is in *this.
virtual const std::string & name() const=0
void _setNotCommitedChange_()
Change the internalChangeState to NOT_COMMITTED_CHANGE.
virtual void invert_(Idx p1, Idx p2)
Inverts variables at position p1 and p2.
virtual ~MultiDimImplementation()
Class destructor.
const std::string & basename() const
Returns the base class name of this MultiDimImplementation.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
virtual Size realSize() const =0
Returns the real number of parameters used for this table.
MultiDimImplementation()
Default constructor.
Sequence< const DiscreteVariable * > _vars_
List of discrete variables (dimensions).
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
_InternalChangeState_ _internalChangeState_
The current change state.
virtual void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
virtual void commitMultipleChanges_()
Synchronize content after MultipleChanges.
float compressionRate() const
The compression ratio of the table (depending on the type of implementation).
const List< Instantiation * > & slaves_() const
Returns a constant reference over the list of slaved instantiations.
bool isCommitNeeded_() const
Get the actual state of *this.
virtual Size domainSize() const override
Returns the product of the variables domain size.
List< Instantiation * > _slaveInstantiations_
List of instantiations of the tuples (sequences) of variables.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
virtual void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
_InternalChangeMethod_ _internalChangeMethod_
The current change method.
virtual bool contains(const DiscreteVariable &v) const override
Returns true if var is in *this.
virtual bool registerSlave(Instantiation &slave) override
Register i as a slave of this MultiDimAdressable.
void endMultipleChanges() override
Call this method after doing important changes in this MultiDimContainer.
Size _domainSize_
This MultiDimImplementation domain size.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual bool unregisterSlave(Instantiation &slave) override
Unregister i as a slave of this MultiDimAdressable.
virtual Idx pos(const DiscreteVariable &v) const override
Returns the index of a variable.
void beginMultipleChanges() override
Call this method before doing important changes in this MultiDimContainer.
bool isInMultipleChangeMethod_() const
Get the actual change method of this MultiDimImplementation.
Exception : the element we looked for cannot be found.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition sequence.h:985
const std::string & name() const
returns the name of the variable
#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 of gum::MultiDimImplementation.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913