aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslatorSet.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 for (auto translator: _translators_) {
64 delete translator;
65 }
66
67 _translators_.clear();
68 _columns_.clear();
69 _highest_column_ = std::size_t(0);
70 }
71
73 void DBTranslatorSet::_copy_(const DBTranslatorSet& from) {
74 if (_translators_.size() != 0) clear();
75
76 // resize the vectors used in the set. First, we reserve new memory. This
77 // will keep the DBTranslatorSet in a correct state, even if the memory
78 // allocation fails
79 const std::size_t size = from._translators_.size();
80 _translators_.reserve(size);
81 _columns_.reserve(size);
82 _translators_.resize(size);
83 _columns_.resize(size);
84
85 std::size_t i;
86 try {
87 for (i = std::size_t(0); i < size; ++i) {
88 _translators_[i] = from._translators_[i]->clone();
89 _columns_[i] = from._columns_[i];
90 }
91 } catch (...) {
92 _translators_.resize(i);
93 clear();
94 throw;
95 }
96
97 _highest_column_ = from._highest_column_;
98 }
99
102 if (this != &from) {
103 clear();
104 _copy_(from);
105 }
106
107 return *this;
108 }
109
111
112
114 std::size_t DBTranslatorSet::insertTranslator(const Variable& var,
115 const std::size_t column,
116 const std::vector< std::string >& missing_symbols,
117 const bool unique_column) {
118 // create the translator, depending on the type of the variable
119 DBTranslator* translator = DBTranslators::create(var, missing_symbols);
120
121 const std::size_t index = insertTranslator(*translator, column, unique_column);
122
123 // deallocate the translator
124 delete translator;
125
126 return index;
127 }
128
130 std::size_t DBTranslatorSet::insertTranslator(const DBTranslator& translator,
131 const std::size_t column,
132 const bool unique_column) {
133 // if the unique_column parameter is set to true and there exists already
134 // another translator that parses the column, raise a DuplicateElement
135 // exception
136 const std::size_t size = _translators_.size();
137 if (unique_column) {
138 for (std::size_t i = std::size_t(0); i < size; ++i) {
139 if (_columns_[i] == column)
140 GUM_ERROR(DuplicateElement,
141 "There already exists a DBTranslator that parses Column" << column)
142 }
143 }
144
145 // reserve some place for the new translator
146 _translators_.reserve(size + 1);
147 _columns_.reserve(size + 1);
148
149 // create and add the new translator
150 DBTranslator* new_translator = translator.clone();
151
152 _translators_.resize(size + 1);
153 _columns_.resize(size + 1);
154 _translators_[size] = new_translator;
155 _columns_[size] = column;
156
157 // update the highest column
158 if (column > _highest_column_) _highest_column_ = column;
159
160 return size;
161 }
162
164 void DBTranslatorSet::eraseTranslator(const std::size_t k, const bool k_is_input_col) {
165 const std::size_t nb_trans = _translators_.size();
166
167 if (!k_is_input_col) {
168 if (nb_trans < k) return;
169
170 // remove the translator and its corresponding column
171 delete _translators_[k];
172
173 const std::size_t colk = _columns_[k];
174 _translators_.erase(_translators_.begin() + k);
175 _columns_.erase(_columns_.begin() + k);
176
177 // if the highest column index corresponded to the kth translator,
178 // we must recomput it
179 if (_highest_column_ == colk) {
180 _highest_column_ = std::size_t(0);
181 for (const auto col: _columns_)
182 if (_highest_column_ < col) _highest_column_ = col;
183 }
184 } else {
185 // remove all the translators parsing the kth column
186 auto iter_trans = _translators_.rbegin();
187 bool translator_found = false;
188 for (auto iter_col = _columns_.rbegin(); iter_col != _columns_.rend();
189 ++iter_col, ++iter_trans) {
190 if (*iter_col == k) {
191 // remove the translator and its corresponding column
192 delete *iter_trans;
193
194 _translators_.erase((iter_trans + 1).base());
195 _columns_.erase((iter_col + 1).base());
196 translator_found = true;
197 }
198 }
199
200 // if the highest column index corresponded to one of the translators
201 // removed, we must recompute it
202 if (translator_found && (k == _highest_column_)) {
203 _highest_column_ = std::size_t(0);
204 for (const auto col: _columns_)
205 if (_highest_column_ < col) _highest_column_ = col;
206 }
207 }
208 }
209
210
211 } /* namespace learning */
212
213} /* namespace gum */
214
215#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A class for storing several translators.
the class for packing together the translators used to preprocess the datasets
void eraseTranslator(const std::size_t k, const bool k_is_input_col=false)
erases either the kth translator or those parsing the kth column of the input database
DBTranslatorSet & operator=(const DBTranslatorSet &from)
copy operator
std::size_t size() const
returns the number of translators stored into the set
DBTranslator & translator(const std::size_t k)
returns the kth translator
void clear()
remove all the translators
std::size_t insertTranslator(const DBTranslator &translator, const std::size_t column, const bool unique_column=true)
inserts a new translator at the end of the translator set
The base class for all the tabular database cell translators.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
DBTranslator * create(const Variable &var, const bool editable_dictionary=false, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
named constructor
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46