aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4IntegerVariable.cpp
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
41
47
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
53# ifdef GUM_NO_INLINE
55# endif /* GUM_NO_INLINE */
56
57namespace gum {
58
59 namespace learning {
60
63 const IntegerVariable& var,
64 const std::vector< std::string >& missing_symbols,
65 std::size_t max_dico_entries) :
67 true,
68 missing_symbols,
69 false,
70 max_dico_entries),
71 _variable_(var) {
72 // check that the variable has not too many entries
73 if (var.domainSize() > max_dico_entries) {
74 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
75 }
76
77 // remove all the missing symbols corresponding to a number belonging to the variable
78 for (auto iter = this->missing_symbols_.beginSafe(); iter != this->missing_symbols_.endSafe();
79 ++iter) {
80 if (DBCell::isInteger(*iter)) {
81 const int missing_val = std::stoi(*iter);
82 if (_variable_.isValue(missing_val)) { this->missing_symbols_.erase(iter); }
83 }
84 }
85
86 // add the content of the variable into the back dictionary
87 std::size_t size = 0;
88 for (const auto& label: var.labels()) {
89 // if the label corresponds to a missing value, then remove it from
90 // the set of missing symbols. If, in addition, it has already
91 // been entered into the back_dictionary, then, this has been done
92 // because the label corresponded to a missing value, so we should
93 // remove the label as well from the back_dictionary.
94 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
95
96 this->back_dico_.insert(size, label);
97 ++size;
98 }
99
100 // for debugging purposes
101 GUM_CONSTRUCTOR(DBTranslator4IntegerVariable);
102 }
103
105 DBTranslator4IntegerVariable::DBTranslator4IntegerVariable(const IntegerVariable& var,
106 std::size_t max_dico_entries) :
107 DBTranslator(DBTranslatedValueType::DISCRETE, true, false, max_dico_entries),
108 _variable_(var) {
109 // check that the variable has not too many entries
110 if (var.domainSize() > max_dico_entries) {
111 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
112 }
113
114 // add the content of the variable into the back dictionary
115 std::size_t size = 0;
116 for (const auto& label: var.labels()) {
117 this->back_dico_.insert(size, label);
118 ++size;
119 }
120
121 // for debugging purposes
122 GUM_CONSTRUCTOR(DBTranslator4IntegerVariable);
123 }
124
126 DBTranslator4IntegerVariable::DBTranslator4IntegerVariable(
127 const DBTranslator4IntegerVariable& from) :
128 DBTranslator(from), _variable_(from._variable_) {
129 // for debugging purposes
130 GUM_CONS_CPY(DBTranslator4IntegerVariable);
131 }
132
134 DBTranslator4IntegerVariable::DBTranslator4IntegerVariable(
135 DBTranslator4IntegerVariable&& from) :
136 DBTranslator(std::move(from)), _variable_(std::move(from._variable_)) {
137 // for debugging purposes
138 GUM_CONS_MOV(DBTranslator4IntegerVariable);
139 }
140
142 DBTranslator4IntegerVariable* DBTranslator4IntegerVariable::clone() const {
143 return new DBTranslator4IntegerVariable(*this);
144 }
145
147 DBTranslator4IntegerVariable&
148 DBTranslator4IntegerVariable::operator=(const DBTranslator4IntegerVariable& from) {
149 if (this != &from) {
150 DBTranslator::operator=(from);
151 _variable_ = from._variable_;
152 }
153
154 return *this;
155 }
156
158 DBTranslator4IntegerVariable&
159 DBTranslator4IntegerVariable::operator=(DBTranslator4IntegerVariable&& from) {
160 if (this != &from) {
161 DBTranslator::operator=(std::move(from));
162 _variable_ = std::move(from._variable_);
163 }
164
165 return *this;
166 }
167
168 } /* namespace learning */
169
170} /* namespace gum */
171
172#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The databases' cell translators for integer variables.
The databases' cell translators for integer variables.
DBTranslator4IntegerVariable(const IntegerVariable &var, const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor with an integer variable as translator
The base class for all the tabular database cell translators.
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.