aGrUM  0.21.0
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  /// returns a new translator corresponding to a variable and some missing symbols
247  //template < template < typename > class ALLOC >
248 
249 
250  /// inserts a new translator for a given variable in the translator set
251  template < template < typename > class ALLOC >
252  template < template < typename > class XALLOC >
254  const Variable& var,
255  const std::size_t column,
256  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
257  const bool unique_column) {
258  // create the translator, depending on the type of the variable
259  switch (var.varType()) {
260  case VarType::Labelized: {
261  const LabelizedVariable& xvar = static_cast< const LabelizedVariable& >(var);
264  }
265 
266  case VarType::Integer: {
267  const IntegerVariable& xvar = static_cast< const IntegerVariable& >(var);
270  }
271 
272  case VarType::Discretized: {
273  const IDiscretizedVariable& xvar = static_cast< const IDiscretizedVariable& >(var);
276  }
277 
278  case VarType::Range: {
279  const RangeVariable& xvar = static_cast< const RangeVariable& >(var);
282  }
283 
284  case VarType::Continuous: {
285  const IContinuousVariable& xvar = static_cast< const IContinuousVariable& >(var);
288  }
289 
290  default:
292  "The insertion of the translator for Variable "
293  << var.name()
294  << " is impossible because a translator "
295  "for such variable is not implemented yet")
296  }
297  }
298 
299 
300  /// inserts a new translator for a given variable in the translator set
301  template < template < typename > class ALLOC >
303  const std::size_t column,
304  const bool unique_column) {
305  const std::vector< std::string, ALLOC< std::string > > missing;
307  }
308 
309 
310  /// substitute a translator by another one
311  template < template < typename > class ALLOC >
312  template < template < template < typename > class > class Translator >
314  const std::size_t pos) {
315  // check that the translator to be substituted exsts
316  if (_translators_.size() < pos) {
318  "The translatorSet contains only " << _translators_.size()
319  << " translators. It is therefore impossible to substitute "
320  << "the translator at index " << pos);
321  }
322 
323  // copy the new translator
324  ALLOC< DBTranslator< ALLOC > > alloc(this->getAllocator());
326 
327  // remove the old translator
330 
331  // insert the copy
333  }
334 
335 
336  /// erase the kth translator
337  template < template < typename > class ALLOC >
338  void DBTranslatorSet< ALLOC >::eraseTranslator(const std::size_t k, const bool k_is_input_col) {
340  const std::size_t nb_trans = _translators_.size();
341 
342  if (!k_is_input_col) {
343  if (nb_trans < k) return;
344 
345  // remove the translator and its corresponding column
348 
349  const std::size_t colk = _columns_[k];
352 
353  // if the highest column index corresponded to the kth translator,
354  // we must recomput it
355  if (_highest_column_ == colk) {
357  for (const auto col: _columns_)
359  }
360  } else {
361  // remove all the translators parsing the kth column
363  bool translator_found = false;
364  for (auto iter_col = _columns_.rbegin(); iter_col != _columns_.rend();
365  ++iter_col, ++iter_trans) {
366  if (*iter_col == k) {
367  // remove the translator and its corresponding column
370 
372  _columns_.erase((iter_col + 1).base());
373  translator_found = true;
374  }
375  }
376 
377  // if the highest column index corresponded to one of the translators
378  // removed, we must recompute it
379  if (translator_found && (k == _highest_column_)) {
381  for (const auto col: _columns_)
383  }
384  }
385  }
386 
387 
388  /// ask the kth translator to translate a string in a row of the database
389  template < template < typename > class ALLOC >
390  template < template < typename > class OTHER_ALLOC >
392  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
393  const std::size_t k) const {
394  return _translators_[k]->translate(row[_columns_[k]]);
395  }
396 
397 
398  /// ask the kth translator to translate a string in a row of the database
399  template < template < typename > class ALLOC >
400  template < template < typename > class OTHER_ALLOC >
402  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
403  const std::size_t k) const {
404  if (_translators_.size() <= k)
405  GUM_ERROR(UndefinedElement, "Translator #" << k << " could not be found")
406 
407  return _translators_[k]->translate(row[_columns_[k]]);
408  }
409 
410 
411  /// returns the original string that was translated into translated_val
412  template < template < typename > class ALLOC >
413  INLINE std::string
415  const std::size_t k) const {
417  }
418 
419 
420  /// returns the original string that was translated into translated_val
421  template < template < typename > class ALLOC >
422  INLINE std::string
424  const std::size_t k) const {
425  if (_translators_.size() <= k)
426  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
427 
429  }
430 
431 
432  // indicates whether the kth translator considers a translated_val
433  // as a missing value
434  template < template < typename > class ALLOC >
436  const std::size_t k) const {
438  }
439 
440 
441  // indicates whether the kth translator considers a translated_val
442  // as a missing value
443  template < template < typename > class ALLOC >
445  const std::size_t k) const {
446  if (_translators_.size() <= k)
447  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
448 
450  }
451 
452 
453  /// returns the kth translator
454  template < template < typename > class ALLOC >
456  return *(_translators_[k]);
457  }
458 
459 
460  /// returns the kth translator
461  template < template < typename > class ALLOC >
462  INLINE const DBTranslator< ALLOC >&
463  DBTranslatorSet< ALLOC >::translator(const std::size_t k) const {
464  return *(_translators_[k]);
465  }
466 
467 
468  /// returns the kth translator
469  template < template < typename > class ALLOC >
471  if (_translators_.size() <= k)
472  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
473 
474  return *(_translators_[k]);
475  }
476 
477 
478  /// returns the kth translator
479  template < template < typename > class ALLOC >
480  INLINE const DBTranslator< ALLOC >&
481  DBTranslatorSet< ALLOC >::translatorSafe(const std::size_t k) const {
482  if (_translators_.size() <= k)
483  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found")
484 
485  return *(_translators_[k]);
486  }
487 
488 
489  /// returns the domain size of the variables stored into the kth translator
490  template < template < typename > class ALLOC >
492  return _translators_[k]->domainSize();
493  }
494 
495 
496  /// returns the domain size of the variables stored into the kth translator
497  template < template < typename > class ALLOC >
499  if (_translators_.size() <= k)
500  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
501 
502  return _translators_[k]->domainSize();
503  }
504 
505 
506  /// returns the variable stored into the kth translator
507  template < template < typename > class ALLOC >
508  INLINE const Variable& DBTranslatorSet< ALLOC >::variable(const std::size_t k) const {
509  return *(_translators_[k]->variable());
510  }
511 
512 
513  /// returns the variable stored into the kth translator
514  template < template < typename > class ALLOC >
515  INLINE const Variable& DBTranslatorSet< ALLOC >::variableSafe(const std::size_t k) const {
516  if (_translators_.size() <= k)
517  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
518 
519  return *(_translators_[k]->variable());
520  }
521 
522 
523  // indicates whether a reordering is needed to make the kth translator
524  // sorted by lexicographical order
525  template < template < typename > class ALLOC >
526  INLINE bool DBTranslatorSet< ALLOC >::needsReordering(const std::size_t k) const {
527  return _translators_[k]->needsReordering();
528  }
529 
530 
531  // indicates whether a reordering is needed to make the kth translator
532  // sorted by lexicographical order
533  template < template < typename > class ALLOC >
534  INLINE bool DBTranslatorSet< ALLOC >::needsReorderingSafe(const std::size_t k) const {
535  if (_translators_.size() <= k)
536  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
537 
538  return _translators_[k]->needsReordering();
539  }
540 
541 
542  // performs a reordering of the dictionary and returns a mapping
543  // from the old translated values to the new ones.
544  template < template < typename > class ALLOC >
546  DBTranslatorSet< ALLOC >::reorder(const std::size_t k) {
547  return _translators_[k]->reorder();
548  }
549 
550 
551  // performs a reordering of the dictionary and returns a mapping
552  // from the old translated values to the new ones.
553  template < template < typename > class ALLOC >
556  if (_translators_.size() <= k)
557  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found")
558 
559  return _translators_[k]->reorder();
560  }
561 
562 
563  /** @brief returns the column of the input database that will be written
564  * in the kth column of the DatabaseTable */
565  template < template < typename > class ALLOC >
567  return _columns_[k];
568  }
569 
570 
571  /** @brief returns the column of the input database that will be written
572  * in the kth column of the DatabaseTable */
573  template < template < typename > class ALLOC >
575  if (_translators_.size() <= k)
576  GUM_ERROR(UndefinedElement, "Column #" << k << "could not be found")
577 
578  return _columns_[k];
579  }
580 
581 
582  /// returns the largest input database column index read by the translators
583  template < template < typename > class ALLOC >
585  return _highest_column_;
586  }
587 
588 
589  /// returns the number of translators stored into the set
590  template < template < typename > class ALLOC >
592  return _columns_.size();
593  }
594 
595 
596  /// returns the number of translators stored into the set
597  template < template < typename > class ALLOC >
598  INLINE std::size_t DBTranslatorSet< ALLOC >::size() const {
599  return _columns_.size();
600  }
601 
602  /// returns the set of translators
603  template < template < typename > class ALLOC >
604  INLINE const std::vector< DBTranslator< ALLOC >*, ALLOC< DBTranslator< ALLOC >* > >&
605  DBTranslatorSet< ALLOC >::translators() const {
606  return _translators_;
607  }
608 
609  } /* namespace learning */
610 
611 } /* namespace gum */
612 
613 #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)