aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DBTranslatorSet_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The set of translators stored into a row filter
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 namespace gum {
30 
31  namespace learning {
32 
33  /// returns the allocator used by the translator set
34  template < template < typename > class ALLOC >
35  typename DBTranslatorSet< ALLOC >::allocator_type
36  DBTranslatorSet< ALLOC >::getAllocator() const {
37  return _columns_.get_allocator();
38  }
39 
40 
41  /// remove all the translators from the vector
42  template < template < typename > class ALLOC >
43  void DBTranslatorSet< ALLOC >::clear() {
45  for (auto translator: _translators_) {
48  }
49 
51  _columns_.clear();
53  }
54 
55 
56  /// copy the content of another translator set that uses another allocator
57  template < template < typename > class ALLOC >
58  void DBTranslatorSet< ALLOC >::_copy_(
59  const DBTranslatorSet< ALLOC >& from,
60  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) {
61  if (_translators_.size() != 0) clear();
62 
63  // resize the vectors used in the set. First, we reserve new memory. This
64  // will keep the DBTranslatorSet in a correct state, even if the memory
65  // allocation fails
66  const std::size_t size = from._translators_.size();
71 
72  std::size_t i;
73  try {
74  for (i = std::size_t(0); i < size; ++i) {
77  }
78  } catch (...) {
80  clear();
81  throw;
82  }
83 
85  }
86 
87 
88  /// default constructor
89  template < template < typename > class ALLOC >
91  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
95  }
96 
97 
98  /// copy constructor with a given allocator
99  template < template < typename > class ALLOC >
101  const DBTranslatorSet< ALLOC >& from,
102  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
104  _columns_(alloc) {
105  _copy_(from, alloc);
106 
108  }
109 
110 
111  /// copy constructor
112  template < template < typename > class ALLOC >
115 
116 
117  /// move constructor with a given allocator
118  template < template < typename > class ALLOC >
121  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
125  }
126 
127 
128  /// move constructor
129  template < template < typename > class ALLOC >
132 
133 
134  /// virtual copy constructor with a given allocator
135  template < template < typename > class ALLOC >
137  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) const {
140  try {
141  allocator.construct(set, *this, alloc);
142  } catch (...) {
144  throw;
145  }
146  return set;
147  }
148 
149 
150  /// virtual copy constructor
151  template < template < typename > class ALLOC >
153  return clone(this->getAllocator());
154  }
155 
156 
157  /// destructor
158  template < template < typename > class ALLOC >
160  clear();
162  }
163 
164 
165  /// copy operator
166  template < template < typename > class ALLOC >
169  if (this != &from) {
170  clear();
171  _copy_(from, this->getAllocator());
172  }
173 
174  return *this;
175  }
176 
177 
178  /// move operator
179  template < template < typename > class ALLOC >
182  if (this != &from) {
183  clear();
187  }
188 
189  return *this;
190  }
191 
192 
193  /// returns the ith translator
194  template < template < typename > class ALLOC >
196  return *(_translators_[k]);
197  }
198 
199 
200  /// returns the ith translator
201  template < template < typename > class ALLOC >
202  INLINE const DBTranslator< ALLOC >&
203  DBTranslatorSet< ALLOC >::operator[](const std::size_t k) const {
204  return *(_translators_[k]);
205  }
206 
207 
208  /// inserts a new translator at the end of the translator set
209  template < template < typename > class ALLOC >
210  template < template < template < typename > class > class Translator >
212  const std::size_t column,
213  const bool unique_column) {
214  // if the unique_column parameter is set to true and there exists already
215  // another translator that parses the column, raise a DuplicateElement
216  // exception
217  const std::size_t size = _translators_.size();
218  if (unique_column) {
219  for (std::size_t i = std::size_t(0); i < size; ++i) {
220  if (_columns_[i] == column)
222  "There already exists a DBTranslator that parses Column" << column)
223  }
224  }
225 
226  // reserve some place for the new translator
228  _columns_.reserve(size + 1);
229 
230  // create and add the new translator
233 
235  _columns_.resize(size + 1);
237  _columns_[size] = column;
238 
239  // update the highest column
241 
242  return size;
243  }
244 
245 
246  /// inserts a new translator for a given variable in the translator set
247  template < template < typename > class ALLOC >
248  template < template < typename > class XALLOC >
250  const Variable& var,
251  const std::size_t column,
252  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
253  const bool unique_column) {
254  // create the translatator, depending on the type of the variable
255  switch (var.varType()) {
256  case VarType::Labelized: {
257  const LabelizedVariable& xvar = static_cast< const LabelizedVariable& >(var);
260  }
261 
262  case VarType::Discretized: {
263  const IDiscretizedVariable& xvar = static_cast< const IDiscretizedVariable& >(var);
266  }
267 
268  case VarType::Range: {
269  const RangeVariable& xvar = static_cast< const RangeVariable& >(var);
272  }
273 
274  case VarType::Continuous: {
275  const IContinuousVariable& xvar = static_cast< const IContinuousVariable& >(var);
278  }
279 
280  default:
282  "The insertion of the translator for Variable "
283  << var.name()
284  << " is impossible because a translator "
285  "for such variable is not implemented yet")
286  }
287  }
288 
289 
290  /// inserts a new translator for a given variable in the translator set
291  template < template < typename > class ALLOC >
293  const std::size_t column,
294  const bool unique_column) {
295  const std::vector< std::string, ALLOC< std::string > > missing;
297  }
298 
299 
300  /// erase the kth translator
301  template < template < typename > class ALLOC >
302  void DBTranslatorSet< ALLOC >::eraseTranslator(const std::size_t k, const bool k_is_input_col) {
304  const std::size_t nb_trans = _translators_.size();
305 
306  if (!k_is_input_col) {
307  if (nb_trans < k) return;
308 
309  // remove the translator and its corresponding column
312 
313  const std::size_t colk = _columns_[k];
316 
317  // if the highest column index corresponded to the kth translator,
318  // we must recomput it
319  if (_highest_column_ == colk) {
321  for (const auto col: _columns_)
323  }
324  } else {
325  // remove all the translators parsing the kth column
327  bool translator_found = false;
328  for (auto iter_col = _columns_.rbegin(); iter_col != _columns_.rend();
329  ++iter_col, ++iter_trans) {
330  if (*iter_col == k) {
331  // remove the translator and its corresponding column
334 
336  _columns_.erase((iter_col + 1).base());
337  translator_found = true;
338  }
339  }
340 
341  // if the highest column index corresponded to one of the translators
342  // removed, we must recompute it
343  if (translator_found && (k == _highest_column_)) {
345  for (const auto col: _columns_)
347  }
348  }
349  }
350 
351 
352  /// ask the kth translator to translate a string in a row of the database
353  template < template < typename > class ALLOC >
354  template < template < typename > class OTHER_ALLOC >
356  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
357  const std::size_t k) const {
358  return _translators_[k]->translate(row[_columns_[k]]);
359  }
360 
361 
362  /// ask the kth translator to translate a string in a row of the database
363  template < template < typename > class ALLOC >
364  template < template < typename > class OTHER_ALLOC >
366  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
367  const std::size_t k) const {
368  if (_translators_.size() <= k)
369  GUM_ERROR(UndefinedElement, "Translator #" << k << " could not be found")
370 
371  return _translators_[k]->translate(row[_columns_[k]]);
372  }
373 
374 
375  /// returns the original string that was translated into translated_val
376  template < template < typename > class ALLOC >
377  INLINE std::string
379  const std::size_t k) const {
381  }
382 
383 
384  /// returns the original string that was translated into translated_val
385  template < template < typename > class ALLOC >
386  INLINE std::string
388  const std::size_t k) const {
389  if (_translators_.size() <= k)
390  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
391 
393  }
394 
395 
396  // indicates whether the kth translator considers a translated_val
397  // as a missing value
398  template < template < typename > class ALLOC >
400  const std::size_t k) const {
402  }
403 
404 
405  // indicates whether the kth translator considers a translated_val
406  // as a missing value
407  template < template < typename > class ALLOC >
409  const std::size_t k) const {
410  if (_translators_.size() <= k)
411  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
412 
414  }
415 
416 
417  /// returns the kth translator
418  template < template < typename > class ALLOC >
420  return *(_translators_[k]);
421  }
422 
423 
424  /// returns the kth translator
425  template < template < typename > class ALLOC >
426  INLINE const DBTranslator< ALLOC >&
427  DBTranslatorSet< ALLOC >::translator(const std::size_t k) const {
428  return *(_translators_[k]);
429  }
430 
431 
432  /// returns the kth translator
433  template < template < typename > class ALLOC >
435  if (_translators_.size() <= k)
436  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
437 
438  return *(_translators_[k]);
439  }
440 
441 
442  /// returns the kth translator
443  template < template < typename > class ALLOC >
444  INLINE const DBTranslator< ALLOC >&
445  DBTranslatorSet< ALLOC >::translatorSafe(const std::size_t k) const {
446  if (_translators_.size() <= k)
447  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
448 
449  return *(_translators_[k]);
450  }
451 
452 
453  /// returns the domain size of the variables stored into the kth translator
454  template < template < typename > class ALLOC >
456  return _translators_[k]->domainSize();
457  }
458 
459 
460  /// returns the domain size of the variables stored into the kth translator
461  template < template < typename > class ALLOC >
463  if (_translators_.size() <= k)
464  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
465 
466  return _translators_[k]->domainSize();
467  }
468 
469 
470  /// returns the variable stored into the kth translator
471  template < template < typename > class ALLOC >
472  INLINE const Variable& DBTranslatorSet< ALLOC >::variable(const std::size_t k) const {
473  return *(_translators_[k]->variable());
474  }
475 
476 
477  /// returns the variable stored into the kth translator
478  template < template < typename > class ALLOC >
479  INLINE const Variable& DBTranslatorSet< ALLOC >::variableSafe(const std::size_t k) const {
480  if (_translators_.size() <= k)
481  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
482 
483  return *(_translators_[k]->variable());
484  }
485 
486 
487  // indicates whether a reordering is needed to make the kth translator
488  // sorted by lexicographical order
489  template < template < typename > class ALLOC >
490  INLINE bool DBTranslatorSet< ALLOC >::needsReordering(const std::size_t k) const {
491  return _translators_[k]->needsReordering();
492  }
493 
494 
495  // indicates whether a reordering is needed to make the kth translator
496  // sorted by lexicographical order
497  template < template < typename > class ALLOC >
498  INLINE bool DBTranslatorSet< ALLOC >::needsReorderingSafe(const std::size_t k) const {
499  if (_translators_.size() <= k)
500  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
501 
502  return _translators_[k]->needsReordering();
503  }
504 
505 
506  // performs a reordering of the dictionary and returns a mapping
507  // from the old translated values to the new ones.
508  template < template < typename > class ALLOC >
510  DBTranslatorSet< ALLOC >::reorder(const std::size_t k) {
511  return _translators_[k]->reorder();
512  }
513 
514 
515  // performs a reordering of the dictionary and returns a mapping
516  // from the old translated values to the new ones.
517  template < template < typename > class ALLOC >
520  if (_translators_.size() <= k)
521  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
522 
523  return _translators_[k]->reorder();
524  }
525 
526 
527  /** @brief returns the column of the input database that will be written
528  * in the kth column of the DatabaseTable */
529  template < template < typename > class ALLOC >
531  return _columns_[k];
532  }
533 
534 
535  /** @brief returns the column of the input database that will be written
536  * in the kth column of the DatabaseTable */
537  template < template < typename > class ALLOC >
539  if (_translators_.size() <= k)
540  GUM_ERROR(UndefinedElement, "Column #" << k << "could not be found")
541 
542  return _columns_[k];
543  }
544 
545 
546  /// returns the largest input database column index read by the translators
547  template < template < typename > class ALLOC >
549  return _highest_column_;
550  }
551 
552 
553  /// returns the number of translators stored into the set
554  template < template < typename > class ALLOC >
556  return _columns_.size();
557  }
558 
559 
560  /// returns the number of translators stored into the set
561  template < template < typename > class ALLOC >
562  INLINE std::size_t DBTranslatorSet< ALLOC >::size() const {
563  return _columns_.size();
564  }
565 
566  /// returns the set of translators
567  template < template < typename > class ALLOC >
568  INLINE const std::vector< DBTranslator< ALLOC >*, ALLOC< DBTranslator< ALLOC >* > >&
569  DBTranslatorSet< ALLOC >::translators() const {
570  return _translators_;
571  }
572 
573  } /* namespace learning */
574 
575 } /* namespace gum */
576 
577 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)