aGrUM  0.16.0
setInst.cpp
Go to the documentation of this file.
1 
24 #include <agrum/multidim/setInst.h>
25 #include <sstream>
26 
27 #ifdef GUM_NO_INLINE
29 #endif
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33 namespace gum {
34 
35  void SetInst::__init(MultiDimAdressable* master) {
36  // for speed issues
37  const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
38  __vars.resize(v.size());
39  __vals.reserve(v.size());
40  // fill the SetInst
41 
42  for (const auto var : v)
43  __add(*var);
44 
45  // if ( master ) actAsSlave( master->getMasterRef() );
46  }
47 
48  // constructor for a SetInst contained into a MultiDimInterface
49 
50  SetInst::SetInst(MultiDimAdressable& d) : /*__master( 0 ),*/ __overflow(false) {
51  // for debugging purposes
52  GUM_CONSTRUCTOR(SetInst);
53  __init(&d);
54  }
55 
57  /*__master( 0 ),*/ __overflow(false) {
58  // for debugging purposes
59  GUM_CONSTRUCTOR(SetInst);
60  __init(const_cast< MultiDimAdressable* >(&d));
61  }
62 
63  // constructor for a SetInst contained into a MultiDimInterface
64 
65  SetInst::SetInst(MultiDimAdressable* d) : /*__master( 0 ),*/ __overflow(false) {
66  // for debugging purposes
67  GUM_CONSTRUCTOR(SetInst);
68 
69  if (d) __init(d);
70  }
71 
72  // constructor for a SetInst contained into a MultiDimInterface
76  SetInst::SetInst(const MultiDimAdressable* const_d) :
77  /*__master( 0 ),*/ __overflow(false) {
78  // for debugging purposes
79  GUM_CONSTRUCTOR(SetInst);
80 
81  if (const_d) __init(const_cast< MultiDimAdressable* >(const_d));
82  }
83 
84  // copy constructor
85 
86  SetInst::SetInst(const SetInst& aI) :
87  /* MultiDimInterface(), __master( 0 ),*/ __overflow(false) {
88  // for debugging purposes
89  GUM_CONS_CPY(SetInst);
90  // copy the content of aI
91  __vars = aI.__vars;
92  __vals = aI.__vals;
93  __overflow = aI.__overflow;
94 
95  // if ( aI.__master && notifyMaster ) actAsSlave( *aI.__master );
96  }
97 
98  SetInst::SetInst(const Instantiation& aI) :
99  /* MultiDimInterface(), __master( 0 ),*/ __overflow(false) {
100  // for debugging purposes
101  GUM_CONS_CPY(SetInst);
102  const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
103  __vars.resize(v.size());
104  //__vals.reserve( v.size() );
105  // fill the SetInst
106 
107  for (const auto var : v) {
108  __add(*var);
109  __vals[__vars.pos(var)] = (Size(1) << (aI.val(*var)));
110  }
111  }
112 
113  // operator=
114  SetInst& SetInst::operator=(const SetInst& aI) {
115  // copy the content of aI
116  __vars = aI.__vars;
117  __vals = aI.__vals;
118  __overflow = aI.__overflow;
119 
120  return *this;
121  }
122 
123  // Gives a string version of a SetInst
124  std::string SetInst::toString() const {
125  std::stringstream sstr;
126  // check if the value of the SetInst is correct
127 
128  if (__overflow) { sstr << "<invalid>"; }
129 
130  sstr << "<";
131 
133 
134  if (iter != __vars.end()) {
135  std::stringstream sstr2;
136  sstr2.str("");
137  Size si = variable(iter.pos()).domainSize();
138  Size valb = vals(iter.pos());
139 
140  while (si-- != 0) {
141  std::stringstream sstr4;
142  sstr4 << ((valb & 1) ? "1" : "0") << sstr2.str();
143  valb >>= 1;
144  sstr2.str("");
145  ;
146  sstr2 << sstr4.str();
147  }
148 
149  sstr << variable(iter.pos()).name() << ":" << sstr2.str();
150  ++iter;
151 
152  while (iter != __vars.end()) {
153  std::stringstream sstr3;
154  sstr3 << "";
155  si = variable(iter.pos()).domainSize();
156 
157  valb = vals(iter.pos());
158 
159  while (si-- != 0) {
160  std::stringstream sstr4;
161 
162  sstr4 << ((valb & 1) ? "1" : "0") << sstr3.str();
163  valb >>= 1;
164  sstr3.str("");
165  sstr3 << sstr4.str();
166  }
167 
168  sstr << "|" << variable(iter.pos()).name() << ":" << sstr3.str();
169  ++iter;
170  }
171  }
172 
173  sstr << ">";
174 
175  return sstr.str();
176  }
177 
178  // an operator for user-friendly displaying the content of a SetInst
179  std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
180  aStream << i.toString();
181  return aStream;
182  }
184  inst.add(i);
185  return inst;
186  }
188  inst.erase(i);
189  return inst;
190  }
191 
194  bij,
195  const gum::SetInst& i,
196  gum::SetInst& j) {
197  try {
198  for (const auto var : i.variablesSequence())
199  j.chgVal(bij.second(var), i.val(var));
200  } catch (gum::NotFound&) {
201  GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst");
202  }
203  }
204 
205 } /* namespace gum */
206 
207 #endif // DOXYGEN_SHOULD_SKIP_THIS
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
virtual Size domainSize() const final
Returns the product of the variables domain size.
static void assign_values(Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const SetInst &i, SetInst &j)
Assign the values of i in j, using bij as a bijection between i and j variables.
SetInst & chgVal(const DiscreteVariable &v, Idx newVal)
Assign newVal to variable v in the SetInst.
void add(const DiscreteVariable &v)
Adds a new variable in the SetInst.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::string toString() const
Give a string version of SetInst.
const Sequence< const DiscreteVariable *> & variablesSequence() const
Returns the sequence of DiscreteVariable of this SetInst.
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:605
Class for assigning/browsing values to tuples of discrete variables.
Definition: setInst.h:82
void __add(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition: sequence.h:1037
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
std::vector< Size > __vals
The current SetInst: the value of the tuple.
Definition: setInst.h:849
bool __overflow
The overflow flag.
Definition: setInst.h:852
Sequence< const DiscreteVariable *> __vars
The tuple of variables to be instantiated.
Definition: setInst.h:846
void erase(const DiscreteVariable &v)
Removes a variable from the SetInst.
MultiDimAdressable()
Default constructor.
SetInst()
Default constructor: creates an empty tuple.
Idx val(Idx i) const
Returns the current value of a variable at a given position.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void __init(MultiDimAdressable *master)
Intialize the SetInst.
MultiDimInterface & operator>>(MultiDimInterface &c, const DiscreteVariable &v)
Removes a var from the variables of the MutliDimAdressing.
SetInst & operator=(const SetInst &aI)
Copy operator.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.