61#ifndef DOXYGEN_SHOULD_SKIP_THIS
69 GUM_CONSTRUCTOR(IntegerVariable);
73 INLINE IntegerVariable::IntegerVariable(
const IntegerVariable& from) :
74 DiscreteVariable(from), _domain_(from._domain_) {
76 GUM_CONS_CPY(IntegerVariable);
80 INLINE IntegerVariable::IntegerVariable(IntegerVariable&& from) noexcept :
81 DiscreteVariable(std::move(from)), _domain_(std::move(from._domain_)) {
82 from._domain_.clear();
84 GUM_CONS_MOV(IntegerVariable);
88 INLINE IntegerVariable* IntegerVariable::clone()
const {
return new IntegerVariable(*
this); }
91 INLINE IntegerVariable::~IntegerVariable() { GUM_DESTRUCTOR(IntegerVariable); }
94 INLINE IntegerVariable& IntegerVariable::operator=(
const IntegerVariable& from) {
97 DiscreteVariable::operator=(from);
98 _domain_ = from._domain_;
105 INLINE IntegerVariable& IntegerVariable::operator=(IntegerVariable&& from) {
108 DiscreteVariable::operator=(std::move(from));
109 _domain_ = std::move(from._domain_);
110 from._domain_.clear();
117 INLINE Size IntegerVariable::domainSize()
const {
return _domain_.size(); }
120 INLINE
VarType IntegerVariable::varType()
const {
return VarType::INTEGER; }
123 INLINE Idx IntegerVariable::index(std::string_view aLabel)
const {
126 x = std::stoi(std::string{aLabel});
127 }
catch (
const std::exception&) {
130 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
132 if (ind != _domain_.size() && _domain_[ind] == x) {
139 INLINE Idx IntegerVariable::closestIndex(
double val)
const {
140 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
142 if (ind == _domain_.size())
return _domain_.size() - 1;
143 if (ind == 0)
return 0;
145 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
153 INLINE std::string IntegerVariable::label(Idx i)
const {
156 if (i < 0 || i >= _domain_.size())
158 return
std::to_string(_domain_[i]);
162 INLINE
double IntegerVariable::numerical(Idx i)
const {
163 if (i < 0 || i >= _domain_.size())
165 return double(_domain_[i]);
169 INLINE
const std::vector< int >& IntegerVariable::integerDomain()
const {
return _domain_; }
171 INLINE
bool IntegerVariable::isValue(
int value)
const {
172 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
173 return (ind != _domain_.size() && _domain_[ind] == value);
177 INLINE
void IntegerVariable::changeValue(
int old_value,
int new_value) {
178 if (!isValue(old_value))
return;
179 if (isValue(new_value)) {
181 "Value" << new_value <<
" already belongs to the domain of the variable");
184 eraseValue(old_value);
189 INLINE
void IntegerVariable::eraseValue(
int value) {
190 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
191 if (ind != _domain_.size() && _domain_[ind] == value) {
192 _domain_.erase(_domain_.begin() + ind);
197 INLINE
void IntegerVariable::eraseValues() { _domain_.clear(); }
199 INLINE
bool IntegerVariable::_checkSameDomain_(
const gum::Variable& aRV)
const {
201 const auto& cv =
static_cast< const IntegerVariable&
>(aRV);
202 if (domainSize() != cv.domainSize())
return false;
203 return cv._domain_ == _domain_;
206 INLINE
void IntegerVariable::addValue(
int value) {
207 if (isValue(value)) {
209 "Value " << value <<
" already belongs to the domain of the variable");
211 _domain_.push_back(value);
212 std::sort(_domain_.begin(), _domain_.end());
215 INLINE std::string IntegerVariable::closestLabel(
double val)
const {
216 return label(closestIndex(val));
219 INLINE std::string IntegerVariable::toFast()
const {
220 return std::format(
"{}{}", name(), domain());
223 INLINE std::string IntegerVariable::stype()
const {
return "Integer"; }
Base class for discrete random variable.
Exception : a similar element already exists.
IntegerVariable(std::string_view aName, std::string_view aDesc="")
constructor
Exception : the element we looked for cannot be found.
Exception : out of bound.
Base class for every random variable.
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Class hash tables iterators.
Base class for integer discrete random variables.
gum is the global namespace for all aGrUM entities