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(
const std::string& aLabel)
const {
124 const auto x = std::stoi(aLabel);
125 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
127 if (ind != _domain_.size() && _domain_[ind] == x) {
134 INLINE Idx IntegerVariable::closestIndex(
double val)
const {
135 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
137 if (ind == _domain_.size())
return _domain_.size() - 1;
138 if (ind == 0)
return 0;
140 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
148 INLINE std::string IntegerVariable::label(Idx i)
const {
151 if (i < 0 || i >= _domain_.size())
153 return
std::to_string(_domain_[i]);
157 INLINE
double IntegerVariable::numerical(Idx i)
const {
158 if (i < 0 || i >= _domain_.size())
160 return double(_domain_[i]);
164 INLINE
const std::vector< int >& IntegerVariable::integerDomain()
const {
return _domain_; }
166 INLINE
bool IntegerVariable::isValue(
int value)
const {
167 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
168 return (ind != _domain_.size() && _domain_[ind] == value);
172 INLINE
void IntegerVariable::changeValue(
int old_value,
int new_value) {
173 if (!isValue(old_value))
return;
174 if (isValue(new_value)) {
176 "Value" << new_value <<
" already belongs to the domain of the variable");
179 eraseValue(old_value);
184 INLINE
void IntegerVariable::eraseValue(
int value) {
185 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
186 if (ind != _domain_.size() && _domain_[ind] == value) {
187 _domain_.erase(_domain_.begin() + ind);
192 INLINE
void IntegerVariable::eraseValues() { _domain_.clear(); }
194 INLINE
bool IntegerVariable::_checkSameDomain_(
const gum::Variable& aRV)
const {
196 const auto& cv =
static_cast< const IntegerVariable&
>(aRV);
197 if (domainSize() != cv.domainSize())
return false;
198 return cv._domain_ == _domain_;
201 INLINE
void IntegerVariable::addValue(
int value) {
202 if (isValue(value)) {
204 "Value " << value <<
" already belongs to the domain of the variable");
206 _domain_.push_back(value);
207 std::sort(_domain_.begin(), _domain_.end());
210 INLINE std::string IntegerVariable::closestLabel(
double val)
const {
211 return label(closestIndex(val));
214 INLINE std::string IntegerVariable::toFast()
const {
216 s << name() << domain();
Base class for discrete random variable.
Exception : a similar element already exists.
IntegerVariable(const std::string &aName, const std::string &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