aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator_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
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
52
53namespace gum {
54
55 namespace learning {
56
59
61 INLINE bool DBTranslator::isLossless() const { return is_lossless_; }
62
65 const bool is_lossless,
66 const std::vector< std::string >& missing_symbols,
67 const bool dynamic_dictionary,
68 std::size_t max_dico_entries) :
69 is_lossless_(is_lossless), is_dictionary_dynamic_(dynamic_dictionary),
70 max_dico_entries_(max_dico_entries), val_type_(val_type) {
71 const std::size_t size = missing_symbols.size();
72
73 if (size) {
74 // save the set of symbols representing the missing values
75 missing_symbols_.resize((Size)missing_symbols.size());
76 for (const auto& symbol: missing_symbols) {
77 missing_symbols_.insert(symbol);
78 }
79 }
80
81 GUM_CONSTRUCTOR(DBTranslator);
82 }
83
85 INLINE DBTranslator::DBTranslator(DBTranslatedValueType val_type,
86 const bool is_lossless,
87 const bool dynamic_dictionary,
88 std::size_t max_dico_entries) :
89 is_lossless_(is_lossless), is_dictionary_dynamic_(dynamic_dictionary),
90 max_dico_entries_(max_dico_entries), val_type_(val_type) {
91 GUM_CONSTRUCTOR(DBTranslator);
92 }
93
95 INLINE DBTranslator::DBTranslator(const DBTranslator& from) :
96 is_lossless_(from.is_lossless_), is_dictionary_dynamic_(from.is_dictionary_dynamic_),
97 max_dico_entries_(from.max_dico_entries_), missing_symbols_(from.missing_symbols_),
98 back_dico_(from.back_dico_), val_type_(from.val_type_) {
99 GUM_CONS_CPY(DBTranslator);
100 }
101
103 INLINE DBTranslator::DBTranslator(DBTranslator&& from) :
104 is_lossless_(from.is_lossless_), is_dictionary_dynamic_(from.is_dictionary_dynamic_),
105 max_dico_entries_(from.max_dico_entries_),
106 missing_symbols_(std::move(from.missing_symbols_)), back_dico_(std::move(from.back_dico_)),
107 val_type_(from.val_type_) {
108 GUM_CONS_MOV(DBTranslator);
109 }
110
112 INLINE DBTranslator::~DBTranslator() { GUM_DESTRUCTOR(DBTranslator); }
113
115 INLINE DBTranslator& DBTranslator::operator=(const DBTranslator& from) {
116 if (this != &from) {
117 is_lossless_ = from.is_lossless_;
118 is_dictionary_dynamic_ = from.is_dictionary_dynamic_;
119 max_dico_entries_ = from.max_dico_entries_;
120 missing_symbols_ = from.missing_symbols_;
121 back_dico_ = from.back_dico_;
122 val_type_ = from.val_type_;
123 }
124 return *this;
125 }
126
128 INLINE DBTranslator& DBTranslator::operator=(DBTranslator&& from) {
129 is_lossless_ = from.is_lossless_;
130 is_dictionary_dynamic_ = from.is_dictionary_dynamic_;
131 max_dico_entries_ = from.max_dico_entries_;
132 missing_symbols_ = std::move(from.missing_symbols_);
133 back_dico_ = std::move(from.back_dico_);
134 val_type_ = from.val_type_;
135
136 return *this;
137 }
138
140 INLINE DBTranslatedValue DBTranslator::operator<<(const std::string& str) {
141 return translate(str);
142 }
143
145 INLINE std::string DBTranslator::operator>>(const DBTranslatedValue translated_val) {
146 return translateBack(translated_val);
147 }
148
150 INLINE bool DBTranslator::hasEditableDictionary() const { return is_dictionary_dynamic_; }
151
153 INLINE void DBTranslator::setEditableDictionaryMode(bool new_mode) {
154 is_dictionary_dynamic_ = new_mode;
155 }
156
158 INLINE const Bijection< std::size_t, std::string >& DBTranslator::getDictionary() const {
159 return back_dico_;
160 }
161
163 INLINE const Set< std::string >& DBTranslator::missingSymbols() const {
164 return missing_symbols_;
165 }
166
168 INLINE bool DBTranslator::isMissingSymbol(const std::string& str) const {
169 return missing_symbols_.exists(str);
170 }
171
173 INLINE void DBTranslator::setVariableName(const std::string& str) const {
174 const_cast< Variable* >(this->variable())->setName(str);
175 }
176
178 INLINE void DBTranslator::setVariableDescription(const std::string& str) const {
179 const_cast< Variable* >(this->variable())->setDescription(str);
180 }
181
183 INLINE bool DBTranslator::isMissingValue(const DBTranslatedValue& value) const {
184 switch (val_type_) {
185 case DBTranslatedValueType::DISCRETE :
186 return value.discr_val == std::numeric_limits< std::size_t >::max();
187
188 case DBTranslatedValueType::CONTINUOUS :
189 return value.cont_val == std::numeric_limits< float >::max();
190
191 default :
193 "No missing value interpretation for this "
194 "translated value type");
195 }
196 }
197
198 } /* namespace learning */
199
200} /* namespace gum */
201
202#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The base class for all the tabular databases' cell translators.
Exception : there is something wrong with an implementation.
DBTranslator(DBTranslatedValueType val_type, const bool is_lossless, const std::vector< std::string > &missing_symbols, const bool editable_dictionary=true, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor
bool is_lossless_
indicates whether the translation is lossless (e.g., ranges) or not
DBTranslatedValueType val_type_
the type of the values translated by the translator
bool isLossless() const
returns a Boolean indicating whether the translation is lossless or not
DBTranslatedValueType getValType() const
returns the type of values handled by the translator
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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.