aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
integerVariable_inl.h
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 /** @file
23  * @brief Base class for Integer discrete random variables
24  *
25  * @author Christophe GONZALES(@AMU) & Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #include <ostream>
28 #include <sstream>
29 #include <string>
30 #include <algorithm>
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/tools/core/hashTable.h>
35 #include <agrum/tools/variables/discreteVariable.h>
36 
37 // to ease IDE parsers
38 #include <agrum/tools/variables/integerVariable.h>
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42 namespace gum {
43 
44  /// Default constructor
45  INLINE IntegerVariable::IntegerVariable(const std::string& aName,
46  const std::string& aDesc) :
47  DiscreteVariable(aName, aDesc) {
48  // for debugging purposes
49  GUM_CONSTRUCTOR(IntegerVariable);
50  }
51 
52 
53  /// copy constructor
54  INLINE IntegerVariable::IntegerVariable(const IntegerVariable& from) :
55  DiscreteVariable(from), _domain_(from._domain_) {
56  // for debugging purposes
57  GUM_CONS_CPY(IntegerVariable);
58  }
59 
60 
61  /// move constructor
62  INLINE IntegerVariable::IntegerVariable(IntegerVariable&& from) :
63  DiscreteVariable(std::move(from)), _domain_(std::move(from._domain_)) {
64  from._domain_.clear();
65  // for debugging purposes
66  GUM_CONSTRUCTOR(IntegerVariable);
67  }
68 
69 
70  /// virtual copy constructor
71  INLINE IntegerVariable* IntegerVariable::clone() const {
72  return new IntegerVariable(*this);
73  }
74 
75 
76  /// destructor
77  INLINE IntegerVariable::~IntegerVariable() {
78  GUM_DESTRUCTOR(IntegerVariable);
79  }
80 
81 
82  /// copy operator
83  INLINE IntegerVariable& IntegerVariable::operator=(const IntegerVariable& from) {
84  // avoid self assignment
85  if (&from != this) {
86  DiscreteVariable::operator=(from);
87  _domain_ = from._domain_;
88  }
89 
90  return *this;
91  }
92 
93 
94  /// move operator
95  INLINE IntegerVariable& IntegerVariable::operator=(IntegerVariable&& from) {
96  // avoid self assignment
97  if (&from != this) {
98  DiscreteVariable::operator=(std::move(from));
99  _domain_ = std::move(from._domain_);
100  from._domain_.clear();
101  }
102 
103  return *this;
104  }
105 
106 
107  /// inequality operator
108  INLINE bool IntegerVariable::operator!=(const Variable& var) const {
109  return !operator==(var);
110  }
111 
112 
113  /// returns the domain size of the discrete random variable
114  INLINE Size IntegerVariable::domainSize() const {
115  return _domain_.size();
116  }
117 
118 
119  /// returns the type of variable
120  INLINE VarType IntegerVariable::varType() const {
121  return VarType::Integer;
122  }
123 
124 
125  /// returns the index of a given label
126  INLINE Idx IntegerVariable::index(const std::string& aLabel) const {
127  try {
128  return _domain_.pos(std::stoi(aLabel));
129  } catch (...) {
130  GUM_ERROR(NotFound, "label '" << aLabel << "' is unknown in " << this->toString());
131  }
132  }
133 
134 
135  /// returns the ith label
136  INLINE std::string IntegerVariable::label(Idx i) const {
137  // note that if i is outside the domain, Sequence _domain_ will raise
138  // an exception
139  return std::to_string(_domain_[i]);
140  }
141 
142 
143  /// get a numerical representation of the indice-th value.
144  INLINE double IntegerVariable::numerical(Idx i) const {
145  return double(_domain_[i]);
146  }
147 
148 
149  /// returns the domain as a sequence of values
150  INLINE const Sequence< int >& IntegerVariable::integerDomain() const {
151  return _domain_;
152  }
153 
154 
155  /// substitute a value by another one
156  INLINE void IntegerVariable::changeValue(int old_value, int new_value) {
157  if (! _domain_.exists(old_value)) return;
158  if (_domain_.exists(new_value)) {
159  GUM_ERROR(DuplicateElement,
160  "Value" << new_value << " already belongs to the domain of the variable");
161  }
162 
163  eraseValue(old_value);
164  addValue(new_value);
165  }
166 
167 
168  /// erase a value from the domain of the variable
169  INLINE void IntegerVariable::eraseValue(int value) {
170  _domain_.erase(value);
171  }
172 
173 
174  /// clear the domain of the variable
175  INLINE void IntegerVariable::eraseValues() {
176  _domain_.clear();
177  }
178 
179 
180 } /* namespace gum */
181 
182 #endif /* DOXYGEN SHOULD SKIP THIS */