aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
49#include <ostream>
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(std::string_view aLabel) {
78 _labels_.insert(std::string(aLabel));
79
80 return *this;
81 }
82
83 INLINE void LabelizedVariable::changeLabel(Idx pos, std::string_view 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, std::string{aLabel});
89 }
90
91 // Default constructor
92
93 INLINE LabelizedVariable::LabelizedVariable(std::string_view aName,
94 std::string_view 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 addLabel(std::to_string(i));
102 }
103
104 INLINE
105 LabelizedVariable::LabelizedVariable(std::string_view aName,
106 std::string_view aDesc,
107 const std::vector< std::string >& labels) :
108 DiscreteVariable(aName, aDesc) {
109 // for debugging purposes
110 GUM_CONSTRUCTOR(LabelizedVariable);
111 _labels_.clear();
112 for (Idx i = 0; i < labels.size(); ++i)
113 _labels_.insert(labels[i]);
114 }
115
116 INLINE Idx LabelizedVariable::posLabel(std::string_view label) const {
117 return _labels_.pos(std::string{label});
118 }
119
120 // Copy constructor
121
122 INLINE
123 LabelizedVariable::LabelizedVariable(const LabelizedVariable& aLDRV) :
124 DiscreteVariable(aLDRV), _labels_(aLDRV._labels_) { // for debugging purposes
125 GUM_CONSTRUCTOR(LabelizedVariable);
126 }
127
128 // destructor
129
130 INLINE LabelizedVariable::~LabelizedVariable() {
131 eraseLabels();
132 GUM_DESTRUCTOR(LabelizedVariable);
133 }
134
135 INLINE
136 LabelizedVariable* LabelizedVariable::clone() const { return new LabelizedVariable(*this); }
137
138 // copy operator
139 INLINE LabelizedVariable& LabelizedVariable::operator=(const LabelizedVariable& aLDRV) {
140 // avoid self assignment
141 if (&aLDRV != this) { copy_(aLDRV); }
142
143 return *this;
144 }
145
146 // indicates whether the variable already has the label passed in argument
147 INLINE bool LabelizedVariable::isLabel(std::string_view aLabel) const {
148 return _labels_.exists(std::string{aLabel});
149 }
150
151 // returns the ith label
152 INLINE std::string LabelizedVariable::label(Idx i) const { return _labels_.atPos(i); }
153
154 // get a numerical representation of the indice-th value.
155 INLINE double LabelizedVariable::numerical(Idx indice) const { return double(indice); }
156
158 INLINE Idx LabelizedVariable::closestIndex(double val) const {
159 GUM_ERROR(NotImplementedYet, "closestIndex has no meaning for LabelizedVariable")}
160
161 INLINE Idx LabelizedVariable::index(std::string_view aLabel) const {
162 try {
163 return _labels_.pos(std::string{aLabel});
164 } catch (const gum::NotFound&) {
165 GUM_ERROR(OutOfBounds, "label '" << aLabel << "' is unknown in " << this->toString())
166 }
167 }
168
169 // returns the size of the random discrete variable domain
170 INLINE Size LabelizedVariable::domainSize() const { return _labels_.size(); }
171
172 INLINE VarType LabelizedVariable::varType() const { return VarType::LABELIZED; }
173
174 INLINE std::string LabelizedVariable::toFast() const { return name() + domain(); }
175
176 INLINE bool LabelizedVariable::_checkSameDomain_(const gum::Variable& aRV) const {
177 // we can assume that aRV is a LabelizedVariable
178 // we ask for a strict equality, label by label
179 const auto& cv = static_cast< const LabelizedVariable& >(aRV);
180 if (domainSize() != cv.domainSize()) return false;
181 for (Idx i = 0; i < domainSize(); ++i)
182 if (label(i) != cv.label(i)) return false;
183 return true;
184 }
185
186 INLINE std::string LabelizedVariable::stype() const { return "Labelized"; }
187
188 INLINE LabelizedVariable::LabelizedVariable() { GUM_CONSTRUCTOR(LabelizedVariable) }
189} /* namespace gum */
190
191#endif /* DOXYGEN SHOULD SKIP THIS */
Base class for discrete random variable.
class LabelizedVariable
void eraseLabels()
erase all the labels
void changeLabel(Idx pos, std::string_view aLabel) const
change a label for this index
LabelizedVariable & addLabel(std::string_view aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
void copy_(const LabelizedVariable &aLDRV)
copies the content of aLDRV
bool isLabel(std::string_view aLabel) const
indicates whether the variable already has the label passed in argument
LabelizedVariable()
(protected) Default constructor
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:81
void copy_(const Variable &aRV)
protected copy
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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:62