aGrUM  0.14.2
instantiation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef GUM_INSTANTIATION_H
27 #define GUM_INSTANTIATION_H
28 
29 #include <ostream>
30 #include <sstream>
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/core/hashTable.h>
36 
37 namespace gum {
38 
39  class MultiDimAdressable;
40  // =========================================================================
41  // === GUM_INSTANTIATION ===
42  // =========================================================================
81  public:
82  // =========================================================================
84  // =========================================================================
86 
90  Instantiation();
91 
105  Instantiation(const Instantiation& aI, const bool notifyMaster = true);
106 
121 
140 
158  Instantiation(const MultiDimAdressable& aMD);
159 
178 
196  Instantiation(const MultiDimAdressable* aMD);
197 
201  ~Instantiation();
202 
204  // =========================================================================
206  // =========================================================================
208 
213  Idx nbrDim() const final;
214 
237  void add(const DiscreteVariable& v) final;
238 
252  void erase(const DiscreteVariable& v) final;
253 
258  void clear();
259 
266  Size domainSize() const final;
267 
275  Idx pos(const DiscreteVariable& v) const final;
276 
290  Idx val(Idx i) const;
291 
305  Idx val(const DiscreteVariable& var) const;
306 
320  Idx valFromPtr(const DiscreteVariable* pvar) const;
321 
329  const DiscreteVariable& variable(Idx i) const final;
330 
339  const DiscreteVariable& variable(const std::string& name) const final;
340 
360  Instantiation& chgVal(const DiscreteVariable& v, Idx newval);
361 
381  Instantiation& chgVal(const DiscreteVariable* v, Idx newval);
382 
403  Instantiation& chgVal(Idx varPos, Idx newval);
404 
425  Instantiation& chgVal(const std::string& var, Idx newval);
426 
447  Instantiation& chgVal(const std::string& var, const std::string& newval);
448 
471 
482  void setValsFrom(
484  const Instantiation& external);
485 
492  bool contains(const DiscreteVariable& v) const final;
493 
500  bool contains(const DiscreteVariable* v) const;
501 
507 
512  virtual bool empty() const final;
513 
515  // =========================================================================
517  // =========================================================================
519 
535  bool inOverflow() const;
536 
574  void unsetOverflow();
575 
580  void unsetEnd();
581 
594  bool end() const;
595 
608  bool rend() const;
609 
611  // =========================================================================
613  // =========================================================================
615 
636  void inc();
637 
658  void dec();
659 
680  void incIn(const Instantiation& i);
681 
702  void decIn(const Instantiation& i);
703 
724  void incOut(const Instantiation& i);
725 
745  void decOut(const Instantiation& i);
746 
762  void incNotVar(const DiscreteVariable& v);
763 
781  void decNotVar(const DiscreteVariable& v);
782 
799  void incVar(const DiscreteVariable& v);
800 
817  void decVar(const DiscreteVariable& v);
818 
820  // =========================================================================
822  // =========================================================================
824 
832  void setFirst();
833 
841  void setLast();
842 
856  void setFirstIn(const Instantiation& i);
857 
874  void setLastIn(const Instantiation& i);
875 
890  void setFirstOut(const Instantiation& i);
891 
905  void setLastOut(const Instantiation& i);
906 
918  void setFirstNotVar(const DiscreteVariable& v);
919 
930  void setLastNotVar(const DiscreteVariable& v);
931 
942  void setFirstVar(const DiscreteVariable& v);
943 
954  void setLastVar(const DiscreteVariable& v);
955 
957  // =========================================================================
959  // =========================================================================
961 
977  bool actAsSlave(MultiDimAdressable& aMD);
978 
984  bool forgetMaster();
985 
990  bool isSlave() const;
991 
996  bool isMaster(const MultiDimAdressable* m) const;
997 
1002  bool isMaster(const MultiDimAdressable& m) const;
1003 
1015 
1024  void addWithMaster(const MultiDimAdressable* m, const DiscreteVariable& v);
1025 
1034  void eraseWithMaster(const MultiDimAdressable* m, const DiscreteVariable& v);
1035 
1037  // =========================================================================
1039  // =========================================================================
1041 
1045  bool operator==(const Instantiation& other) const;
1046 
1052 
1058 
1063  Instantiation& operator+=(Size depl);
1064 
1069  Instantiation& operator-=(Size depl);
1070 
1072  // =========================================================================
1074  // =========================================================================
1076 
1081  Idx hamming() const;
1082 
1087  std::string toString() const;
1088 
1101 
1108  void reorder(const Instantiation& i);
1109 
1111 
1112  protected:
1118  virtual void _replace(const DiscreteVariable* x,
1119  const DiscreteVariable* y) final;
1120 
1121  private:
1125 
1128 
1130  std::vector< Idx > __vals;
1131 
1138 
1144  void __swap(Idx i, Idx j);
1145 
1155  void __chgVal(Idx varPos, Idx newVal);
1156 
1179  void __add(const DiscreteVariable& v);
1180 
1197  void __erase(const DiscreteVariable& v);
1198 
1208 
1213  void __init(MultiDimAdressable* master);
1214 
1226 
1227  void __masterChangeNotification(Idx varPos, Idx newVal, Idx oldVal) const;
1228  void __masterFirstNotification() const;
1229  void __masterIncNotification() const;
1230  void __masterLastNotification() const;
1231  void __masterDecNotification() const;
1232  };
1233 
1237  std::ostream& operator<<(std::ostream&, const Instantiation&);
1238 
1243  template <>
1244  // class HashFunc< Instantiation > : public HashFuncBase< Instantiation > {
1245  class HashFunc< Instantiation > : public HashFuncBase< Instantiation > {
1246  public:
1252  static Size castToSize(const Instantiation& key);
1253 
1259  virtual Size operator()(const Instantiation& key) const override final;
1260  };
1261 } /* namespace gum */
1262 
1263 #ifndef GUM_NO_INLINE
1265 #endif /* GUM_NO_INLINE */
1266 
1267 #endif /* GUM_INSTANTIATION_H */
void __erase(const DiscreteVariable &v)
Removes a variable from the sequence of vars.
void __init(MultiDimAdressable *master)
Initialize this Instantiation.
bool __overflow
Indicates whether the current value of the tuple is valid when we loop sufficiently over values of th...
void decIn(const Instantiation &i)
Operator decrement for the variables in i.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
void addWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation::__add(const DiscreteVariable&) by master.
Idx pos(const DiscreteVariable &v) const final
Returns the position of the variable v.
Size domainSize() const final
Returns the product of the variable&#39;s domain size in the Instantiation.
void __masterDecNotification() const
~Instantiation()
Destructor.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
void setLast()
Assign the last values in the Instantiation.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
void setLastIn(const Instantiation &i)
Assign the last values in the Instantiation for the variables in i.
Sequence< const DiscreteVariable *> __vars
The tuple of variables to be instantiated.
bool forgetMaster()
Deassociate the master MultiDimAdressable, if any.
void unsetOverflow()
Removes the flag overflow.
void __masterIncNotification() const
void dec()
Operator decrement.
bool operator==(const Instantiation &other) const
operator==
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Interface for all classes addressing in a multiDim fashion.
void reorder(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
Class template representing hashing function of LpCol.
Definition: hashFunc.h:469
Base class for discrete random variable.
void setLastOut(const Instantiation &i)
Assign the last values in the Instantiation for the variables not in i.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
The class for generic Hash Tables.
Definition: hashTable.h:676
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void unsetEnd()
Alias for unsetOverflow().
void decOut(const Instantiation &i)
Operator decrement for the variables not in i.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Instantiation & operator--()
Alias of Instantiation::dec().
Instantiation & operator++()
Alias of Instantiation::inc().
bool rend() const
Returns true if the Instantiation reached the rend.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
void clear()
Erase all variables from an Instantiation.
void inc()
Operator increment.
void __chgVal(Idx varPos, Idx newVal)
Modifies internally the value of a given variable of the sequence.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
std::string toString() const
Give a string version of instantiation.
void __reorder(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
Idx hamming() const
Returns the hamming distance of this instantiation.
Instantiation & operator-=(Size depl)
Calls depl times Instantiation::dec().
virtual bool empty() const final
Returns true if the instantiation is empty.
void __masterFirstNotification() const
void incIn(const Instantiation &i)
Operator increment for the variables in i.
void setLastNotVar(const DiscreteVariable &v)
Assign the last values to variables different of v.
void __masterLastNotification() const
MultiDimAdressable * __master
The master, if any, contains precisely the set of variables to be instantiated.
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Headers of MultiDimInterface.
void __notifiedDimChanged(const Sequence< const DiscreteVariable * > &v)
This function is called by the master (if any) when changes arise in its vars list.
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...
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
Instantiation & operator+=(Size depl)
Calls depl times Instantiation::inc().
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
void setLastVar(const DiscreteVariable &v)
Assign the last value in the Instantiation for var v.
void setFirst()
Assign the first values to the tuple of the Instantiation.
Instantiation & operator=(const Instantiation &aI)
Copy operator.
void __swap(Idx i, Idx j)
Swap two variables in the Instantiation.
All hash functions should inherit from this class.
Definition: hashFunc.h:147
Instantiation()
Default constructor: creates an empty tuple.
Size Idx
Type for indexes.
Definition: types.h:50
bool inOverflow() const
Indicates whether the current value of the tuple is correct or not.
void decNotVar(const DiscreteVariable &v)
Operator decrement for vars which are not v.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
bool isSlave() const
Indicates whether the Instantiation has a master.
Inline implemenation of gum::Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
bool actAsSlave(MultiDimAdressable &aMD)
Tries to register the Instantiation to a MultiDimAdressable.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) final
Replace x by y.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
void __add(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
Class hash tables iterators.
void decVar(const DiscreteVariable &v)
Operator decrement for variable v only.
void eraseWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation::__erase(const DiscreteVariable&) by master.
void __masterChangeNotification(Idx varPos, Idx newVal, Idx oldVal) const
bool end() const
Returns true if the Instantiation reached the end.
std::vector< Idx > __vals
The current instantiation: the value of the tuple.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.