aGrUM  0.16.0
labelizedVariable_inl.h
Go to the documentation of this file.
1 
28 #include <ostream>
29 #include <sstream>
30 #include <string>
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/core/hashTable.h>
36 
37 // to ease IDE parsers
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42 namespace gum {
43 
44  // erase all the labels
45 
46  INLINE void LabelizedVariable::eraseLabels() { __labels.clear(); }
47 
48  // copies the content of aLDRV
49 
50  INLINE void LabelizedVariable::_copy(const LabelizedVariable& aLDRV) {
52  __labels.clear();
53  __labels = aLDRV.__labels;
54  }
55 
56  // add a label with a new index (we assume that we will NEVER remove a label)
57  INLINE LabelizedVariable&
58  LabelizedVariable::addLabel(const std::string& aLabel) {
59  __labels.insert(aLabel);
60 
61  return *this;
62  }
63 
64  INLINE void LabelizedVariable::changeLabel(Idx pos,
65  const std::string& aLabel) const {
66  if (__labels[pos] == aLabel) return;
67 
68  if (isLabel(aLabel))
69  GUM_ERROR(DuplicateElement, "Label '" << aLabel << "' already exists");
70 
71  __labels.setAtPos(pos, aLabel);
72  }
73 
74  // Default constructor
75 
76  INLINE LabelizedVariable::LabelizedVariable(const std::string& aName,
77  const std::string& aDesc,
78  const Size nbrLabel) :
79  DiscreteVariable(aName, aDesc) {
80  // for debugging purposes
81  GUM_CONSTRUCTOR(LabelizedVariable);
82 
83  for (Idx i = 0; i < nbrLabel; ++i) {
84  std::ostringstream oss;
85  oss << i;
86  addLabel(oss.str());
87  }
88  }
89  INLINE
90  LabelizedVariable::LabelizedVariable(const std::string& aName,
91  const std::string& aDesc,
92  const std::vector< std::string >& labels) :
93  DiscreteVariable(aName, aDesc) {
94  // for debugging purposes
95  GUM_CONSTRUCTOR(LabelizedVariable);
96  __labels.clear();
97  for (Idx i = 0; i < labels.size(); ++i)
98  __labels.insert(labels[i]);
99  }
100 
101  INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
102  return __labels.pos(label);
103  }
104 
105  // Copy constructor
106 
107  INLINE
109  DiscreteVariable(aLDRV), __labels(aLDRV.__labels) {
110  // for debugging purposes
111  GUM_CONSTRUCTOR(LabelizedVariable);
112  }
113 
114  // destructor
115 
117  eraseLabels();
118  GUM_DESTRUCTOR(LabelizedVariable);
119  }
120 
121  INLINE
123  return new LabelizedVariable(*this);
124  }
125 
126  // copy operator
128  operator=(const LabelizedVariable& aLDRV) {
129  // avoid self assignment
130  if (&aLDRV != this) { _copy(aLDRV); }
131 
132  return *this;
133  }
134 
135  // indicates whether the variable already has the label passed in argument
136  INLINE bool LabelizedVariable::isLabel(const std::string& aLabel) const {
137  return __labels.exists(aLabel);
138  }
139 
140  // returns the ith label
141  INLINE std::string LabelizedVariable::label(Idx i) const {
142  return __labels.atPos(i);
143  }
144 
145  // get a numerical representation of the indice-th value.
146  INLINE double LabelizedVariable::numerical(Idx indice) const {
147  return double(indice);
148  }
149 
150 
151  INLINE Idx LabelizedVariable::index(const std::string& aLabel) const {
152  try {
153  return __labels.pos(aLabel);
154  } catch (...) {
155  GUM_ERROR(OutOfBounds,
156  "label '" << aLabel << "' is unknown in " << this->toString());
157  }
158  }
159 
160  // returns the size of the random discrete variable domain
161  INLINE Size LabelizedVariable::domainSize() const { return __labels.size(); }
162 
163  INLINE VarType LabelizedVariable::varType() const { return VarType::Labelized; }
164 
165 } /* namespace gum */
166 
167 #endif /* DOXYGEN SHOULD SKIP THIS */
virtual double numerical(Idx indice) const
get a numerical representation of he indice-the value.
std::vector< std::string > labels() const
vector of labels
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:518
void _copy(const Variable &aRV)
protected copy
void clear()
Clear the sequence.
Definition: sequence_tpl.h:271
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
virtual std::string label(Idx i) const
returns the ith label
virtual VarType varType() const
returns the type of variable
void eraseLabels()
erase all the labels
virtual Size domainSize() const
returns the size of the random discrete variable domain
virtual ~LabelizedVariable()
destructor
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
bool isLabel(const std::string &aLabel) const
indicates whether the variable already has the label passed in argument
void _copy(const LabelizedVariable &aLDRV)
copies the content of aLDRV
Sequence< std::string > __labels
the set of labels contained in the variable
VarType
Definition: variable.h:41
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:402
Idx posLabel(const std::string &label) const
return the pos from label
virtual LabelizedVariable * clone() const
a virtual clone ...
LabelizedVariable & operator=(const LabelizedVariable &aLDRV)
copy operator
LabelizedVariable()
(protected) Default constructor
DiscreteVariable()
(protected) Default constructor
const std::string toString() const
string version of *this
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setAtPos(Idx i, const Key &newKey)
Change the value.
Definition: sequence_tpl.h:525
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual Idx index(const std::string &label) const
returns the index of a given label
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
const Key & atPos(Idx i) const
Returns the object at the pos i.
Definition: sequence_tpl.h:500
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408