aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
labelizedVariable_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief Base class for labelized discrete random variables
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #include <ostream>
28 #include <sstream>
29 #include <string>
30 
31 #include <agrum/agrum.h>
32 
33 #include <agrum/tools/core/hashTable.h>
34 #include <agrum/tools/variables/discreteVariable.h>
35 
36 // to ease IDE parsers
37 #include <agrum/tools/variables/labelizedVariable.h>
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 
41 namespace gum {
42 
43  // erase all the labels
44 
45  INLINE void LabelizedVariable::eraseLabels() { labels__.clear(); }
46 
47  // copies the content of aLDRV
48 
49  INLINE void LabelizedVariable::copy_(const LabelizedVariable& aLDRV) {
50  DiscreteVariable::copy_(aLDRV);
51  labels__.clear();
52  labels__ = aLDRV.labels__;
53  }
54 
55  // add a label with a new index (we assume that we will NEVER remove a label)
56  INLINE LabelizedVariable&
57  LabelizedVariable::addLabel(const std::string& aLabel) {
58  labels__.insert(aLabel);
59 
60  return *this;
61  }
62 
63  INLINE void LabelizedVariable::changeLabel(Idx pos,
64  const std::string& aLabel) const {
65  if (labels__[pos] == aLabel) return;
66 
67  if (isLabel(aLabel))
68  GUM_ERROR(DuplicateElement, "Label '" << aLabel << "' already exists");
69 
70  labels__.setAtPos(pos, aLabel);
71  }
72 
73  // Default constructor
74 
75  INLINE LabelizedVariable::LabelizedVariable(const std::string& aName,
76  const std::string& aDesc,
77  const Size nbrLabel) :
78  DiscreteVariable(aName, aDesc) {
79  // for debugging purposes
80  GUM_CONSTRUCTOR(LabelizedVariable);
81 
82  for (Idx i = 0; i < nbrLabel; ++i) {
83  std::ostringstream oss;
84  oss << i;
85  addLabel(oss.str());
86  }
87  }
88  INLINE
89  LabelizedVariable::LabelizedVariable(const std::string& aName,
90  const std::string& aDesc,
91  const std::vector< std::string >& labels) :
92  DiscreteVariable(aName, aDesc) {
93  // for debugging purposes
94  GUM_CONSTRUCTOR(LabelizedVariable);
95  labels__.clear();
96  for (Idx i = 0; i < labels.size(); ++i)
97  labels__.insert(labels[i]);
98  }
99 
100  INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
101  return labels__.pos(label);
102  }
103 
104  // Copy constructor
105 
106  INLINE
107  LabelizedVariable::LabelizedVariable(const LabelizedVariable& aLDRV) :
108  DiscreteVariable(aLDRV), labels__(aLDRV.labels__) {
109  // for debugging purposes
110  GUM_CONSTRUCTOR(LabelizedVariable);
111  }
112 
113  // destructor
114 
115  INLINE LabelizedVariable::~LabelizedVariable() {
116  eraseLabels();
117  GUM_DESTRUCTOR(LabelizedVariable);
118  }
119 
120  INLINE
121  LabelizedVariable* LabelizedVariable::clone() const {
122  return new LabelizedVariable(*this);
123  }
124 
125  // copy operator
126  INLINE LabelizedVariable&
127  LabelizedVariable::operator=(const LabelizedVariable& aLDRV) {
128  // avoid self assignment
129  if (&aLDRV != this) { copy_(aLDRV); }
130 
131  return *this;
132  }
133 
134  // indicates whether the variable already has the label passed in argument
135  INLINE bool LabelizedVariable::isLabel(const std::string& aLabel) const {
136  return labels__.exists(aLabel);
137  }
138 
139  // returns the ith label
140  INLINE std::string LabelizedVariable::label(Idx i) const {
141  return labels__.atPos(i);
142  }
143 
144  // get a numerical representation of the indice-th value.
145  INLINE double LabelizedVariable::numerical(Idx indice) const {
146  return double(indice);
147  }
148 
149 
150  INLINE Idx LabelizedVariable::index(const std::string& aLabel) const {
151  try {
152  return labels__.pos(aLabel);
153  } catch (...) {
154  GUM_ERROR(OutOfBounds,
155  "label '" << aLabel << "' is unknown in " << this->toString());
156  }
157  }
158 
159  // returns the size of the random discrete variable domain
160  INLINE Size LabelizedVariable::domainSize() const { return labels__.size(); }
161 
162  INLINE VarType LabelizedVariable::varType() const { return VarType::Labelized; }
163 
164 } /* namespace gum */
165 
166 #endif /* DOXYGEN SHOULD SKIP THIS */