aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
labelizedVariable_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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& LabelizedVariable::addLabel(const std::string& aLabel) {
57  _labels_.insert(aLabel);
58 
59  return *this;
60  }
61 
62  INLINE void LabelizedVariable::changeLabel(Idx pos, const std::string& aLabel) const {
63  if (_labels_[pos] == aLabel) return;
64 
65  if (isLabel(aLabel)) GUM_ERROR(DuplicateElement, "Label '" << aLabel << "' already exists")
66 
67  _labels_.setAtPos(pos, aLabel);
68  }
69 
70  // Default constructor
71 
72  INLINE LabelizedVariable::LabelizedVariable(const std::string& aName,
73  const std::string& aDesc,
74  const Size nbrLabel) :
75  DiscreteVariable(aName, aDesc) {
76  // for debugging purposes
77  GUM_CONSTRUCTOR(LabelizedVariable);
78 
79  for (Idx i = 0; i < nbrLabel; ++i) {
80  std::ostringstream oss;
81  oss << i;
82  addLabel(oss.str());
83  }
84  }
85  INLINE
86  LabelizedVariable::LabelizedVariable(const std::string& aName,
87  const std::string& aDesc,
88  const std::vector< std::string >& labels) :
89  DiscreteVariable(aName, aDesc) {
90  // for debugging purposes
91  GUM_CONSTRUCTOR(LabelizedVariable);
92  _labels_.clear();
93  for (Idx i = 0; i < labels.size(); ++i)
94  _labels_.insert(labels[i]);
95  }
96 
97  INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
98  return _labels_.pos(label);
99  }
100 
101  // Copy constructor
102 
103  INLINE
104  LabelizedVariable::LabelizedVariable(const LabelizedVariable& aLDRV) :
105  DiscreteVariable(aLDRV), _labels_(aLDRV._labels_) { // for debugging purposes
106  GUM_CONSTRUCTOR(LabelizedVariable);
107  }
108 
109  // destructor
110 
111  INLINE LabelizedVariable::~LabelizedVariable() {
112  eraseLabels();
113  GUM_DESTRUCTOR(LabelizedVariable);
114  }
115 
116  INLINE
117  LabelizedVariable* LabelizedVariable::clone() const { return new LabelizedVariable(*this); }
118 
119  // copy operator
120  INLINE LabelizedVariable& LabelizedVariable::operator=(const LabelizedVariable& aLDRV) {
121  // avoid self assignment
122  if (&aLDRV != this) { copy_(aLDRV); }
123 
124  return *this;
125  }
126 
127  // indicates whether the variable already has the label passed in argument
128  INLINE bool LabelizedVariable::isLabel(const std::string& aLabel) const {
129  return _labels_.exists(aLabel);
130  }
131 
132  // returns the ith label
133  INLINE std::string LabelizedVariable::label(Idx i) const { return _labels_.atPos(i); }
134 
135  // get a numerical representation of the indice-th value.
136  INLINE double LabelizedVariable::numerical(Idx indice) const { return double(indice); }
137 
138 
139  INLINE Idx LabelizedVariable::index(const std::string& aLabel) const {
140  try {
141  return _labels_.pos(aLabel);
142  } catch (...) {
143  GUM_ERROR(OutOfBounds, "label '" << aLabel << "' is unknown in " << this->toString())
144  }
145  }
146 
147  // returns the size of the random discrete variable domain
148  INLINE Size LabelizedVariable::domainSize() const { return _labels_.size(); }
149 
150  INLINE VarType LabelizedVariable::varType() const { return VarType::Labelized; }
151 
152 } /* namespace gum */
153 
154 #endif /* DOXYGEN SHOULD SKIP THIS */