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&
57 LabelizedVariable::addLabel(
const std::string& aLabel) {
58 labels__.insert(aLabel);
63 INLINE
void LabelizedVariable::changeLabel(Idx pos,
64 const std::string& aLabel)
const {
65 if (labels__[pos] == aLabel)
return;
68 GUM_ERROR(DuplicateElement,
"Label '" << aLabel <<
"' already exists");
70 labels__.setAtPos(pos, aLabel);
75 INLINE LabelizedVariable::LabelizedVariable(
const std::string& aName,
76 const std::string& aDesc,
77 const Size nbrLabel) :
78 DiscreteVariable(aName, aDesc) {
80 GUM_CONSTRUCTOR(LabelizedVariable);
82 for (Idx i = 0; i < nbrLabel; ++i) {
83 std::ostringstream oss;
89 LabelizedVariable::LabelizedVariable(
const std::string& aName,
90 const std::string& aDesc,
91 const std::vector< std::string >& labels) :
92 DiscreteVariable(aName, aDesc) {
94 GUM_CONSTRUCTOR(LabelizedVariable);
96 for (Idx i = 0; i < labels.size(); ++i)
97 labels__.insert(labels[i]);
100 INLINE Idx LabelizedVariable::posLabel(
const std::string& label)
const {
101 return labels__.pos(label);
107 LabelizedVariable::LabelizedVariable(
const LabelizedVariable& aLDRV) :
108 DiscreteVariable(aLDRV), labels__(aLDRV.labels__) {
110 GUM_CONSTRUCTOR(LabelizedVariable);
115 INLINE LabelizedVariable::~LabelizedVariable() {
117 GUM_DESTRUCTOR(LabelizedVariable);
121 LabelizedVariable* LabelizedVariable::clone()
const {
122 return new LabelizedVariable(*
this);
126 INLINE LabelizedVariable&
127 LabelizedVariable::operator=(
const LabelizedVariable& aLDRV) {
129 if (&aLDRV !=
this) { copy_(aLDRV); }
135 INLINE
bool LabelizedVariable::isLabel(
const std::string& aLabel)
const {
136 return labels__.exists(aLabel);
140 INLINE std::string LabelizedVariable::label(Idx i)
const {
141 return labels__.atPos(i);
145 INLINE
double LabelizedVariable::numerical(Idx indice)
const {
146 return double(indice);
150 INLINE Idx LabelizedVariable::index(
const std::string& aLabel)
const {
152 return labels__.pos(aLabel);
154 GUM_ERROR(OutOfBounds,
155 "label '" << aLabel <<
"' is unknown in " <<
this->toString());
160 INLINE Size LabelizedVariable::domainSize()
const {
return labels__.size(); }
162 INLINE VarType LabelizedVariable::varType()
const {
return VarType::Labelized; }