aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
labelizedVariable_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
48#include <ostream>
49#include <sstream>
50#include <string>
51
52#include <agrum/agrum.h>
53
56
57// to ease IDE parsers
59
60#ifndef DOXYGEN_SHOULD_SKIP_THIS
61
62namespace gum {
63
64 // erase all the labels
65
66 INLINE void LabelizedVariable::eraseLabels() { _labels_.clear(); }
67
68 // copies the content of aLDRV
69
70 INLINE void LabelizedVariable::copy_(const LabelizedVariable& aLDRV) {
72 _labels_.clear();
73 _labels_ = aLDRV._labels_;
74 }
75
76 // add a label with a new index (we assume that we will NEVER remove a label)
77 INLINE LabelizedVariable& LabelizedVariable::addLabel(const std::string& aLabel) {
78 _labels_.insert(aLabel);
79
80 return *this;
81 }
82
83 INLINE void LabelizedVariable::changeLabel(Idx pos, const std::string& aLabel) const {
84 if (_labels_[pos] == aLabel) return;
85
86 if (isLabel(aLabel)) GUM_ERROR(DuplicateElement, "Label '" << aLabel << "' already exists")
87
88 _labels_.setAtPos(pos, aLabel);
89 }
90
91 // Default constructor
92
93 INLINE LabelizedVariable::LabelizedVariable(const std::string& aName,
94 const std::string& aDesc,
95 const Size nbrLabel) :
96 DiscreteVariable(aName, aDesc) {
97 // for debugging purposes
98 GUM_CONSTRUCTOR(LabelizedVariable);
99
100 for (Idx i = 0; i < nbrLabel; ++i) {
101 std::ostringstream oss;
102 oss << i;
103 addLabel(oss.str());
104 }
105 }
106
107 INLINE
108 LabelizedVariable::LabelizedVariable(const std::string& aName,
109 const std::string& aDesc,
110 const std::vector< std::string >& labels) :
111 DiscreteVariable(aName, aDesc) {
112 // for debugging purposes
113 GUM_CONSTRUCTOR(LabelizedVariable);
114 _labels_.clear();
115 for (Idx i = 0; i < labels.size(); ++i)
116 _labels_.insert(labels[i]);
117 }
118
119 INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
120 return _labels_.pos(label);
121 }
122
123 // Copy constructor
124
125 INLINE
126 LabelizedVariable::LabelizedVariable(const LabelizedVariable& aLDRV) :
127 DiscreteVariable(aLDRV), _labels_(aLDRV._labels_) { // for debugging purposes
128 GUM_CONSTRUCTOR(LabelizedVariable);
129 }
130
131 // destructor
132
133 INLINE LabelizedVariable::~LabelizedVariable() {
134 eraseLabels();
135 GUM_DESTRUCTOR(LabelizedVariable);
136 }
137
138 INLINE
139 LabelizedVariable* LabelizedVariable::clone() const { return new LabelizedVariable(*this); }
140
141 // copy operator
142 INLINE LabelizedVariable& LabelizedVariable::operator=(const LabelizedVariable& aLDRV) {
143 // avoid self assignment
144 if (&aLDRV != this) { copy_(aLDRV); }
145
146 return *this;
147 }
148
149 // indicates whether the variable already has the label passed in argument
150 INLINE bool LabelizedVariable::isLabel(const std::string& aLabel) const {
151 return _labels_.exists(aLabel);
152 }
153
154 // returns the ith label
155 INLINE std::string LabelizedVariable::label(Idx i) const { return _labels_.atPos(i); }
156
157 // get a numerical representation of the indice-th value.
158 INLINE double LabelizedVariable::numerical(Idx indice) const { return double(indice); }
159
161 INLINE Idx LabelizedVariable::closestIndex(double val) const {
162 GUM_ERROR(NotImplementedYet, "closestIndex has no meaning for LabelizedVariable")}
163
164 INLINE Idx LabelizedVariable::index(const std::string& aLabel) const {
165 try {
166 return _labels_.pos(aLabel);
167 } catch (...) {
168 GUM_ERROR(OutOfBounds, "label '" << aLabel << "' is unknown in " << this->toString())
169 }
170 }
171
172 // returns the size of the random discrete variable domain
173 INLINE Size LabelizedVariable::domainSize() const { return _labels_.size(); }
174
175 INLINE VarType LabelizedVariable::varType() const { return VarType::LABELIZED; }
176
177 INLINE std::string LabelizedVariable::toFast() const {
178 std::stringstream s;
179 s << name() << domain();
180 return s.str();
181 }
182
183 INLINE bool LabelizedVariable::_checkSameDomain_(const gum::Variable& aRV) const {
184 // we can assume that aRV is a LabelizedVariable
185 // we ask for a strict equality, label by label
186 const auto& cv = static_cast< const LabelizedVariable& >(aRV);
187 if (domainSize() != cv.domainSize()) return false;
188 for (Idx i = 0; i < domainSize(); ++i)
189 if (label(i) != cv.label(i)) return false;
190 return true;
191 }
192
193} /* namespace gum */
194
195#endif /* DOXYGEN SHOULD SKIP THIS */
DiscreteVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
std::vector< std::string > labels() const
vector of labels
class LabelizedVariable
void eraseLabels()
erase all the labels
bool isLabel(const std::string &aLabel) const
indicates whether the variable already has the label passed in argument
LabelizedVariable(const std::string &aName, const std::string &aDesc="", const Size nbrLabel=2)
constructor
void copy_(const LabelizedVariable &aLDRV)
copies the content of aLDRV
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
LabelizedVariable()
(protected) Default constructor
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
Sequence< std::string > _labels_
the set of labels contained in the variable
Exception : there is something wrong with an implementation.
Exception : out of bound.
Base class for every random variable.
Definition variable.h:79
void copy_(const Variable &aRV)
protected copy
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Class hash tables iterators.
Base class for labelized discrete random variables.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:60