63#ifndef DOXYGEN_SHOULD_SKIP_THIS
69 std::string_view 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(std::string_view aLabel)
const {
136 x = std::stod(std::string{aLabel});
137 }
catch (
const std::exception&) {
140 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
142 if (ind != _domain_.size() && _domain_[ind] == x) {
149 INLINE Idx NumericalDiscreteVariable::closestIndex(
double val)
const {
150 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
152 if (ind == _domain_.size())
return _domain_.size() - 1;
153 if (ind == 0)
return 0;
155 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
163 INLINE std::string NumericalDiscreteVariable::label(Idx i)
const {
166 if (i < 0 || i >= _domain_.size())
168 return _generateLabel_(_domain_[i]);
172 INLINE
double NumericalDiscreteVariable::numerical(Idx i)
const {
173 if (i < 0 || i >= _domain_.size())
179 INLINE
const std::vector< double >& NumericalDiscreteVariable::numericalDomain()
const {
183 INLINE
bool NumericalDiscreteVariable::isValue(
double value)
const {
184 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
185 return (ind != _domain_.size() && _domain_[ind] == value);
189 INLINE
void NumericalDiscreteVariable::changeValue(
double old_value,
double new_value) {
192 "Value '" << new_value <<
"' is not allowed for variable " << name())
194 if (!isValue(old_value))
return;
195 if (isValue(new_value)) {
197 "Value" << new_value <<
" already belongs to the domain of the variable");
199 eraseValue(old_value);
204 INLINE
void NumericalDiscreteVariable::eraseValue(
double value) {
205 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
206 if (ind < _domain_.size() && _domain_[ind] == value) { _domain_.erase(_domain_.begin() + ind); }
210 INLINE
void NumericalDiscreteVariable::eraseValues() { _domain_.clear(); }
212 INLINE
bool NumericalDiscreteVariable::_checkSameDomain_(
const gum::Variable& aRV)
const {
214 const auto& cv =
static_cast< const NumericalDiscreteVariable&
>(aRV);
215 if (domainSize() != cv.domainSize())
return false;
216 return cv._domain_ == _domain_;
219 INLINE std::string NumericalDiscreteVariable::closestLabel(
double val)
const {
220 return label(closestIndex(val));
223 INLINE
void NumericalDiscreteVariable::addValue(
double value) {
226 "Value '" << value <<
"' is not allowed for variable " << toString())
228 if (isValue(value)) {
230 "Value " << value <<
" already belongs to the domain of the variable "
233 _domain_.push_back(value);
234 std::sort(_domain_.begin(), _domain_.end());
237 INLINE std::string NumericalDiscreteVariable::_generateLabel_(
double f)
const {
241 INLINE std::string NumericalDiscreteVariable::toFast()
const {
242 return std::format(
"{}{}", name(), domain());
245 INLINE std::string NumericalDiscreteVariable::stype()
const {
return "NumericalDiscrete"; }
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(std::string_view aName, std::string_view 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.