aGrUM 2.3.2
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-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 <algorithm>
49#include <ostream>
50#include <sstream>
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(const std::string& aName, const std::string& 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(const std::string& aLabel) const {
124 const auto x = std::stoi(aLabel);
125 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), x) - _domain_.begin();
126
127 if (ind != _domain_.size() && _domain_[ind] == x) {
128 return ind;
129 } else {
130 GUM_ERROR(NotFound, "label '" << aLabel << "' is unknown in " << toString());
131 }
132 }
133
134 INLINE Idx IntegerVariable::closestIndex(double val) const {
135 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), val) - _domain_.begin();
136
137 if (ind == _domain_.size()) return _domain_.size() - 1;
138 if (ind == 0) return 0;
139
140 if (_domain_[ind] - val < val - _domain_[ind - 1]) {
141 return ind;
142 } else {
143 return ind - 1;
144 }
145 }
146
148 INLINE std::string IntegerVariable::label(Idx i) const {
149 // note that if i is outside the domain, Sequence _domain_ will raise
150 // an exception
151 if (i < 0 || i >= _domain_.size())
152 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".")
153 return std::to_string(_domain_[i]);
154 }
155
157 INLINE double IntegerVariable::numerical(Idx i) const {
158 if (i < 0 || i >= _domain_.size())
159 GUM_ERROR(OutOfBounds, "Index out of bounds : " << i << "for variable " << toString() << ".")
160 return double(_domain_[i]);
161 }
162
164 INLINE const std::vector< int >& IntegerVariable::integerDomain() const { return _domain_; }
165
166 INLINE bool IntegerVariable::isValue(int value) const {
167 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
168 return (ind != _domain_.size() && _domain_[ind] == value);
169 }
170
172 INLINE void IntegerVariable::changeValue(int old_value, int new_value) {
173 if (!isValue(old_value)) return;
174 if (isValue(new_value)) {
176 "Value" << new_value << " already belongs to the domain of the variable");
177 }
178
179 eraseValue(old_value);
180 addValue(new_value);
181 }
182
184 INLINE void IntegerVariable::eraseValue(int value) {
185 const Idx ind = std::lower_bound(_domain_.begin(), _domain_.end(), value) - _domain_.begin();
186 if (ind != _domain_.size() && _domain_[ind] == value) {
187 _domain_.erase(_domain_.begin() + ind);
188 }
189 }
190
192 INLINE void IntegerVariable::eraseValues() { _domain_.clear(); }
193
194 INLINE bool IntegerVariable::_checkSameDomain_(const gum::Variable& aRV) const {
195 // we can assume that aRV is a IntegerVariable
196 const auto& cv = static_cast< const IntegerVariable& >(aRV);
197 if (domainSize() != cv.domainSize()) return false;
198 return cv._domain_ == _domain_;
199 }
200
201 INLINE void IntegerVariable::addValue(int value) {
202 if (isValue(value)) {
204 "Value " << value << " already belongs to the domain of the variable");
205 }
206 _domain_.push_back(value);
207 std::sort(_domain_.begin(), _domain_.end());
208 }
209
210 INLINE std::string IntegerVariable::closestLabel(double val) const {
211 return label(closestIndex(val));
212 }
213
214 INLINE std::string IntegerVariable::toFast() const {
215 std::stringstream s;
216 s << name() << domain();
217 return s.str();
218 }
219} /* namespace gum */
220
221#endif /* DOXYGEN SHOULD SKIP THIS */
Base class for discrete random variable.
Exception : a similar element already exists.
IntegerVariable(const std::string &aName, const std::string &aDesc="")
constructor
Exception : the element we looked for cannot be found.
Exception : out of bound.
Base class for every random variable.
Definition variable.h:79
Base class for discrete random variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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:60
STL namespace.