aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
integerVariable_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 <algorithm>
50#include <ostream>
51#include <string>
52
53#include <agrum/agrum.h>
54
57
58// to ease IDE parsers
60
61#ifndef DOXYGEN_SHOULD_SKIP_THIS
62
63namespace gum {
64
66 INLINE IntegerVariable::IntegerVariable(std::string_view aName, std::string_view aDesc) :
67 DiscreteVariable(aName, aDesc) {
68 // for debugging purposes
69 GUM_CONSTRUCTOR(IntegerVariable);
70 }
71
73 INLINE IntegerVariable::IntegerVariable(const IntegerVariable& from) :
74 DiscreteVariable(from), _domain_(from._domain_) {
75 // for debugging purposes
76 GUM_CONS_CPY(IntegerVariable);
77 }
78
80 INLINE IntegerVariable::IntegerVariable(IntegerVariable&& from) noexcept :
81 DiscreteVariable(std::move(from)), _domain_(std::move(from._domain_)) {
82 from._domain_.clear();
83 // for debugging purposes
84 GUM_CONS_MOV(IntegerVariable);
85 }
86
88 INLINE IntegerVariable* IntegerVariable::clone() const { return new IntegerVariable(*this); }
89
91 INLINE IntegerVariable::~IntegerVariable() { GUM_DESTRUCTOR(IntegerVariable); }
92
94 INLINE IntegerVariable& IntegerVariable::operator=(const IntegerVariable& from) {
95 // avoid self assignment
96 if (&from != this) {
97 DiscreteVariable::operator=(from);
98 _domain_ = from._domain_;
99 }
100
101 return *this;
102 }
103
105 INLINE IntegerVariable& IntegerVariable::operator=(IntegerVariable&& from) {
106 // avoid self assignment
107 if (&from != this) {
108 DiscreteVariable::operator=(std::move(from));
109 _domain_ = std::move(from._domain_);
110 from._domain_.clear();
111 }
112
113 return *this;
114 }
115
117 INLINE Size IntegerVariable::domainSize() const { return _domain_.size(); }
118
120 INLINE VarType IntegerVariable::varType() const { return VarType::INTEGER; }
121
123 INLINE Idx IntegerVariable::index(std::string_view aLabel) const {
124 int x;
125 try {
126 x = std::stoi(std::string{aLabel});
127 } catch (const std::exception&) {
128 GUM_ERROR(NotFound, "label '" << aLabel << "' is unknown in " << toString());
129 }
130 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
131
132 if (ind != _domain_.size() && _domain_[ind] == x) {
133 return ind;
134 } else {
135 GUM_ERROR(NotFound, "label '" << aLabel << "' is unknown in " << toString());
136 }
137 }
138
139 INLINE Idx IntegerVariable::closestIndex(double val) const {
140 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
141
142 if (ind == _domain_.size()) return _domain_.size() - 1;
143 if (ind == 0) return 0;
144
145 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
146 return ind;
147 } else {
148 return ind - 1;
149 }
150 }
151
153 INLINE std::string IntegerVariable::label(Idx i) const {
154 // note that if i is outside the domain, Sequence _domain_ will raise
155 // an exception
156 if (i < 0 || i >= _domain_.size())
157 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".")
158 return std::to_string(_domain_[i]);
159 }
160
162 INLINE double IntegerVariable::numerical(Idx i) const {
163 if (i < 0 || i >= _domain_.size())
164 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".")
165 return double(_domain_[i]);
166 }
167
169 INLINE const std::vector< int >& IntegerVariable::integerDomain() const { return _domain_; }
170
171 INLINE bool IntegerVariable::isValue(int value) const {
172 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
173 return (ind != _domain_.size() && _domain_[ind] == value);
174 }
175
177 INLINE void IntegerVariable::changeValue(int old_value, int new_value) {
178 if (!isValue(old_value)) return;
179 if (isValue(new_value)) {
181 "Value" << new_value << " already belongs to the domain of the variable");
182 }
183
184 eraseValue(old_value);
185 addValue(new_value);
186 }
187
189 INLINE void IntegerVariable::eraseValue(int value) {
190 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
191 if (ind != _domain_.size() && _domain_[ind] == value) {
192 _domain_.erase(_domain_.begin() + ind);
193 }
194 }
195
197 INLINE void IntegerVariable::eraseValues() { _domain_.clear(); }
198
199 INLINE bool IntegerVariable::_checkSameDomain_(const gum::Variable& aRV) const {
200 // we can assume that aRV is a IntegerVariable
201 const auto& cv = static_cast< const IntegerVariable& >(aRV);
202 if (domainSize() != cv.domainSize()) return false;
203 return cv._domain_ == _domain_;
204 }
205
206 INLINE void IntegerVariable::addValue(int value) {
207 if (isValue(value)) {
209 "Value " << value << " already belongs to the domain of the variable");
210 }
211 _domain_.push_back(value);
212 std::sort(_domain_.begin(), _domain_.end());
213 }
214
215 INLINE std::string IntegerVariable::closestLabel(double val) const {
216 return label(closestIndex(val));
217 }
218
219 INLINE std::string IntegerVariable::toFast() const {
220 return std::format("{}{}", name(), domain());
221 }
222
223 INLINE std::string IntegerVariable::stype() const { return "Integer"; }
224} /* namespace gum */
225
226#endif /* DOXYGEN SHOULD SKIP THIS */
Base class for discrete random variable.
Exception : a similar element already exists.
IntegerVariable(std::string_view aName, std::string_view aDesc="")
constructor
Exception : the element we looked for cannot be found.
Exception : out of bound.
Base class for every random variable.
Definition variable.h:81
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Class hash tables iterators.
Base class for integer discrete random variables.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:62
STL namespace.