aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
setInst.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #include <agrum/tools/multidim/implementations/multiDimAdressable.h>
23 #include <agrum/tools/multidim/setInst.h>
24 #include <sstream>
25 
26 #ifdef GUM_NO_INLINE
27 # include <agrum/tools/multidim/setInst_inl.h>
28 #endif
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace gum {
33 
34  void SetInst::_init_(MultiDimAdressable* master) {
35  // for speed issues
36  const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
37  _vars_.resize(v.size());
38  _vals_.reserve(v.size());
39  // fill the SetInst
40 
41  for (const auto var: v)
42  _add_(*var);
43 
44  // if ( master ) actAsSlave( master->getMasterRef() );
45  }
46 
47  // constructor for a SetInst contained into a MultiDimInterface
48 
49  SetInst::SetInst(MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
50  // for debugging purposes
51  GUM_CONSTRUCTOR(SetInst);
52  _init_(&d);
53  }
54 
55  SetInst::SetInst(const MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
56  // for debugging purposes
57  GUM_CONSTRUCTOR(SetInst);
58  _init_(const_cast< MultiDimAdressable* >(&d));
59  }
60 
61  // constructor for a SetInst contained into a MultiDimInterface
62 
63  SetInst::SetInst(MultiDimAdressable* d) : /* _master_( 0 ),*/ _overflow_(false) {
64  // for debugging purposes
65  GUM_CONSTRUCTOR(SetInst);
66 
67  if (d) _init_(d);
68  }
69 
70  // constructor for a SetInst contained into a MultiDimInterface
71  /** this constructor is needed in order to allow creation of SetInst(this)
72  * in MultiDimAdressable and below */
73 
74  SetInst::SetInst(const MultiDimAdressable* const_d) : /* _master_( 0 ),*/ _overflow_(false) {
75  // for debugging purposes
76  GUM_CONSTRUCTOR(SetInst);
77 
78  if (const_d) _init_(const_cast< MultiDimAdressable* >(const_d));
79  }
80 
81  // copy constructor
82 
83  SetInst::SetInst(const SetInst& aI) :
84  /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
85  // for debugging purposes
86  GUM_CONS_CPY(SetInst);
87  // copy the content of aI
88  _vars_ = aI._vars_;
89  _vals_ = aI._vals_;
90  _overflow_ = aI._overflow_;
91 
92  // if ( aI. _master_ && notifyMaster ) actAsSlave( *aI. _master_ );
93  }
94 
95  SetInst::SetInst(const Instantiation& aI) :
96  /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
97  // for debugging purposes
98  GUM_CONS_CPY(SetInst);
99  const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
100  _vars_.resize(v.size());
101  // __vals.reserve( v.size() );
102  // fill the SetInst
103 
104  for (const auto var: v) {
105  _add_(*var);
106  _vals_[_vars_.pos(var)] = (Size(1) << (aI.val(*var)));
107  }
108  }
109 
110  // operator=
111  SetInst& SetInst::operator=(const SetInst& aI) {
112  // copy the content of aI
113  _vars_ = aI._vars_;
114  _vals_ = aI._vals_;
115  _overflow_ = aI._overflow_;
116 
117  return *this;
118  }
119 
120  // Gives a string version of a SetInst
121  std::string SetInst::toString() const {
122  std::stringstream sstr;
123  // check if the value of the SetInst is correct
124 
125  if (_overflow_) { sstr << "<invalid>"; }
126 
127  sstr << "<";
128 
129  Sequence< const DiscreteVariable* >::iterator_safe iter = _vars_.begin();
130 
131  if (iter != _vars_.end()) {
132  std::stringstream sstr2;
133  sstr2.str("");
134  Size si = variable(iter.pos()).domainSize();
135  Size valb = vals(iter.pos());
136 
137  while (si-- != 0) {
138  std::stringstream sstr4;
139  sstr4 << ((valb & 1) ? "1" : "0") << sstr2.str();
140  valb >>= 1;
141  sstr2.str("");
142  ;
143  sstr2 << sstr4.str();
144  }
145 
146  sstr << variable(iter.pos()).name() << ":" << sstr2.str();
147  ++iter;
148 
149  while (iter != _vars_.end()) {
150  std::stringstream sstr3;
151  sstr3 << "";
152  si = variable(iter.pos()).domainSize();
153 
154  valb = vals(iter.pos());
155 
156  while (si-- != 0) {
157  std::stringstream sstr4;
158 
159  sstr4 << ((valb & 1) ? "1" : "0") << sstr3.str();
160  valb >>= 1;
161  sstr3.str("");
162  sstr3 << sstr4.str();
163  }
164 
165  sstr << "|" << variable(iter.pos()).name() << ":" << sstr3.str();
166  ++iter;
167  }
168  }
169 
170  sstr << ">";
171 
172  return sstr.str();
173  }
174 
175  // an operator for user-friendly displaying the content of a SetInst
176  std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
177  aStream << i.toString();
178  return aStream;
179  }
180  gum::SetInst& operator<<(gum::SetInst& inst, const gum::DiscreteVariable& i) {
181  inst.add(i);
182  return inst;
183  }
184  gum::SetInst& operator>>(gum::SetInst& inst, const gum::DiscreteVariable& i) {
185  inst.erase(i);
186  return inst;
187  }
188 
189  void gum::SetInst::assign_values(
190  gum::Bijection< const gum::DiscreteVariable*, const gum::DiscreteVariable* >& bij,
191  const gum::SetInst& i,
192  gum::SetInst& j) {
193  try {
194  for (const auto var: i.variablesSequence())
195  j.chgVal(bij.second(var), i.val(var));
196  } catch (gum::NotFound&) {
197  GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst")
198  }
199  }
200 
201 } /* namespace gum */
202 
203 #endif // DOXYGEN_SHOULD_SKIP_THIS