31 #include <agrum/agrum.h> 33 #include <agrum/tools/core/hashTable.h> 34 #include <agrum/tools/variables/discreteVariable.h> 37 #include <agrum/tools/variables/labelizedVariable.h> 39 #ifndef DOXYGEN_SHOULD_SKIP_THIS 45 INLINE
void LabelizedVariable::eraseLabels() { _labels_.clear(); }
49 INLINE
void LabelizedVariable::copy_(
const LabelizedVariable& aLDRV) {
50 DiscreteVariable::copy_(aLDRV);
52 _labels_ = aLDRV._labels_;
56 INLINE LabelizedVariable& LabelizedVariable::addLabel(
const std::string& aLabel) {
57 _labels_.insert(aLabel);
62 INLINE
void LabelizedVariable::changeLabel(Idx pos,
const std::string& aLabel)
const {
63 if (_labels_[pos] == aLabel)
return;
65 if (isLabel(aLabel)) GUM_ERROR(DuplicateElement,
"Label '" << aLabel <<
"' already exists")
67 _labels_.setAtPos(pos, aLabel);
72 INLINE LabelizedVariable::LabelizedVariable(
const std::string& aName,
73 const std::string& aDesc,
74 const Size nbrLabel) :
75 DiscreteVariable(aName, aDesc) {
77 GUM_CONSTRUCTOR(LabelizedVariable);
79 for (Idx i = 0; i < nbrLabel; ++i) {
80 std::ostringstream oss;
86 LabelizedVariable::LabelizedVariable(
const std::string& aName,
87 const std::string& aDesc,
88 const std::vector< std::string >& labels) :
89 DiscreteVariable(aName, aDesc) {
91 GUM_CONSTRUCTOR(LabelizedVariable);
93 for (Idx i = 0; i < labels.size(); ++i)
94 _labels_.insert(labels[i]);
97 INLINE Idx LabelizedVariable::posLabel(
const std::string& label)
const {
98 return _labels_.pos(label);
104 LabelizedVariable::LabelizedVariable(
const LabelizedVariable& aLDRV) :
105 DiscreteVariable(aLDRV), _labels_(aLDRV._labels_) {
106 GUM_CONSTRUCTOR(LabelizedVariable);
111 INLINE LabelizedVariable::~LabelizedVariable() {
113 GUM_DESTRUCTOR(LabelizedVariable);
117 LabelizedVariable* LabelizedVariable::clone()
const {
return new LabelizedVariable(*
this); }
120 INLINE LabelizedVariable& LabelizedVariable::operator=(
const LabelizedVariable& aLDRV) {
122 if (&aLDRV !=
this) { copy_(aLDRV); }
128 INLINE
bool LabelizedVariable::isLabel(
const std::string& aLabel)
const {
129 return _labels_.exists(aLabel);
133 INLINE std::string LabelizedVariable::label(Idx i)
const {
return _labels_.atPos(i); }
136 INLINE
double LabelizedVariable::numerical(Idx indice)
const {
return double(indice); }
139 INLINE Idx LabelizedVariable::index(
const std::string& aLabel)
const {
141 return _labels_.pos(aLabel);
143 GUM_ERROR(OutOfBounds,
"label '" << aLabel <<
"' is unknown in " <<
this->toString())
148 INLINE Size LabelizedVariable::domainSize()
const {
return _labels_.size(); }
150 INLINE VarType LabelizedVariable::varType()
const {
return VarType::Labelized; }