63#ifndef DOXYGEN_SHOULD_SKIP_THIS
69 const std::string& aDesc) :
72 GUM_CONSTRUCTOR(NumericalDiscreteVariable);
77 NumericalDiscreteVariable::NumericalDiscreteVariable(
const NumericalDiscreteVariable& from) :
78 DiscreteVariable(from), _domain_(from._domain_) {
80 GUM_CONS_CPY(NumericalDiscreteVariable);
84 INLINE NumericalDiscreteVariable::NumericalDiscreteVariable(NumericalDiscreteVariable&& from) :
85 DiscreteVariable(
std::move(from)), _domain_(
std::move(from._domain_)) {
86 from._domain_.clear();
88 GUM_CONS_MOV(NumericalDiscreteVariable)
92 INLINE NumericalDiscreteVariable* NumericalDiscreteVariable::clone()
const {
93 return new NumericalDiscreteVariable(*
this);
97 INLINE NumericalDiscreteVariable::~NumericalDiscreteVariable() {
98 GUM_DESTRUCTOR(NumericalDiscreteVariable);
102 INLINE NumericalDiscreteVariable&
103 NumericalDiscreteVariable::operator=(
const NumericalDiscreteVariable& from) {
106 DiscreteVariable::operator=(from);
107 _domain_ = from._domain_;
114 INLINE NumericalDiscreteVariable&
115 NumericalDiscreteVariable::operator=(NumericalDiscreteVariable&& from) {
118 DiscreteVariable::operator=(std::move(from));
119 _domain_ = std::move(from._domain_);
120 from._domain_.clear();
127 INLINE Size NumericalDiscreteVariable::domainSize()
const {
return _domain_.size(); }
130 INLINE
VarType NumericalDiscreteVariable::varType()
const {
return VarType::NUMERICAL; }
133 INLINE Idx NumericalDiscreteVariable::index(
const std::string& aLabel)
const {
134 const auto x = std::stod(aLabel);
135 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
137 if (ind != _domain_.size() && _domain_[ind] == x) {
144 INLINE Idx NumericalDiscreteVariable::closestIndex(
double val)
const {
145 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
147 if (ind == _domain_.size())
return _domain_.size() - 1;
148 if (ind == 0)
return 0;
150 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
158 INLINE std::string NumericalDiscreteVariable::label(Idx i)
const {
161 if (i < 0 || i >= _domain_.size())
163 return _generateLabel_(_domain_[i]);
167 INLINE
double NumericalDiscreteVariable::numerical(Idx i)
const {
168 if (i < 0 || i >= _domain_.size())
174 INLINE
const std::vector< double >& NumericalDiscreteVariable::numericalDomain()
const {
178 INLINE
bool NumericalDiscreteVariable::isValue(
double value)
const {
179 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
180 return (ind != _domain_.size() && _domain_[ind] == value);
184 INLINE
void NumericalDiscreteVariable::changeValue(
double old_value,
double new_value) {
187 "Value '" << new_value <<
"' is not allowed for variable " << name())
189 if (!isValue(old_value))
return;
190 if (isValue(new_value)) {
192 "Value" << new_value <<
" already belongs to the domain of the variable");
194 eraseValue(old_value);
199 INLINE
void NumericalDiscreteVariable::eraseValue(
double value) {
200 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
201 if (ind < _domain_.size() && _domain_[ind] == value) { _domain_.erase(_domain_.begin() + ind); }
205 INLINE
void NumericalDiscreteVariable::eraseValues() { _domain_.clear(); }
207 INLINE
bool NumericalDiscreteVariable::_checkSameDomain_(
const gum::Variable& aRV)
const {
209 const auto& cv =
static_cast< const NumericalDiscreteVariable&
>(aRV);
210 if (domainSize() != cv.domainSize())
return false;
211 return cv._domain_ == _domain_;
214 INLINE std::string NumericalDiscreteVariable::closestLabel(
double val)
const {
215 return label(closestIndex(val));
218 INLINE
void NumericalDiscreteVariable::addValue(
double value) {
221 "Value '" << value <<
"' is not allowed for variable " << toString())
223 if (isValue(value)) {
225 "Value " << value <<
" already belongs to the domain of the variable "
228 _domain_.push_back(value);
229 std::sort(_domain_.begin(), _domain_.end());
232 INLINE std::string NumericalDiscreteVariable::_generateLabel_(
double f)
const {
236 INLINE std::string NumericalDiscreteVariable::toFast()
const {
238 s << name() << domain();
Exception : default in label.
Base class for discrete random variable.
Exception : a similar element already exists.
Exception : the element we looked for cannot be found.
NumericalDiscreteVariable(const std::string &aName, const std::string &aDesc="")
constructor
Exception : out of bound.
Base class for every random variable.
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
std::string compact_tostr(T value)
Returns a path to a unique file name.
Class hash tables iterators.
gum is the global namespace for all aGrUM entities
Base class for numerical discrete random variables.