aGrUM 3.1.1
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-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
50#include <agrum/base/database/DBTranslator.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54
55namespace gum {
56
57 namespace learning {
58
61
63 INLINE bool DBTranslator::isLossless() const { return is_lossless_; }
64
67 const bool is_lossless,
68 const std::vector< std::string >& missing_symbols,
69 const bool dynamic_dictionary,
70 std::size_t max_dico_entries) :
71 is_lossless_(is_lossless), is_dictionary_dynamic_(dynamic_dictionary),
72 max_dico_entries_(max_dico_entries), val_type_(val_type) {
73 const std::size_t size = missing_symbols.size();
74
75 if (size) {
76 // save the set of symbols representing the missing values
77 missing_symbols_.resize((Size)missing_symbols.size());
78 for (const auto& symbol: missing_symbols) {
79 missing_symbols_.insert(symbol);
80 }
81 }
82
83 GUM_CONSTRUCTOR(DBTranslator);
84 }
85
87 INLINE DBTranslator::DBTranslator(DBTranslatedValueType val_type,
88 const bool is_lossless,
89 const bool dynamic_dictionary,
90 std::size_t max_dico_entries) :
91 is_lossless_(is_lossless), is_dictionary_dynamic_(dynamic_dictionary),
92 max_dico_entries_(max_dico_entries), val_type_(val_type) {
93 GUM_CONSTRUCTOR(DBTranslator);
94 }
95
97 INLINE DBTranslator::DBTranslator(const DBTranslator& from) :
98 is_lossless_(from.is_lossless_), is_dictionary_dynamic_(from.is_dictionary_dynamic_),
99 max_dico_entries_(from.max_dico_entries_), missing_symbols_(from.missing_symbols_),
100 back_dico_(from.back_dico_), val_type_(from.val_type_) {
101 GUM_CONS_CPY(DBTranslator);
102 }
103
105 INLINE DBTranslator::DBTranslator(DBTranslator&& from) :
106 is_lossless_(from.is_lossless_), is_dictionary_dynamic_(from.is_dictionary_dynamic_),
107 max_dico_entries_(from.max_dico_entries_),
108 missing_symbols_(std::move(from.missing_symbols_)), back_dico_(std::move(from.back_dico_)),
109 val_type_(from.val_type_) {
110 GUM_CONS_MOV(DBTranslator);
111 }
112
114 INLINE DBTranslator::~DBTranslator() { GUM_DESTRUCTOR(DBTranslator); }
115
117 INLINE DBTranslator& DBTranslator::operator=(const DBTranslator& from) {
118 if (this != &from) {
119 is_lossless_ = from.is_lossless_;
120 is_dictionary_dynamic_ = from.is_dictionary_dynamic_;
121 max_dico_entries_ = from.max_dico_entries_;
122 missing_symbols_ = from.missing_symbols_;
123 back_dico_ = from.back_dico_;
124 val_type_ = from.val_type_;
125 }
126 return *this;
127 }
128
130 INLINE DBTranslator& DBTranslator::operator=(DBTranslator&& from) {
131 is_lossless_ = from.is_lossless_;
132 is_dictionary_dynamic_ = from.is_dictionary_dynamic_;
133 max_dico_entries_ = from.max_dico_entries_;
134 missing_symbols_ = std::move(from.missing_symbols_);
135 back_dico_ = std::move(from.back_dico_);
136 val_type_ = from.val_type_;
137
138 return *this;
139 }
140
142 INLINE DBTranslatedValue DBTranslator::operator<<(std::string_view str) {
143 return translate(str);
144 }
145
147 INLINE std::string DBTranslator::operator>>(const DBTranslatedValue translated_val) {
148 return translateBack(translated_val);
149 }
150
152 INLINE bool DBTranslator::hasEditableDictionary() const { return is_dictionary_dynamic_; }
153
155 INLINE void DBTranslator::setEditableDictionaryMode(bool new_mode) {
156 is_dictionary_dynamic_ = new_mode;
157 }
158
160 INLINE const Bijection< std::size_t, std::string >& DBTranslator::getDictionary() const {
161 return back_dico_;
162 }
163
165 INLINE const Set< std::string >& DBTranslator::missingSymbols() const {
166 return missing_symbols_;
167 }
168
170 INLINE bool DBTranslator::isMissingSymbol(std::string_view str) const {
171 return missing_symbols_.exists(std::string(str));
172 }
173
175 INLINE void DBTranslator::setVariableName(std::string_view str) const {
176 const_cast< Variable* >(this->variable())->setName(std::string(str));
177 }
178
180 INLINE void DBTranslator::setVariableDescription(std::string_view str) const {
181 const_cast< Variable* >(this->variable())->setDescription(std::string(str));
182 }
183
185 INLINE bool DBTranslator::isMissingValue(const DBTranslatedValue& value) const {
186 switch (val_type_) {
187 case DBTranslatedValueType::DISCRETE :
188 return value.discr_val == std::numeric_limits< std::size_t >::max();
189
190 case DBTranslatedValueType::CONTINUOUS :
191 return value.cont_val == std::numeric_limits< float >::max();
192
193 default :
195 "No missing value interpretation for this "
196 "translated value type");
197 }
198 }
199
200 } /* namespace learning */
201
202} /* namespace gum */
203
204#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:76
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.