aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
instantiation.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
48#ifndef GUM_INSTANTIATION_H
49#define GUM_INSTANTIATION_H
50
51#include <ostream>
52#include <sstream>
53
54#include <agrum/agrum.h>
55
57
58namespace gum {
59
61
62 // =========================================================================
63 // === GUM_INSTANTIATION ===
64 // =========================================================================
101
102 class Instantiation final: public MultiDimInterface {
103 public:
104 // =========================================================================
106 // =========================================================================
108
113
127 Instantiation(const Instantiation& aI, const bool notifyMaster = true);
128
143
162
181
200
219
224
226 // =========================================================================
228 // =========================================================================
230
235 Idx nbrDim() const final;
236
259 void add(const DiscreteVariable& v) final;
260
274 void erase(const DiscreteVariable& v) final;
275 void erase(const std::string& name);
276
281 void clear();
282
289 Size domainSize() const final;
290
298 Idx pos(const DiscreteVariable& v) const final;
299
313 Idx val(Idx i) const;
314
328 Idx val(const DiscreteVariable& var) const;
329 Idx val(const std::string& name) const;
330
344 Idx valFromPtr(const DiscreteVariable* pvar) const;
345
353 const DiscreteVariable& variable(Idx i) const final;
354
363 const DiscreteVariable& variable(const std::string& name) const final;
364
384 Instantiation& chgVal(const DiscreteVariable& v, Idx newval);
385
406 Instantiation& chgVal(Idx varPos, Idx newval);
407
428 Instantiation& chgVal(const std::string& var, Idx newval);
429
450 Instantiation& chgVal(const std::string& var, const std::string& newval);
451
474
486 const Instantiation& external);
487
494 bool contains(const DiscreteVariable& v) const final;
495 bool contains(const std::string& name) const;
496
503 bool contains(const DiscreteVariable* v) const;
504
510
515 virtual bool empty() const final;
516
518 // =========================================================================
520 // =========================================================================
522
538 bool inOverflow() const;
539
576
577 void unsetOverflow();
578
583 void unsetEnd();
584
597 bool end() const;
598
611 bool rend() const;
612
614 // =========================================================================
616 // =========================================================================
618
639 void inc();
640
661 void dec();
662
683 void incIn(const Instantiation& i);
684
705 void decIn(const Instantiation& i);
706
727 void incOut(const Instantiation& i);
728
748 void decOut(const Instantiation& i);
749
765 void incNotVar(const DiscreteVariable& v);
766
784 void decNotVar(const DiscreteVariable& v);
785
801
802 void incVar(const DiscreteVariable& v);
803
819
820 void decVar(const DiscreteVariable& v);
821
823 // =========================================================================
825 // =========================================================================
827
835 void setFirst();
836
844 void setLast();
845
859 void setFirstIn(const Instantiation& i);
860
877 void setLastIn(const Instantiation& i);
878
892
893 void setFirstOut(const Instantiation& i);
894
908 void setLastOut(const Instantiation& i);
909
920
921 void setFirstNotVar(const DiscreteVariable& v);
922
933 void setLastNotVar(const DiscreteVariable& v);
934
945 void setFirstVar(const DiscreteVariable& v);
946
957 void setLastVar(const DiscreteVariable& v);
958
960 // =========================================================================
962 // =========================================================================
964
981
987 bool forgetMaster();
988
993 bool isSlave() const;
994
999 bool isMaster(const MultiDimAdressable* m) const;
1000
1005 bool isMaster(const MultiDimAdressable& m) const;
1006
1018
1027 void addWithMaster(const MultiDimAdressable* m, const DiscreteVariable& v);
1028
1037 void eraseWithMaster(const MultiDimAdressable* m, const DiscreteVariable& v);
1038
1040 // =========================================================================
1042 // =========================================================================
1044
1048 bool operator==(const Instantiation& other) const;
1049
1055
1061
1067
1073
1075 // =========================================================================
1077 // =========================================================================
1079
1084 Idx hamming() const;
1085
1090 std::string toString() const;
1091
1104
1111 void reorder(const Instantiation& i);
1112
1114
1115 protected:
1121 virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y) final;
1122
1123 private:
1127
1130
1132 std::vector< Idx > _vals_;
1133
1140
1146 void _swap_(Idx i, Idx j);
1147
1157 void _chgVal_(Idx varPos, Idx newVal);
1158
1181 void _add_(const DiscreteVariable& v);
1182
1199 void _erase_(const DiscreteVariable& v);
1200
1210
1215 void _init_(MultiDimAdressable* master);
1216
1228
1229 void _masterChangeNotification_(Idx varPos, Idx newVal, Idx oldVal) const;
1230 void _masterFirstNotification_() const;
1231 void _masterIncNotification_() const;
1232 void _masterLastNotification_() const;
1233 void _masterDecNotification_() const;
1234 };
1235
1239 std::ostream& operator<<(std::ostream&, const Instantiation&);
1240
1245 template <>
1246 // class HashFunc< Instantiation > : public HashFuncBase< Instantiation > {
1247 class HashFunc< Instantiation >: public HashFuncBase< Instantiation > {
1248 public:
1254 static Size castToSize(const Instantiation& key);
1255
1261 virtual Size operator()(const Instantiation& key) const override final;
1262 };
1263} /* namespace gum */
1264
1265#ifndef GUM_NO_INLINE
1267#endif /* GUM_NO_INLINE */
1268
1269#endif /* GUM_INSTANTIATION_H */
Base class for discrete random variable.
All hash functions should inherit from this class.
Definition hashFunc.h:169
static Size castToSize(const Instantiation &key)
Returns the value of a key as a Size.
virtual Size operator()(const Instantiation &key) const override final
Computes the hashed value of a key.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
The class for generic Hash Tables.
Definition hashTable.h:637
Class for assigning/browsing values to tuples of discrete variables.
const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
void setLastOut(const Instantiation &i)
Assign the last values in the Instantiation for the variables not in i.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
void decNotVar(const DiscreteVariable &v)
Operator decrement for vars which are not v.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
void decOut(const Instantiation &i)
Operator decrement for the variables not in i.
void clear()
Erase all variables from an Instantiation.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
void _masterIncNotification_() const
Instantiation & operator--()
Alias of Instantiation::dec().
Sequence< const DiscreteVariable * > _vars_
The tuple of variables to be instantiated.
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
bool actAsSlave(MultiDimAdressable &aMD)
Tries to register the Instantiation to a MultiDimAdressable.
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
void _masterFirstNotification_() const
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void decVar(const DiscreteVariable &v)
Operator decrement for variable v only.
void decIn(const Instantiation &i)
Operator decrement for the variables in i.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
Instantiation & operator-=(Size depl)
Calls depl times Instantiation::dec().
std::vector< Idx > _vals_
The current instantiation: the value of the tuple.
void setLastIn(const Instantiation &i)
Assign the last values in the Instantiation for the variables in i.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
virtual bool empty() const final
Returns true if the instantiation is empty.
bool _overflow_
Indicates whether the current value of the tuple is valid when we loop sufficiently over values of th...
void _reorder_(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
Idx pos(const DiscreteVariable &v) const final
Returns the position of the variable v.
Size domainSize() const final
Returns the product of the variable's domain size in the Instantiation.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
void _notifiedDimChanged_(const Sequence< const DiscreteVariable * > &v)
This function is called by the master (if any) when changes arise in its vars list.
Instantiation()
Default constructor: creates an empty tuple.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
bool rend() const
Returns true if the Instantiation reached the rend.
void inc()
Operator increment.
MultiDimAdressable * _master_
The master, if any, contains precisely the set of variables to be instantiated.
void dec()
Operator decrement.
void _init_(MultiDimAdressable *master)
Initialize this Instantiation.
~Instantiation()
Destructor.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
bool isSlave() const
Indicates whether the Instantiation has a master.
void addWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation:: add(const DiscreteVariable&) by master.
Idx hamming() const
Returns the hamming distance of this instantiation.
void setLastNotVar(const DiscreteVariable &v)
Assign the last values to variables different of v.
void _erase_(const DiscreteVariable &v)
Removes a variable from the sequence of vars.
void reorder(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
Instantiation & operator+=(Size depl)
Calls depl times Instantiation::inc().
void _masterLastNotification_() const
void _swap_(Idx i, Idx j)
Swap two variables in the Instantiation.
bool inOverflow() const
Indicates whether the current value of the tuple is correct or not.
void _add_(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
void _masterDecNotification_() const
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
void setValsFrom(const HashTable< const DiscreteVariable *, const DiscreteVariable * > &map, const Instantiation &external)
Assign the values of external in *this, using map as a bijection between external and this variables.
void _masterChangeNotification_(Idx varPos, Idx newVal, Idx oldVal) const
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
void _chgVal_(Idx varPos, Idx newVal)
Modifies internally the value of a given variable of the sequence.
void unsetEnd()
Alias for unsetOverflow().
std::string toString() const
Give a string version of instantiation.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Instantiation & operator++()
Alias of Instantiation::inc().
bool operator==(const Instantiation &other) const
operator==
void setLastVar(const DiscreteVariable &v)
Assign the last value in the Instantiation for var v.
bool forgetMaster()
Deassociate the master MultiDimAdressable, if any.
void unsetOverflow()
Removes the flag overflow.
void eraseWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation:: erase(const DiscreteVariable&) by master.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) final
Replace x by y.
Instantiation & operator=(const Instantiation &aI)
Copy operator.
void setLast()
Assign the last values in the Instantiation.
Abstract base class for all multi dimensionnal addressable.
Interface for all classes addressing in a multiDim fashion.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
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
Inline implemenation of gum::Instantiation.
Headers of MultiDimInterface.
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