aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
setInst_inl.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#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
46
47// to ease IDE PARSER
49
50namespace gum {
51
52 // indicates whether a given variable belongs to the SetInst
53
54 INLINE bool SetInst::contains(const DiscreteVariable& v) const { return _vars_.exists(&v); }
55
56 // indicates whether a given variable belongs to the SetInst
57
58 INLINE bool SetInst::contains(const DiscreteVariable* v) const { return _vars_.exists(v); }
59
60 // modifies internally the value of a given variable of the sequence
61
62 INLINE void SetInst::_chgVal_(Idx varPos, Idx newVal) {
63 // Size oldVal = _vals_[varPos];
64 _vals_[varPos] = Idx(1) << newVal;
65
66 // if ( _master_ )
67 // _master_->changeNotification( *this, _vars_[varPos], oldVal, newVal
68 // );
69 }
70
71 // modifies the value of a given variable of the sequence (external function)
72
73 INLINE SetInst& SetInst::chgVal(const DiscreteVariable& v, Idx newVal) {
74 try {
75 // check that the variable does belong to the SetInst and that the new
76 // value is possible.
77 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
78
79 if (newVal >= v.domainSize()) GUM_ERROR(OutOfBounds, "")
80
81 // if we were in overflow, indicate that we are not anymore
82 _overflow_ = false;
83
84 _chgVal_(varPos, newVal);
85
86 return *this;
87 } catch (NotFound const&) {
88 std::string name = "SetInst does not contain this DiscreteVariable: ";
89 GUM_ERROR(NotFound, name + v.name())
90 }
91 }
92
93 INLINE SetInst& SetInst::chgVal(const DiscreteVariable* v, Idx newVal) {
94 try {
95 // check that the variable does belong to the SetInst and that the new
96 // value is possible.
97 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
98
99 if (newVal >= v->domainSize()) GUM_ERROR(OutOfBounds, "")
100
101 // if we were in overflow, indicate that we are not anymore
102 _overflow_ = false;
103
104 _chgVal_(varPos, newVal);
105
106 return *this;
107 } catch (NotFound const&) {
108 std::string name = "SetInst does not contain this DiscreteVariable: ";
109 GUM_ERROR(NotFound, name + v->name())
110 }
111 }
112
113 // modifies the value of a given variable of the sequence (external function)
114
115 INLINE SetInst& SetInst::chgVal(Idx varPos, Idx newVal) {
116 // check that the variable does belong to the SetInst and that the new
117 // value is possible.
118 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
119
120 if (newVal >= _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
121
122 // if we were in overflow, indicate that we are not anymore
123 _overflow_ = false;
124
125 _chgVal_(varPos, newVal);
126
127 return *this;
128 }
129
130 // modifies internally the value of a given variable of the sequence
131
132 INLINE void SetInst::_chgVals_(Idx varPos, const Size newVals) {
133 // Size oldVal = _vals_[varPos];
134 _vals_[varPos] = 0;
135 _vals_[varPos] = newVals;
136
137 // if ( _master_ )
138 // _master_->changeNotification( *this, _vars_[varPos], oldVal, newVals
139 // );
140 }
141
142 // modifies the value of a given variable of the sequence (external function)
143
144 INLINE SetInst& SetInst::chgVals(const DiscreteVariable& v, const Size newVals) {
145 try {
146 // check that the variable does belong to the SetInst and that the new
147 // value is possible.
148 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
149
150 if (newVals >= (Size)1 << v.domainSize()) GUM_ERROR(OutOfBounds, "")
151
152 // if we were in overflow, indicate that we are not anymore
153 _overflow_ = false;
154
155 _chgVals_(varPos, newVals);
156
157 return *this;
158 } catch (NotFound const&) {
159 std::string name = "SetInst does not contain this DiscreteVariable: ";
160 GUM_ERROR(NotFound, name + v.name())
161 }
162 }
163
164 INLINE SetInst& SetInst::chgVals(const DiscreteVariable* v, const Size newVals) {
165 try {
166 // check that the variable does belong to the SetInst and that the new
167 // value is possible.
168 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
169
170 if (newVals >= (Size)1 << v->domainSize()) GUM_ERROR(OutOfBounds, "")
171
172 // if we were in overflow, indicate that we are not anymore
173 _overflow_ = false;
174
175 _chgVals_(varPos, newVals);
176
177 return *this;
178 } catch (NotFound const&) {
179 std::string name = "SetInst does not contain this DiscreteVariable: ";
180 GUM_ERROR(NotFound, name + v->name())
181 }
182 }
183
184 // modifies the value of a given variable of the sequence (external function)
185
186 INLINE SetInst& SetInst::chgVals(Idx varPos, const Size newVal) {
187 // check that the variable does belong to the SetInst and that the new
188 // value is possible.
189 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
190
191 if (newVal >= (Size)1 << _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
192
193 // if we were in overflow, indicate that we are not anymore
194 _overflow_ = false;
195
196 _chgVals_(varPos, newVal);
197
198 return *this;
199 }
200
201 INLINE SetInst& SetInst::addVal(Idx varPos, Idx newVal) {
202 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
203
204 if (newVal >= _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
205
206 _chgVals_(varPos, (Idx(1) << newVal) | _vals_[varPos]);
207 return *this;
208 }
209
210 INLINE SetInst& SetInst::addVal(const DiscreteVariable* v, Idx newVal) {
211 try {
212 // check that the variable does belong to the SetInst and that the new
213 // value is possible.
214 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
215
216 if (newVal >= v->domainSize()) GUM_ERROR(OutOfBounds, "")
217
218 // if we were in overflow, indicate that we are not anymore
219 _overflow_ = false;
220
221 addVal(varPos, newVal);
222
223 return *this;
224 } catch (NotFound const&) {
225 std::string name = "SetInst does not contain this DiscreteVariable: ";
226 GUM_ERROR(NotFound, name + v->name())
227 }
228 }
229
230 INLINE SetInst& SetInst::addVal(const DiscreteVariable& v, Idx newVal) {
231 try {
232 // check that the variable does belong to the SetInst and that the new
233 // value is possible.
234 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
235
236 if (newVal >= v.domainSize()) GUM_ERROR(OutOfBounds, "")
237
238 // if we were in overflow, indicate that we are not anymore
239 _overflow_ = false;
240
241 addVal(varPos, newVal);
242
243 return *this;
244 } catch (NotFound const&) {
245 std::string name = "SetInst does not contain this DiscreteVariable: ";
246 GUM_ERROR(NotFound, name + v.name())
247 }
248 }
249
250 INLINE SetInst& SetInst::addVals(Idx varPos, const Size newVal) {
251 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
252
253 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
254
255 _chgVals_(varPos, newVal | _vals_[varPos]);
256 return *this;
257 }
258
259 INLINE SetInst& SetInst::addVals(const DiscreteVariable* v, const Size newVal) {
260 try {
261 // check that the variable does belong to the SetInst and that the new
262 // value is possible.
263 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
264
265 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
266
267 // if we were in overflow, indicate that we are not anymore
268
269 addVals(varPos, newVal);
270
271 return *this;
272 } catch (NotFound const&) {
273 std::string name = "SetInst does not contain this DiscreteVariable: ";
274 GUM_ERROR(NotFound, name + v->name())
275 }
276 }
277
278 INLINE SetInst& SetInst::addVals(const DiscreteVariable& v, const Size newVal) {
279 try {
280 // check that the variable does belong to the SetInst and that the new
281 // value is possible.
282 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
283
284 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
285
286 // if we were in overflow, indicate that we are not anymore
287 _overflow_ = false;
288
289 addVals(varPos, newVal);
290
291 return *this;
292 } catch (NotFound const&) {
293 std::string name = "SetInst does not contain this DiscreteVariable: ";
294 GUM_ERROR(NotFound, name + v.name())
295 }
296 }
297
298 INLINE SetInst& SetInst::remVal(Idx varPos, Idx newVal) {
299 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
300
301 if (newVal >= _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
302
303 _chgVals_(varPos, ~(1 << newVal) & _vals_[varPos]);
304 return *this;
305 }
306
307 INLINE SetInst& SetInst::remVal(const DiscreteVariable* v, Idx newVal) {
308 try {
309 // check that the variable does belong to the SetInst and that the new
310 // value is possible.
311 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
312
313 if (newVal >= v->domainSize()) GUM_ERROR(OutOfBounds, "")
314
315 // if we were in overflow, indicate that we are not anymore
316
317 remVal(varPos, newVal);
318
319 return *this;
320 } catch (NotFound const&) {
321 std::string name = "SetInst does not contain this DiscreteVariable: ";
322 GUM_ERROR(NotFound, name + v->name())
323 }
324 }
325
326 INLINE SetInst& SetInst::remVal(const DiscreteVariable& v, Idx newVal) {
327 try {
328 // check that the variable does belong to the SetInst and that the new
329 // value is possible.
330 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
331
332 if (newVal >= v.domainSize()) GUM_ERROR(OutOfBounds, "")
333
334 // if we were in overflow, indicate that we are not anymore
335 _overflow_ = false;
336
337 remVal(varPos, newVal);
338
339 return *this;
340 } catch (NotFound const&) {
341 std::string name = "SetInst does not contain this DiscreteVariable: ";
342 GUM_ERROR(NotFound, name + v.name())
343 }
344 }
345
346 INLINE SetInst& SetInst::remVals(Idx varPos, const Size newVal) {
347 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
348
349 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
350
351 _chgVals_(varPos, ~newVal & _vals_[varPos]);
352 return *this;
353 }
354
355 INLINE SetInst& SetInst::remVals(const DiscreteVariable* v, const Size newVal) {
356 try {
357 // check that the variable does belong to the SetInst and that the new
358 // value is possible.
359 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
360
361 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
362
363 // if we were in overflow, indicate that we are not anymore
364
365 remVals(varPos, newVal);
366
367 return *this;
368 } catch (NotFound const&) {
369 std::string name = "SetInst does not contain this DiscreteVariable: ";
370 GUM_ERROR(NotFound, name + v->name())
371 }
372 }
373
374 INLINE SetInst& SetInst::remVals(const DiscreteVariable& v, const Size newVal) {
375 try {
376 // check that the variable does belong to the SetInst and that the new
377 // value is possible.
378 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
379
380 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
381
382 // if we were in overflow, indicate that we are not anymore
383 _overflow_ = false;
384
385 remVals(varPos, newVal);
386
387 return *this;
388 } catch (NotFound const&) {
389 std::string name = "SetInst does not contain this DiscreteVariable: ";
390 GUM_ERROR(NotFound, name + v.name())
391 }
392 }
393
394 INLINE SetInst& SetInst::chgDifVal(Idx varPos, const Size newVal) {
395 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
396
397 if (newVal >= _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
398
399 _chgVals_(varPos, newVal ^ _vals_[varPos]);
400 return *this;
401 }
402
403 INLINE SetInst& SetInst::interVals(Idx varPos, const Size newVal) {
404 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
405
406 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
407
408 _chgVals_(varPos, newVal & _vals_[varPos]);
409 return *this;
410 }
411
412 INLINE SetInst& SetInst::interVals(const DiscreteVariable* v, const Size newVal) {
413 try {
414 // check that the variable does belong to the SetInst and that the new
415 // value is possible.
416 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
417
418 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
419
420 // if we were in overflow, indicate that we are not anymore
421
422 interVals(varPos, newVal);
423
424 return *this;
425 } catch (NotFound const&) {
426 std::string name = "SetInst does not contain this DiscreteVariable: ";
427 GUM_ERROR(NotFound, name + v->name())
428 }
429 }
430
431 INLINE SetInst& SetInst::interVals(const DiscreteVariable& v, const Size newVal) {
432 try {
433 // check that the variable does belong to the SetInst and that the new
434 // value is possible.
435 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
436
437 if (newVal >= (Size(1) << _vars_[varPos]->domainSize())) GUM_ERROR(OutOfBounds, "")
438
439 // if we were in overflow, indicate that we are not anymore
440 _overflow_ = false;
441
442 interVals(varPos, newVal);
443
444 return *this;
445 } catch (NotFound const&) {
446 std::string name = "SetInst does not contain this DiscreteVariable: ";
447 GUM_ERROR(NotFound, name + v.name())
448 }
449 }
450
451 INLINE SetInst& SetInst::interVal(Idx varPos, Idx newVal) {
452 if (_vals_.size() <= varPos) GUM_ERROR(NotFound, "")
453
454 if (newVal >= _vars_[varPos]->domainSize()) GUM_ERROR(OutOfBounds, "")
455
456 _chgVals_(varPos, (Size(1) << newVal) & _vals_[varPos]);
457 return *this;
458 }
459
460 INLINE SetInst& SetInst::interVal(const DiscreteVariable* v, Idx newVal) {
461 try {
462 // check that the variable does belong to the SetInst and that the new
463 // value is possible.
464 Idx varPos = _vars_.pos(v); // throws NotFound if v doesn't belong to this
465
466 if (newVal >= v->domainSize()) GUM_ERROR(OutOfBounds, "")
467
468 // if we were in overflow, indicate that we are not anymore
469
470 interVal(varPos, newVal);
471
472 return *this;
473 } catch (NotFound const&) {
474 std::string name = "SetInst does not contain this DiscreteVariable: ";
475 GUM_ERROR(NotFound, name + v->name())
476 }
477 }
478
479 INLINE SetInst& SetInst::interVal(const DiscreteVariable& v, Idx newVal) {
480 try {
481 // check that the variable does belong to the SetInst and that the new
482 // value is possible.
483 Idx varPos = _vars_.pos(&v); // throws NotFound if v doesn't belong to this
484
485 if (newVal >= v.domainSize()) GUM_ERROR(OutOfBounds, "")
486
487 // if we were in overflow, indicate that we are not anymore
488 _overflow_ = false;
489
490 interVal(varPos, newVal);
491
492 return *this;
493 } catch (NotFound const&) {
494 std::string name = "SetInst does not contain this DiscreteVariable: ";
495 GUM_ERROR(NotFound, name + v.name())
496 }
497 }
498
499 // adds a new var to the sequence of vars
500
501 INLINE void SetInst::add(const DiscreteVariable& v) {
502 // if _master_ : not allowed
503 // if ( _master_ ) GUM_ERROR( OperationNotAllowed, "in slave SetInst" )
504
505 // check if the variable already belongs to the tuple of variables
506 // of the SetInst
507 if (_vars_.exists(&v))
508 GUM_ERROR(DuplicateElement, "Variable '" << v.name() << "' already exists in this SetInst")
509
510 // actually add the new dimension
511 _add_(v);
512 }
513
514 // removes a variable from the sequence of vars
515
516 INLINE void SetInst::erase(const DiscreteVariable& v) {
517 // if _master_ : not allowed
518 // if ( _master_ ) GUM_ERROR( OperationNotAllowed, "in slave SetInst" )
519
520 // check that the variable does actually belong to the SetInst
521 if (!_vars_.exists(&v)) GUM_ERROR(NotFound, "Var does not exist in this SetInst")
522
523 // actually delete the dimension
524 _erase_(v);
525 }
526
527 // removes everything
528 INLINE void SetInst::clear() {
529 // if ( _master_ ) GUM_ERROR( OperationNotAllowed, "in slave SetInst" )
530
531 _vars_.clear();
532 _vals_.clear();
533 }
534
537
538 INLINE Size SetInst::domainSize() const {
539 Size s = 1;
540
541 for (const auto var: _vars_)
542 s *= var->domainSize();
543
544 return s;
545 }
546
547 // returns the index of a var
548
549 INLINE Idx SetInst::pos(const DiscreteVariable& k) const { return _vars_.pos(&k); }
550
551 // Default constructor
552
553 INLINE SetInst::SetInst() : /* _master_( 0 ),*/ _overflow_(false) { GUM_CONSTRUCTOR(SetInst); }
554
555 // destructor
556
557 INLINE SetInst::~SetInst() {
558 GUM_DESTRUCTOR(SetInst);
559 // unregister the SetInst from its _master_
560
561 // if ( _master_ ) _master_->unregisterSlave( *this );
562 }
563
564 // returns the number of vars in the sequence
565
566 INLINE Idx SetInst::nbrDim() const { return _vars_.size(); }
567
568 // returns the current value of a given variable
569
570 INLINE Size SetInst::vals(Idx i) const {
571 if (i >= _vals_.size()) GUM_ERROR(NotFound, "")
572
573 return _vals_[i];
574 }
575
576 INLINE Size SetInst::vals(const DiscreteVariable& var) const { return _vals_[_vars_.pos(&var)]; }
577
578 INLINE Size SetInst::vals(const DiscreteVariable* var) const { return _vals_[_vars_.pos(var)]; }
579
580 INLINE Idx SetInst::val(const DiscreteVariable* var) const {
581 Idx n = 0;
582 Size value = _vals_[_vars_.pos(var)];
583
584 if (_vals_[_vars_.pos(var)] % 2 == 0) {
585 while (value & 1) {
586 n++;
587 value >>= 1;
588 }
589
590 return n;
591 } else GUM_ERROR(NotFound, "There is more than one value ")
592 }
593
594 INLINE Idx SetInst::nbrOccurences(const DiscreteVariable& var) const {
595 Idx n = 0;
596 Size val = _vals_[_vars_.pos(&var)];
597
598 while (val) {
599 n += (val & 1);
600 val >>= 1;
601 }
602
603 return n;
604 }
605
606 INLINE Idx SetInst::val(const DiscreteVariable& var) const {
607 Idx n = 0;
608 Size value = _vals_[_vars_.pos(&var)];
609
610 if (nbrOccurences(var) == 1) {
611 while (value > 1) {
612 n++;
613 value >>= 1;
614 }
615
616 return n;
617 } else GUM_ERROR(NotFound, "There is more than one value ")
618 }
619
620 // returns the variable at position i in the tuple
621
622 INLINE const DiscreteVariable& SetInst::variable(Idx i) const { return *(_vars_.atPos(i)); }
623
624 // indicates whether the current value of the tuple is correct or not
625
626 INLINE bool SetInst::inOverflow() const { return _overflow_; }
627
628 // end() just is a synonym for inOverflow()
629
630 INLINE bool SetInst::end() const { return inOverflow(); }
631
632 // rend() just is a synonym for inOverflow()
633
634 INLINE bool SetInst::rend() const { return inOverflow(); }
635
636 // indicates that the current value is correct even if it should be in
637 // overflow
638
639 INLINE void SetInst::unsetOverflow() { _overflow_ = false; }
640
641 // alias for unsetOverflow
642
643 INLINE void SetInst::unsetEnd() { _overflow_ = false; }
644
645 // reorder vars in *this
646 INLINE void SetInst::reorder(const SetInst& i) { reorder(i.variablesSequence()); }
647
648 // change values with those in i
649
650 INLINE SetInst& SetInst::chgValIn(const SetInst& i) {
651 _overflow_ = false;
652 Idx s = i.nbrDim();
653
654 for (Size p = 0; p < s; ++p)
655 if (contains(i.variable(p)))
656 // __vals[pos( i.variable( p ) )] = i.val( p );
657 _chgVals_(pos(i.variable(p)), i.vals(i.variable(p)));
658
659 return *this;
660 }
661
662 // returns the sequence of DiscreteVariable
663
664 INLINE const Sequence< const DiscreteVariable* >& SetInst::variablesSequence() const {
665 return _vars_;
666 }
667
668 // replace 2 vars in the SetInst
669
670 INLINE void SetInst::_swap_(Idx i, Idx j) {
671 if (i == j) return;
672
673 _vars_.swap(i, j);
674
675 Size v;
676
677 v = _vals_[i];
678
679 _vals_[i] = _vals_[j];
680
681 _vals_[j] = v;
682 }
683
684 // reordering
685
686 INLINE void SetInst::reorder(const Sequence< const DiscreteVariable* >& original) {
687 Idx max = original.size();
688 Idx position = 0;
689
690 for (Idx i = 0; i < max; ++i) {
691 const DiscreteVariable* pv = original.atPos(i);
692
693 if (contains(pv)) {
694 GUM_ASSERT(pos(*pv) >= position); // this var should not be
695 // already placed.
696 _swap_(position, pos(*pv));
697 position++;
698 }
699 }
700 }
701
702 // adds a new var to the sequence of vars
703
704 INLINE void SetInst::_add_(const DiscreteVariable& v) {
705 _vars_.insert(&v);
706 _vals_.push_back(1);
707 _overflow_ = false;
708 }
709
710 // removes a variable from the sequence of vars
711
712 INLINE void SetInst::_erase_(const DiscreteVariable& v) {
713 // get the position of the variable
714 Idx pos = _vars_.pos(&v);
715 _vars_.erase(&v);
716 _vals_.erase(_vals_.begin() + pos);
717 }
718
719 // is this empty ?
720 INLINE bool SetInst::empty() const { return _vals_.empty(); }
721
722 // Replace x by y.
723 INLINE void SetInst::replace_(const DiscreteVariable* x, const DiscreteVariable* y) {
724 _vars_.setAtPos(_vars_.pos(x), y);
725 }
726} /* namespace gum */
727
728#endif // DOXYGEN_SHOULD_SKIP_THIS
Base class for discrete random variable.
Exception : the element we looked for cannot be found.
Class for assigning/browsing values to tuples of discrete variables.
Definition setInst.h:101
SetInst()
Default constructor: creates an empty tuple.
void _chgVal_(Idx varPos, Idx newVal)
Change the value of a variable.
Idx pos(const DiscreteVariable &v) const
Returns the position of the variable v.
void erase(const DiscreteVariable &v)
Removes a variable from the SetInst.
Sequence< const DiscreteVariable * > _vars_
The tuple of variables to be instantiated.
Definition setInst.h:864
void _chgVals_(Idx varPos, const Size newVal)
Change the value of a variable.
SetInst & interVals(const DiscreteVariable &v, const Size newVal)
Does an intersection (binary and) between the old value and new value.
bool contains(const DiscreteVariable &v) const
Indicates whether a given variable belongs to the SetInst.
void _erase_(const DiscreteVariable &v)
Removes a variable from the sequence of vars.
SetInst & chgDifVal(Idx varPos, const Size newVal)
Does the difference (binary or) between the old value and new value.
SetInst & addVal(const DiscreteVariable &v, Idx newVal)
Add newVal to variable v in the SetInst.
void _add_(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
bool _overflow_
The overflow flag.
Definition setInst.h:870
SetInst & remVals(const DiscreteVariable &v, const Size newVal)
Remove newVal from the variable v in the SetInst.
Size domainSize() const
Returns the product of the variable's domain size in the SetInst.
SetInst & interVal(const DiscreteVariable &v, Idx newVal)
Does an intersection (binary and) between the old value and new value.
SetInst & remVal(const DiscreteVariable &v, Idx newVal)
Remove newVal from the variable v in the SetInst.
void add(const DiscreteVariable &v)
Adds a new variable in the SetInst.
std::vector< Size > _vals_
The current SetInst: the value of the tuple.
Definition setInst.h:867
SetInst & chgVal(const DiscreteVariable &v, Idx newVal)
Assign newVal to variable v in the SetInst.
void clear()
Erase all variables from an SetInst.
SetInst & chgVals(const DiscreteVariable &v, const Size newVal)
Assign newVal to variable v in the SetInst.
SetInst & addVals(const DiscreteVariable &v, const Size newVal)
Add newVal to variable v in the SetInst.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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
Headers for the abstract base class for all multi dimensionnal containers.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of SetInst.