aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
multiDimBucket.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
49
50#ifndef GUM_MULTIDIM_BUCKET_H
51#define GUM_MULTIDIM_BUCKET_H
52
53#include <climits>
54#include <sstream>
55
59
60namespace gum {
61
88
89 template < typename GUM_ELEMENT >
90 class MultiDimBucket final: public MultiDimReadOnly< GUM_ELEMENT > {
91 public:
92 // ========================================================================
94 // ========================================================================
96
105 explicit MultiDimBucket(Size bufferSize = INT_MAX);
106
112
116 ~MultiDimBucket() override;
117
119 // ========================================================================
121 // ========================================================================
123
130
137
143
149
156
163 const gum::VariableSet& allVariables() const;
164
169 Size bucketSize() const;
170
175 bool isBucketEmpty() const;
176
183 bool bucketChanged() const;
184
191
196 Size bufferSize() const;
197
207 void setBufferSize(Size amount);
208
228 void compute(bool force = false) const;
229
231 // ========================================================================
233 // ========================================================================
235
236 [[nodiscard]] MultiDimContainer< GUM_ELEMENT >* newFactory() const override;
237
238 const std::string& name() const override;
239
240 void add(const DiscreteVariable& v) override;
241
242 void erase(const DiscreteVariable& v) override;
243
244 Size realSize() const override;
245
246 bool contains(const DiscreteVariable& v) const override;
247
248 GUM_ELEMENT get(const Instantiation& i) const override;
249
250 void changeNotification(const Instantiation& i,
251 const DiscreteVariable* const var,
252 Idx oldval,
253 Idx newval) override;
254
255 void setFirstNotification(const Instantiation& i) override;
256
257 void setLastNotification(const Instantiation& i) override;
258
259 void setIncNotification(const Instantiation& i) override;
260
261 void setDecNotification(const Instantiation& i) override;
262
263 void setChangeNotification(const Instantiation& i) override;
264
265 bool registerSlave(Instantiation& i) override;
266
267 bool unregisterSlave(Instantiation& i) override;
268
270
271 const MultiDimAdressable& getMasterRef() const override;
272
273 std::string toString(const Instantiation* i) const override;
274
276
282
283 protected:
284 void commitMultipleChanges_() override;
285
290 GUM_ELEMENT& get_(const Instantiation& i) const override;
291
292 void replace_(const DiscreteVariable* x, const DiscreteVariable* y) override;
293
294 private:
297
301
304
307
310
313
320 void _addVariable_(const DiscreteVariable* var);
321
328 void _eraseVariable_(const DiscreteVariable* var);
329
335 void _initializeBuffer_();
336
340 void _eraseBuffer_();
341
351 GUM_ELEMENT _computeValue_(const Instantiation& value) const;
352
355 mutable bool _changed_;
356
360
362 std::string _name_;
363 };
364
365
366#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
367 extern template class GUM_SHARED_PUBLIC MultiDimBucket< double >;
368#endif
369
370
371} /* namespace gum */
372
374
375#endif /* GUM_MULTIDIM_BUCKET_H */
Set of pairs of elements with fast search for both elements.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:640
Class for assigning/browsing values to tuples of discrete variables.
Abstract base class for all multi dimensionnal addressable.
Multidimensional matrix stored as an array in memory.
Bijection< Instantiation *, Instantiation * > _instantiations_
Bijection between instantiations registered on this and their equivalent on bucket.
GUM_ELEMENT get(const Instantiation &i) const override
Returns the value pointed by i.
bool isBucketEmpty() const
Returns true if this bucket is empty.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.
void setFirstNotification(const Instantiation &i) override
Listen to setFirst in a given Instantiation.
void setLastNotification(const Instantiation &i) override
Listen to setLast in a given Instantiation.
const gum::VariableSet & allVariables() const
Returns the sequence of all the variables contained in the bucket.
void erase(const MultiDimContainer< GUM_ELEMENT > &impl)
Remove a MultiDimContainer from this bucket.
void commitMultipleChanges_() override
Synchronize content after MultipleChanges.
gum::VariableSet _allVariables_
The set of all variables of the multidims in this bucket.
void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) override
Listen to changes in a given Instantiation.
Size bufferSize() const
Returns the amount of memory allowed for this bucket.
MultiDimBucket(const MultiDimBucket< GUM_ELEMENT > &source)
Copy constructor.
void _initializeBuffer_()
Initialize the internal buffer.
Instantiation _allVarsInst_
Instantiation over all variable in this.
void setChangeNotification(const Instantiation &i) override
Listen to an assignment of a value in a Instantiation.
void _eraseVariable_(const DiscreteVariable *var)
Erase a variable from allVariables if no other multidimensional table uses it in this bucket.
std::string toString(const Instantiation *i) const override
Display the internal representation of i.
GUM_ELEMENT _computeValue_(const Instantiation &value) const
Compute the value of the final table of this bucket given i.
void setIncNotification(const Instantiation &i) override
Listen to increment in a given Instantiation.
const HashTable< const MultiDimContainer< GUM_ELEMENT > *, Instantiation * > & multidims() const
Returns the MultiDimContainer and their respective Instantiation.
void _eraseBuffer_()
Clean the buffer and switch it's instantiation to this bucket.
void setBufferSize(Size amount)
Changes the amount of memory allowed for this bucket.
MultiDimArray< GUM_ELEMENT > * _bucket_
The result table of this bucket.
const MultiDimArray< GUM_ELEMENT > & bucket() const
Returns the MultiDimArray used by this MultiDimBucket.
Size _bufferSize_
The number of element allowed in bucket.
void add(const MultiDimContainer< GUM_ELEMENT > *impl)
Add a MultiDimContainer in the bucket.
MultiDimContainer< GUM_ELEMENT > * newFactory() const override
Default constructor.
const std::string & name() const override
Returns the real name of the multiDim implementation.
bool bucketChanged() const
Returns true if the bucket need re-computation since the last computation.
HashTable< const MultiDimContainer< GUM_ELEMENT > *, Instantiation * > _multiDims_
The set of MultiDimContainer in this bucket.
void add(const MultiDimContainer< GUM_ELEMENT > &impl)
Add a MultiDimContainer in the bucket.
MultiDimAdressable & getMasterRef() override
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
bool _changed_
Flag used to know if changes has occurred in the bucket since last computation.
GUM_ELEMENT & get_(const Instantiation &i) const override
Size bucketSize() const
Returns the number of MultiDimContainer in this bucket.
void erase(const MultiDimContainer< GUM_ELEMENT > *impl)
Remove a MultiDimContainer from this bucket.
bool registerSlave(Instantiation &i) override
Register i as a slave of this MultiDimAdressable.
void _addVariable_(const DiscreteVariable *var)
Add a variable to allVariables, and do nothing if var is already in the set.
bool unregisterSlave(Instantiation &i) override
Unregister i as a slave of this MultiDimAdressable.
void setDecNotification(const Instantiation &i) override
Listen to increment in each recorded Instantiation.
void compute(bool force=false) const
This method computes the final table of this bucket.
Size realSize() const override
Returns the real number of parameters used for this table.
MultiDimBucket(Size bufferSize=INT_MAX)
Default constructor.
std::string _name_
The class name.
bool contains(const MultiDimContainer< GUM_ELEMENT > &impl) const
Returns true if the MultiDimContainer is in this bucket.
HashTable< const Instantiation *, GUM_ELEMENT > _slavesValue_
This table is used to keep the last value computed for an instantiation when the value are computed o...
Abstract base class for all multi dimensionnal containers.
MultiDimReadOnly()
Default constructor.
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.
Implementation of the MultiDimBucket class.
Header of the MultiDimReadOnly class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
template class GUM_SHARED_PUBLIC MultiDimBucket< double >