aGrUM 2.3.2
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-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
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_SCALAR >
90 class MultiDimBucket final: public MultiDimReadOnly< GUM_SCALAR > {
91 public:
92 // ========================================================================
94 // ========================================================================
96
105 explicit MultiDimBucket(Size bufferSize = INT_MAX);
106
112
116 virtual ~MultiDimBucket();
117
119 // ========================================================================
121 // ========================================================================
123
129 void add(const MultiDimContainer< GUM_SCALAR >& impl);
130
136 void add(const MultiDimContainer< GUM_SCALAR >* impl);
137
142 void erase(const MultiDimContainer< GUM_SCALAR >& impl);
143
148 void erase(const MultiDimContainer< GUM_SCALAR >* impl);
149
155 bool contains(const MultiDimContainer< GUM_SCALAR >& impl) const;
156
163 const gum::VariableSet& allVariables() const;
164
169 Size bucketSize() const;
170
175 bool isBucketEmpty() const;
176
183 bool bucketChanged() const;
184
190 const MultiDimArray< GUM_SCALAR >& bucket() const;
191
196 Size bufferSize() const;
197
207 void setBufferSize(Size amount);
208
228 void compute(bool force = false) const;
229
231 // ========================================================================
233 // ========================================================================
235
236 virtual MultiDimContainer< GUM_SCALAR >* newFactory() const override;
237
238 const std::string& name() const override;
239
240 virtual void add(const DiscreteVariable& v) override;
241
242 virtual void erase(const DiscreteVariable& v) override;
243
244 virtual Size realSize() const override;
245
246 bool contains(const DiscreteVariable& v) const override;
247
248 virtual GUM_SCALAR get(const Instantiation& i) const override;
249
250 virtual void changeNotification(const Instantiation& i,
251 const DiscreteVariable* const var,
252 Idx oldval,
253 Idx newval) override;
254
255 virtual void setFirstNotification(const Instantiation& i) override;
256
257 virtual void setLastNotification(const Instantiation& i) override;
258
259 virtual void setIncNotification(const Instantiation& i) override;
260
261 virtual void setDecNotification(const Instantiation& i) override;
262
263 virtual void setChangeNotification(const Instantiation& i) override;
264
265 virtual bool registerSlave(Instantiation& i) override;
266
267 virtual bool unregisterSlave(Instantiation& i) override;
268
269 virtual MultiDimAdressable& getMasterRef() override;
270
271 virtual const MultiDimAdressable& getMasterRef() const override;
272
273 virtual std::string toString(const Instantiation* i) const override;
274
276
282
283 protected:
284 virtual void commitMultipleChanges_() override;
285
290 virtual GUM_SCALAR& get_(const Instantiation& i) const override;
291
292 virtual 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_SCALAR _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 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:1594
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:637
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.
A multidim implementation for buckets.
HashTable< const Instantiation *, GUM_SCALAR > _slavesValue_
This table is used to keep the last value computed for an instantiation when the value are computed o...
bool bucketChanged() const
Returns true if the bucket need re-computation since the last computation.
bool isBucketEmpty() const
Returns true if this bucket is empty.
virtual void setChangeNotification(const Instantiation &i) override
Listen to an assignment of a value in a Instantiation.
virtual GUM_SCALAR get(const Instantiation &i) const override
Returns the value pointed by i.
Size bucketSize() const
Returns the number of MultiDimContainer in in this bukcet.
virtual void setDecNotification(const Instantiation &i) override
Listen to increment in each recorded Instantiation.
Instantiation _allVarsInst_
Instantiation over all variable in this.
MultiDimArray< GUM_SCALAR > * _bucket_
The result table of this bucket.
virtual Size realSize() const override
Returns the real number of parameters used for this table.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const override
Default constructor.
virtual bool unregisterSlave(Instantiation &i) override
Unregister i as a slave of this MultiDimAdressable.
virtual void commitMultipleChanges_() override
Synchronize content after MultipleChanges.
const gum::VariableSet & allVariables() const
Returns the sequence of all the variables contained in the bucket.
void _initializeBuffer_()
Initialize the internal buffer.
Size _bufferSize_
The number of element allowed in bucket.
const MultiDimArray< GUM_SCALAR > & bucket() const
Returns the MultiDimArray used by this MultiDimBucket.
void setBufferSize(Size amount)
Changes the amount of memory allowed for this bucket.
Bijection< Instantiation *, Instantiation * > _instantiations_
Bijection between instantiations registered on this and their equivalent on bucket.
void _eraseVariable_(const DiscreteVariable *var)
Erase a variable from allVariables if no other multidimensional table uses it in this bucket.
GUM_SCALAR _computeValue_(const Instantiation &value) const
Compute the value of the final table of this bucket given i.
virtual MultiDimAdressable & getMasterRef() override
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
bool contains(const MultiDimContainer< GUM_SCALAR > &impl) const
Returns true if the MultiDimContainer is in this bucket.
void _eraseBuffer_()
Clean the buffer and switch it's instantiation to this bucket.
virtual ~MultiDimBucket()
Destructor.
bool _changed_
Flag used to know if changes has occurred in the bucket since last computation.
virtual bool registerSlave(Instantiation &i) override
Register i as a slave of this MultiDimAdressable.
void erase(const MultiDimContainer< GUM_SCALAR > &impl)
Remove a MultiDimContainer from this bucket.
const HashTable< const MultiDimContainer< GUM_SCALAR > *, Instantiation * > & multidims() const
Returns the MultiDimContainer and their respective Instantiation.
void add(const MultiDimContainer< GUM_SCALAR > &impl)
Add a MultiDimContainer in the bucket.
virtual void setLastNotification(const Instantiation &i) override
Listen to setLast in a given Instantiation.
const std::string & name() const override
Returns the real name of the multiDim implementation.
virtual void setFirstNotification(const Instantiation &i) override
Listen to setFirst in a given Instantiation.
std::string _name_
The class name.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
Size bufferSize() const
Returns the amount of memory allowed for this bucket.
HashTable< const MultiDimContainer< GUM_SCALAR > *, Instantiation * > _multiDims_
The set of MultiDimContainer in this bucket.
void _addVariable_(const DiscreteVariable *var)
Add a variable to allVariables, and do nothing if var is already in the set.
void compute(bool force=false) const
This method computes the final table of this bucket.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) override
Listen to changes in a given Instantiation.
MultiDimBucket(Size bufferSize=INT_MAX)
Default constructor.
virtual void setIncNotification(const Instantiation &i) override
Listen to increment in a given Instantiation.
virtual GUM_SCALAR & get_(const Instantiation &i) const override
gum::VariableSet _allVariables_
The set of all variables of the multidims in this bucket.
Abstract base class for all multi dimensionnal containers.
virtual std::string toString() const
Returns a representation of this MultiDimContainer.
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