60#ifndef DOXYGEN_SHOULD_SKIP_THIS
78 _labels_.insert(std::string(aLabel));
86 if (
isLabel(aLabel))
GUM_ERROR(DuplicateElement,
"Label '" << aLabel <<
"' already exists")
88 _labels_.setAtPos(pos, std::
string{aLabel});
94 std::string_view aDesc,
95 const Size nbrLabel) :
98 GUM_CONSTRUCTOR(LabelizedVariable);
100 for (Idx i = 0; i < nbrLabel; ++i)
101 addLabel(std::to_string(i));
105 LabelizedVariable::LabelizedVariable(std::string_view aName,
106 std::string_view aDesc,
107 const std::vector< std::string >& labels) :
108 DiscreteVariable(aName, aDesc) {
110 GUM_CONSTRUCTOR(LabelizedVariable);
112 for (Idx i = 0; i < labels.size(); ++i)
113 _labels_.insert(labels[i]);
116 INLINE Idx LabelizedVariable::posLabel(std::string_view label)
const {
117 return _labels_.pos(std::string{label});
123 LabelizedVariable::LabelizedVariable(
const LabelizedVariable& aLDRV) :
124 DiscreteVariable(aLDRV), _labels_(aLDRV._labels_) {
125 GUM_CONSTRUCTOR(LabelizedVariable);
130 INLINE LabelizedVariable::~LabelizedVariable() {
132 GUM_DESTRUCTOR(LabelizedVariable);
136 LabelizedVariable* LabelizedVariable::clone()
const {
return new LabelizedVariable(*
this); }
139 INLINE LabelizedVariable& LabelizedVariable::operator=(
const LabelizedVariable& aLDRV) {
141 if (&aLDRV !=
this) { copy_(aLDRV); }
147 INLINE
bool LabelizedVariable::isLabel(std::string_view aLabel)
const {
148 return _labels_.exists(std::string{aLabel});
152 INLINE std::string LabelizedVariable::label(Idx i)
const {
return _labels_.atPos(i); }
155 INLINE
double LabelizedVariable::numerical(Idx indice)
const {
return double(indice); }
158 INLINE Idx LabelizedVariable::closestIndex(
double val)
const {
161 INLINE Idx LabelizedVariable::index(std::string_view aLabel)
const {
163 return _labels_.pos(std::string{aLabel});
164 }
catch (
const gum::NotFound&) {
170 INLINE Size LabelizedVariable::domainSize()
const {
return _labels_.size(); }
172 INLINE
VarType LabelizedVariable::varType()
const {
return VarType::LABELIZED; }
174 INLINE std::string LabelizedVariable::toFast()
const {
return name() + domain(); }
176 INLINE
bool LabelizedVariable::_checkSameDomain_(
const gum::Variable& aRV)
const {
179 const auto& cv =
static_cast< const LabelizedVariable&
>(aRV);
180 if (domainSize() != cv.domainSize())
return false;
181 for (Idx i = 0; i < domainSize(); ++i)
182 if (label(i) != cv.label(i))
return false;
186 INLINE std::string LabelizedVariable::stype()
const {
return "Labelized"; }
188 INLINE LabelizedVariable::LabelizedVariable() { GUM_CONSTRUCTOR(LabelizedVariable) }
Base class for discrete random variable.
void eraseLabels()
erase all the labels
void changeLabel(Idx pos, std::string_view aLabel) const
change a label for this index
LabelizedVariable & addLabel(std::string_view aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
void copy_(const LabelizedVariable &aLDRV)
copies the content of aLDRV
bool isLabel(std::string_view aLabel) const
indicates whether the variable already has the label passed in argument
LabelizedVariable()
(protected) Default constructor
Sequence< std::string > _labels_
the set of labels contained in the variable
Exception : there is something wrong with an implementation.
Exception : out of bound.
Base class for every random variable.
void copy_(const Variable &aRV)
protected copy
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Class hash tables iterators.
Base class for labelized discrete random variables.
gum is the global namespace for all aGrUM entities