aGrUM  0.14.2
labelizedVariable_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #include <ostream>
26 #include <sstream>
27 #include <string>
28 
29 #include <agrum/agrum.h>
30 
31 #include <agrum/core/hashTable.h>
33 
34 // to ease IDE parsers
36 
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 
39 namespace gum {
40 
41  // erase all the labels
42 
43  INLINE void LabelizedVariable::eraseLabels() { __labels.clear(); }
44 
45  // copies the content of aLDRV
46 
47  INLINE void LabelizedVariable::_copy(const LabelizedVariable& aLDRV) {
49  __labels.clear();
50  __labels = aLDRV.__labels;
51  }
52 
53  // add a label with a new index (we assume that we will NEVER remove a label)
54  INLINE LabelizedVariable&
55  LabelizedVariable::addLabel(const std::string& aLabel) {
56  __labels.insert(aLabel);
57 
58  return *this;
59  }
60 
61  INLINE void LabelizedVariable::changeLabel(Idx pos,
62  const std::string& aLabel) const {
63  if (__labels[pos] == aLabel) return;
64 
65  if (isLabel(aLabel))
66  GUM_ERROR(DuplicateElement, "Label '" << aLabel << "' already exists");
67 
68  __labels.setAtPos(pos, aLabel);
69  }
70 
71  // Default constructor
72 
73  INLINE LabelizedVariable::LabelizedVariable(const std::string& aName,
74  const std::string& aDesc,
75  const Size nbrLabel) :
76  DiscreteVariable(aName, aDesc) {
77  // for debugging purposes
78  GUM_CONSTRUCTOR(LabelizedVariable);
79 
80  for (Idx i = 0; i < nbrLabel; ++i) {
81  std::ostringstream oss;
82  oss << i;
83  addLabel(oss.str());
84  }
85  }
86  INLINE
87  LabelizedVariable::LabelizedVariable(const std::string& aName,
88  const std::string& aDesc,
89  const std::vector< std::string >& labels) :
90  DiscreteVariable(aName, aDesc) {
91  // for debugging purposes
92  GUM_CONSTRUCTOR(LabelizedVariable);
93  __labels.clear();
94  for (Idx i = 0; i < labels.size(); ++i)
95  __labels.insert(labels[i]);
96  }
97 
98  INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
99  return __labels.pos(label);
100  }
101 
102  // Copy constructor
103 
104  INLINE
106  DiscreteVariable(aLDRV), __labels(aLDRV.__labels) {
107  // for debugging purposes
108  GUM_CONSTRUCTOR(LabelizedVariable);
109  }
110 
111  // destructor
112 
114  eraseLabels();
115  GUM_DESTRUCTOR(LabelizedVariable);
116  }
117 
118  INLINE
120  return new LabelizedVariable(*this);
121  }
122 
123  // copy operator
125  operator=(const LabelizedVariable& aLDRV) {
126  // avoid self assignment
127  if (&aLDRV != this) { _copy(aLDRV); }
128 
129  return *this;
130  }
131 
132  // indicates whether the variable already has the label passed in argument
133  INLINE bool LabelizedVariable::isLabel(const std::string& aLabel) const {
134  return __labels.exists(aLabel);
135  }
136 
137  // returns the ith label
138  INLINE std::string LabelizedVariable::label(Idx i) const {
139  return __labels.atPos(i);
140  }
141 
142  // get a numerical representation of the indice-th value.
143  INLINE double LabelizedVariable::numerical(Idx indice) const {
144  return double(indice);
145  }
146 
147 
148  INLINE Idx LabelizedVariable::index(const std::string& aLabel) const {
149  try {
150  return __labels.pos(aLabel);
151  } catch (...) {
152  GUM_ERROR(OutOfBounds,
153  "label '" << aLabel << "' is unknown in " << this->toString());
154  }
155  }
156 
157  // returns the size of the random discrete variable domain
158  INLINE Size LabelizedVariable::domainSize() const { return __labels.size(); }
159 
160  INLINE VarType LabelizedVariable::varType() const { return VarType::Labelized; }
161 
162 } /* namespace gum */
163 
164 #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:515
void _copy(const Variable &aRV)
protected copy
void clear()
Clear the sequence.
Definition: sequence_tpl.h:268
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
Base class for discrete random variable.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:35
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
gum is the global namespace for all aGrUM entities
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:38
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:399
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:45
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
Base class for labelized discrete random variables.
void setAtPos(Idx i, const Key &newKey)
Change the value.
Definition: sequence_tpl.h:522
Class hash tables iterators.
virtual Idx index(const std::string &label) const
returns the index of a given label
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
const Key & atPos(Idx i) const
Returns the object at the pos i.
Definition: sequence_tpl.h:497
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405