aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DBTranslatorSet_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 >
114  const DBTranslatorSet< ALLOC >& from) :
116 
117 
118  /// move constructor with a given allocator
119  template < template < typename > class ALLOC >
122  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
127  }
128 
129 
130  /// move constructor
131  template < template < typename > class ALLOC >
132  INLINE
135 
136 
137  /// virtual copy constructor with a given allocator
138  template < template < typename > class ALLOC >
140  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) const {
143  try {
144  allocator.construct(set, *this, alloc);
145  } catch (...) {
147  throw;
148  }
149  return set;
150  }
151 
152 
153  /// virtual copy constructor
154  template < template < typename > class ALLOC >
156  return clone(this->getAllocator());
157  }
158 
159 
160  /// destructor
161  template < template < typename > class ALLOC >
163  clear();
165  }
166 
167 
168  /// copy operator
169  template < template < typename > class ALLOC >
172  if (this != &from) {
173  clear();
174  copy__(from, this->getAllocator());
175  }
176 
177  return *this;
178  }
179 
180 
181  /// move operator
182  template < template < typename > class ALLOC >
185  if (this != &from) {
186  clear();
190  }
191 
192  return *this;
193  }
194 
195 
196  /// returns the ith translator
197  template < template < typename > class ALLOC >
199  DBTranslatorSet< ALLOC >::operator[](const std::size_t k) {
200  return *(translators__[k]);
201  }
202 
203 
204  /// returns the ith translator
205  template < template < typename > class ALLOC >
206  INLINE const DBTranslator< ALLOC >&
207  DBTranslatorSet< ALLOC >::operator[](const std::size_t k) const {
208  return *(translators__[k]);
209  }
210 
211 
212  /// inserts a new translator at the end of the translator set
213  template < template < typename > class ALLOC >
214  template < template < template < typename > class > class Translator >
216  const Translator< ALLOC >& translator,
217  const std::size_t column,
218  const bool unique_column) {
219  // if the unique_column parameter is set to true and there exists already
220  // another translator that parses the column, raise a DuplicateElement
221  // exception
222  const std::size_t size = translators__.size();
223  if (unique_column) {
224  for (std::size_t i = std::size_t(0); i < size; ++i) {
225  if (columns__[i] == column)
227  "There already exists a DBTranslator that parses Column"
228  << column);
229  }
230  }
231 
232  // reserve some place for the new translator
234  columns__.reserve(size + 1);
235 
236  // create and add the new translator
239 
241  columns__.resize(size + 1);
243  columns__[size] = column;
244 
245  // update the highest column
247 
248  return size;
249  }
250 
251 
252  /// inserts a new translator for a given variable in the translator set
253  template < template < typename > class ALLOC >
254  template < template < typename > class XALLOC >
256  const Variable& var,
257  const std::size_t column,
258  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
259  const bool unique_column) {
260  // create the translatator, depending on the type of the variable
261  switch (var.varType()) {
262  case VarType::Labelized: {
263  const LabelizedVariable& xvar
264  = static_cast< const LabelizedVariable& >(var);
268  }
269 
270  case VarType::Discretized: {
272  = 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: {
286  = static_cast< const IContinuousVariable& >(var);
290  }
291 
292  default:
294  "The insertion of the translator for Variable "
295  << var.name()
296  << " is impossible because a translator "
297  "for such variable is not implemented yet");
298  }
299  }
300 
301 
302  /// inserts a new translator for a given variable in the translator set
303  template < template < typename > class ALLOC >
304  INLINE std::size_t
306  const std::size_t column,
307  const bool unique_column) {
308  const std::vector< std::string, ALLOC< std::string > > missing;
310  }
311 
312 
313  /// erase the kth translator
314  template < template < typename > class ALLOC >
316  const bool k_is_input_col) {
318  const std::size_t nb_trans = translators__.size();
319 
320  if (!k_is_input_col) {
321  if (nb_trans < k) return;
322 
323  // remove the translator and its corresponding column
326 
327  const std::size_t colk = columns__[k];
330 
331  // if the highest column index corresponded to the kth translator,
332  // we must recomput it
333  if (highest_column__ == colk) {
335  for (const auto col: columns__)
337  }
338  } else {
339  // remove all the translators parsing the kth column
341  bool translator_found = false;
342  for (auto iter_col = columns__.rbegin(); iter_col != columns__.rend();
343  ++iter_col, ++iter_trans) {
344  if (*iter_col == k) {
345  // remove the translator and its corresponding column
348 
350  columns__.erase((iter_col + 1).base());
351  translator_found = true;
352  }
353  }
354 
355  // if the highest column index corresponded to one of the translators
356  // removed, we must recompute it
357  if (translator_found && (k == highest_column__)) {
359  for (const auto col: columns__)
361  }
362  }
363  }
364 
365 
366  /// ask the kth translator to translate a string in a row of the database
367  template < template < typename > class ALLOC >
368  template < template < typename > class OTHER_ALLOC >
370  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
371  const std::size_t k) const {
372  return translators__[k]->translate(row[columns__[k]]);
373  }
374 
375 
376  /// ask the kth translator to translate a string in a row of the database
377  template < template < typename > class ALLOC >
378  template < template < typename > class OTHER_ALLOC >
380  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
381  const std::size_t k) const {
382  if (translators__.size() <= k)
383  GUM_ERROR(UndefinedElement, "Translator #" << k << " could not be found");
384  return translators__[k]->translate(row[columns__[k]]);
385  }
386 
387 
388  /// returns the original string that was translated into translated_val
389  template < template < typename > class ALLOC >
392  const std::size_t k) const {
394  }
395 
396 
397  /// returns the original string that was translated into translated_val
398  template < template < typename > class ALLOC >
401  const std::size_t k) const {
402  if (translators__.size() <= k)
403  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
405  }
406 
407 
408  // indicates whether the kth translator considers a translated_val
409  // as a missing value
410  template < template < typename > class ALLOC >
413  const std::size_t k) const {
415  }
416 
417 
418  // indicates whether the kth translator considers a translated_val
419  // as a missing value
420  template < template < typename > class ALLOC >
423  const std::size_t k) const {
424  if (translators__.size() <= k)
425  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
427  }
428 
429 
430  /// returns the kth translator
431  template < template < typename > class ALLOC >
434  return *(translators__[k]);
435  }
436 
437 
438  /// returns the kth translator
439  template < template < typename > class ALLOC >
440  INLINE const DBTranslator< ALLOC >&
441  DBTranslatorSet< ALLOC >::translator(const std::size_t k) const {
442  return *(translators__[k]);
443  }
444 
445 
446  /// returns the kth translator
447  template < template < typename > class ALLOC >
450  if (translators__.size() <= k)
451  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
452  return *(translators__[k]);
453  }
454 
455 
456  /// returns the kth translator
457  template < template < typename > class ALLOC >
458  INLINE const DBTranslator< ALLOC >&
459  DBTranslatorSet< ALLOC >::translatorSafe(const std::size_t k) const {
460  if (translators__.size() <= k)
461  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
462  return *(translators__[k]);
463  }
464 
465 
466  /// returns the domain size of the variables stored into the kth translator
467  template < template < typename > class ALLOC >
468  INLINE std::size_t
469  DBTranslatorSet< ALLOC >::domainSize(const std::size_t k) const {
470  return translators__[k]->domainSize();
471  }
472 
473 
474  /// returns the domain size of the variables stored into the kth translator
475  template < template < typename > class ALLOC >
476  INLINE std::size_t
477  DBTranslatorSet< ALLOC >::domainSizeSafe(const std::size_t k) const {
478  if (translators__.size() <= k)
479  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
480  return translators__[k]->domainSize();
481  }
482 
483 
484  /// returns the variable stored into the kth translator
485  template < template < typename > class ALLOC >
486  INLINE const Variable&
487  DBTranslatorSet< ALLOC >::variable(const std::size_t k) const {
488  return *(translators__[k]->variable());
489  }
490 
491 
492  /// returns the variable stored into the kth translator
493  template < template < typename > class ALLOC >
494  INLINE const Variable&
495  DBTranslatorSet< ALLOC >::variableSafe(const std::size_t k) const {
496  if (translators__.size() <= k)
497  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
498  return *(translators__[k]->variable());
499  }
500 
501 
502  // indicates whether a reordering is needed to make the kth translator
503  // sorted by lexicographical order
504  template < template < typename > class ALLOC >
505  INLINE bool
506  DBTranslatorSet< ALLOC >::needsReordering(const std::size_t k) const {
507  return translators__[k]->needsReordering();
508  }
509 
510 
511  // indicates whether a reordering is needed to make the kth translator
512  // sorted by lexicographical order
513  template < template < typename > class ALLOC >
514  INLINE bool
516  if (translators__.size() <= k)
517  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
518  return translators__[k]->needsReordering();
519  }
520 
521 
522  // performs a reordering of the dictionary and returns a mapping
523  // from the old translated values to the new ones.
524  template < template < typename > class ALLOC >
526  std::size_t,
527  ALLOC< std::pair< std::size_t, std::size_t > > >
528  DBTranslatorSet< ALLOC >::reorder(const std::size_t k) {
529  return translators__[k]->reorder();
530  }
531 
532 
533  // performs a reordering of the dictionary and returns a mapping
534  // from the old translated values to the new ones.
535  template < template < typename > class ALLOC >
537  std::size_t,
538  ALLOC< std::pair< std::size_t, std::size_t > > >
540  if (translators__.size() <= k)
541  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
542  return translators__[k]->reorder();
543  }
544 
545 
546  /** @brief returns the column of the input database that will be written
547  * in the kth column of the DatabaseTable */
548  template < template < typename > class ALLOC >
549  INLINE std::size_t
550  DBTranslatorSet< ALLOC >::inputColumn(const std::size_t k) const {
551  return columns__[k];
552  }
553 
554 
555  /** @brief returns the column of the input database that will be written
556  * in the kth column of the DatabaseTable */
557  template < template < typename > class ALLOC >
558  INLINE std::size_t
559  DBTranslatorSet< ALLOC >::inputColumnSafe(const std::size_t k) const {
560  if (translators__.size() <= k)
561  GUM_ERROR(UndefinedElement, "Column #" << k << "could not be found");
562  return columns__[k];
563  }
564 
565 
566  /// returns the largest input database column index read by the translators
567  template < template < typename > class ALLOC >
569  return highest_column__;
570  }
571 
572 
573  /// returns the number of translators stored into the set
574  template < template < typename > class ALLOC >
576  return columns__.size();
577  }
578 
579 
580  /// returns the number of translators stored into the set
581  template < template < typename > class ALLOC >
582  INLINE std::size_t DBTranslatorSet< ALLOC >::size() const {
583  return columns__.size();
584  }
585 
586  /// returns the set of translators
587  template < template < typename > class ALLOC >
588  INLINE const
590  DBTranslatorSet< ALLOC >::translators() const {
591  return translators__;
592  }
593 
594  } /* namespace learning */
595 
596 } /* namespace gum */
597 
598 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)