aGrUM  0.16.0
DBTranslatorSet_tpl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
35  template < template < typename > class ALLOC >
38  return __columns.get_allocator();
39  }
40 
41 
43  template < template < typename > class ALLOC >
45  ALLOC< DBTranslator< ALLOC > > allocator(this->getAllocator());
46  for (auto translator : __translators) {
47  allocator.destroy(translator);
48  allocator.deallocate(translator, 1);
49  }
50 
51  __translators.clear();
52  __columns.clear();
53  __highest_column = std::size_t(0);
54  }
55 
56 
58  template < template < typename > class ALLOC >
59  void DBTranslatorSet< ALLOC >::__copy(
60  const DBTranslatorSet< ALLOC >& from,
61  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) {
62  if (__translators.size() != 0) clear();
63 
64  // resize the vectors used in the set. First, we reserve new memory. This
65  // will keep the DBTranslatorSet in a correct state, even if the memory
66  // allocation fails
67  const std::size_t size = from.__translators.size();
68  __translators.reserve(size);
69  __columns.reserve(size);
70  __translators.resize(size);
71  __columns.resize(size);
72 
73  std::size_t i;
74  try {
75  for (i = std::size_t(0); i < size; ++i) {
76  __translators[i] = from.__translators[i]->clone(alloc);
77  __columns[i] = from.__columns[i];
78  }
79  } catch (...) {
80  __translators.resize(i);
81  clear();
82  throw;
83  }
84 
85  __highest_column = from.__highest_column;
86  }
87 
88 
90  template < template < typename > class ALLOC >
92  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
93  __translators(alloc),
94  __columns(allocator_type(alloc)) {
95  GUM_CONSTRUCTOR(DBTranslatorSet);
96  }
97 
98 
100  template < template < typename > class ALLOC >
102  const DBTranslatorSet< ALLOC >& from,
103  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
104  __translators(alloc),
105  __columns(alloc) {
106  __copy(from, alloc);
107 
108  GUM_CONS_CPY(DBTranslatorSet);
109  }
110 
111 
113  template < template < typename > class ALLOC >
115  const DBTranslatorSet< ALLOC >& from) :
116  DBTranslatorSet< ALLOC >(from, from.getAllocator()) {}
117 
118 
120  template < template < typename > class ALLOC >
122  DBTranslatorSet< ALLOC >&& from,
123  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) :
124  __translators(std::move(from.__translators), alloc),
125  __columns(std::move(from.__columns), alloc),
126  __highest_column(from.__highest_column) {
127  GUM_CONS_MOV(DBTranslatorSet);
128  }
129 
130 
132  template < template < typename > class ALLOC >
133  INLINE
134  DBTranslatorSet< ALLOC >::DBTranslatorSet(DBTranslatorSet< ALLOC >&& from) :
135  DBTranslatorSet< ALLOC >(from, from.getAllocator()) {}
136 
137 
139  template < template < typename > class ALLOC >
140  DBTranslatorSet< ALLOC >* DBTranslatorSet< ALLOC >::clone(
141  const typename DBTranslatorSet< ALLOC >::allocator_type& alloc) const {
142  ALLOC< DBTranslatorSet< ALLOC > > allocator(alloc);
143  DBTranslatorSet< ALLOC >* set = allocator.allocate(1);
144  try {
145  allocator.construct(set, *this, alloc);
146  } catch (...) {
147  allocator.deallocate(set, 1);
148  throw;
149  }
150  return set;
151  }
152 
153 
155  template < template < typename > class ALLOC >
156  DBTranslatorSet< ALLOC >* DBTranslatorSet< ALLOC >::clone() const {
157  return clone(this->getAllocator());
158  }
159 
160 
162  template < template < typename > class ALLOC >
164  clear();
165  GUM_DESTRUCTOR(DBTranslatorSet);
166  }
167 
168 
170  template < template < typename > class ALLOC >
171  DBTranslatorSet< ALLOC >& DBTranslatorSet< ALLOC >::
172  operator=(const DBTranslatorSet< ALLOC >& from) {
173  if (this != &from) {
174  clear();
175  __copy(from, this->getAllocator());
176  }
177 
178  return *this;
179  }
180 
181 
183  template < template < typename > class ALLOC >
184  INLINE DBTranslatorSet< ALLOC >& DBTranslatorSet< ALLOC >::
185  operator=(DBTranslatorSet< ALLOC >&& from) {
186  if (this != &from) {
187  clear();
188  __translators = std::move(from.__translators);
189  __columns = std::move(from.__columns);
190  __highest_column = from.__highest_column;
191  }
192 
193  return *this;
194  }
195 
196 
198  template < template < typename > class ALLOC >
199  INLINE DBTranslator< ALLOC >& DBTranslatorSet< ALLOC >::
200  operator[](const std::size_t k) {
201  return *(__translators[k]);
202  }
203 
204 
206  template < template < typename > class ALLOC >
207  INLINE const DBTranslator< ALLOC >& DBTranslatorSet< ALLOC >::
208  operator[](const std::size_t k) const {
209  return *(__translators[k]);
210  }
211 
212 
214  template < template < typename > class ALLOC >
215  template < template < template < typename > class > class Translator >
216  std::size_t DBTranslatorSet< ALLOC >::insertTranslator(
217  const Translator< ALLOC >& translator,
218  const std::size_t column,
219  const bool unique_column) {
220  // if the unique_column parameter is set to true and there exists already
221  // another translator that parses the column, raise a DuplicateElement
222  // exception
223  const std::size_t size = __translators.size();
224  if (unique_column) {
225  for (std::size_t i = std::size_t(0); i < size; ++i) {
226  if (__columns[i] == column)
227  GUM_ERROR(DuplicateElement,
228  "There already exists a DBTranslator that parses Column"
229  << column);
230  }
231  }
232 
233  // reserve some place for the new translator
234  __translators.reserve(size + 1);
235  __columns.reserve(size + 1);
236 
237  // create and add the new translator
238  ALLOC< DBTranslator< ALLOC > > allocator(this->getAllocator());
239  DBTranslator< ALLOC >* new_translator = translator.clone(allocator);
240 
241  __translators.resize(size + 1);
242  __columns.resize(size + 1);
243  __translators[size] = new_translator;
244  __columns[size] = column;
245 
246  // update the highest column
247  if (column > __highest_column) __highest_column = column;
248 
249  return size;
250  }
251 
252 
254  template < template < typename > class ALLOC >
255  template < template < typename > class XALLOC >
257  const Variable& var,
258  const std::size_t column,
259  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
260  const bool unique_column) {
261  // create the translatator, depending on the type of the variable
262  switch (var.varType()) {
263  case VarType::Labelized: {
264  const LabelizedVariable& xvar =
265  static_cast< const LabelizedVariable& >(var);
266  DBTranslator4LabelizedVariable< ALLOC > translator(xvar,
267  missing_symbols);
268  return insertTranslator(translator, column, unique_column);
269  }
270 
271  case VarType::Discretized: {
272  const IDiscretizedVariable& xvar =
273  static_cast< const IDiscretizedVariable& >(var);
274  DBTranslator4DiscretizedVariable< ALLOC > translator(xvar,
275  missing_symbols);
276  return insertTranslator(translator, column, unique_column);
277  }
278 
279  case VarType::Range: {
280  const RangeVariable& xvar = static_cast< const RangeVariable& >(var);
281  DBTranslator4RangeVariable< ALLOC > translator(xvar, missing_symbols);
282  return insertTranslator(translator, column, unique_column);
283  }
284 
285  case VarType::Continuous: {
286  const IContinuousVariable& xvar =
287  static_cast< const IContinuousVariable& >(var);
288  DBTranslator4ContinuousVariable< ALLOC > translator(xvar,
289  missing_symbols);
290  return insertTranslator(translator, column, unique_column);
291  }
292 
293  default:
294  GUM_ERROR(NotImplementedYet,
295  "The insertion of the translator for Variable "
296  << var.name()
297  << " is impossible because a translator "
298  "for such variable is not implemented yet");
299  }
300  }
301 
302 
304  template < template < typename > class ALLOC >
306  const Variable& var, const std::size_t column, const bool unique_column) {
307  const std::vector< std::string, ALLOC< std::string > > missing;
308  return this->insertTranslator(var, column, missing, unique_column);
309  }
310 
311 
313  template < template < typename > class ALLOC >
314  void DBTranslatorSet< ALLOC >::eraseTranslator(const std::size_t k,
315  const bool k_is_input_col) {
316  ALLOC< DBTranslator< ALLOC > > allocator(this->getAllocator());
317  const std::size_t nb_trans = __translators.size();
318 
319  if (!k_is_input_col) {
320  if (nb_trans < k) return;
321 
322  // remove the translator and its corresponding column
323  allocator.destroy(__translators[k]);
324  allocator.deallocate(__translators[k], 1);
325 
326  const std::size_t colk = __columns[k];
327  __translators.erase(__translators.begin() + k);
328  __columns.erase(__columns.begin() + k);
329 
330  // if the highest column index corresponded to the kth translator,
331  // we must recomput it
332  if (__highest_column == colk) {
333  __highest_column = std::size_t(0);
334  for (const auto col : __columns)
335  if (__highest_column < col) __highest_column = col;
336  }
337  } else {
338  // remove all the translators parsing the kth column
339  auto iter_trans = __translators.rbegin();
340  bool translator_found = false;
341  for (auto iter_col = __columns.rbegin(); iter_col != __columns.rend();
342  ++iter_col, ++iter_trans) {
343  if (*iter_col == k) {
344  // remove the translator and its corresponding column
345  allocator.destroy(*iter_trans);
346  allocator.deallocate(*iter_trans, 1);
347 
348  __translators.erase((iter_trans + 1).base());
349  __columns.erase((iter_col + 1).base());
350  translator_found = true;
351  }
352  }
353 
354  // if the highest column index corresponded to one of the translators
355  // removed, we must recompute it
356  if (translator_found && (k == __highest_column)) {
357  __highest_column = std::size_t(0);
358  for (const auto col : __columns)
359  if (__highest_column < col) __highest_column = col;
360  }
361  }
362  }
363 
364 
366  template < template < typename > class ALLOC >
367  template < template < typename > class OTHER_ALLOC >
368  INLINE DBTranslatedValue DBTranslatorSet< ALLOC >::translate(
369  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
370  const std::size_t k) const {
371  return __translators[k]->translate(row[__columns[k]]);
372  }
373 
374 
376  template < template < typename > class ALLOC >
377  template < template < typename > class OTHER_ALLOC >
378  INLINE DBTranslatedValue DBTranslatorSet< ALLOC >::translateSafe(
379  const std::vector< std::string, OTHER_ALLOC< std::string > >& row,
380  const std::size_t k) const {
381  if (__translators.size() <= k)
382  GUM_ERROR(UndefinedElement, "Translator #" << k << " could not be found");
383  return __translators[k]->translate(row[__columns[k]]);
384  }
385 
386 
388  template < template < typename > class ALLOC >
389  INLINE std::string DBTranslatorSet< ALLOC >::translateBack(
390  const DBTranslatedValue translated_val, const std::size_t k) const {
391  return __translators[k]->translateBack(translated_val);
392  }
393 
394 
396  template < template < typename > class ALLOC >
398  const DBTranslatedValue translated_val, const std::size_t k) const {
399  if (__translators.size() <= k)
400  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
401  return __translators[k]->translateBack(translated_val);
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 DBTranslatedValue translated_val, const std::size_t k) const {
410  return __translators[k]->isMissingValue(translated_val);
411  }
412 
413 
414  // indicates whether the kth translator considers a translated_val
415  // as a missing value
416  template < template < typename > class ALLOC >
418  const DBTranslatedValue translated_val, const std::size_t k) const {
419  if (__translators.size() <= k)
420  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
421  return __translators[k]->isMissingValue(translated_val);
422  }
423 
424 
426  template < template < typename > class ALLOC >
427  INLINE DBTranslator< ALLOC >&
428  DBTranslatorSet< ALLOC >::translator(const std::size_t k) {
429  return *(__translators[k]);
430  }
431 
432 
434  template < template < typename > class ALLOC >
435  INLINE const DBTranslator< ALLOC >&
436  DBTranslatorSet< ALLOC >::translator(const std::size_t k) const {
437  return *(__translators[k]);
438  }
439 
440 
442  template < template < typename > class ALLOC >
443  INLINE DBTranslator< ALLOC >&
444  DBTranslatorSet< ALLOC >::translatorSafe(const std::size_t k) {
445  if (__translators.size() <= k)
446  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
447  return *(__translators[k]);
448  }
449 
450 
452  template < template < typename > class ALLOC >
453  INLINE const DBTranslator< ALLOC >&
454  DBTranslatorSet< ALLOC >::translatorSafe(const std::size_t k) const {
455  if (__translators.size() <= k)
456  GUM_ERROR(UndefinedElement, "Translator #" << k << "could not be found");
457  return *(__translators[k]);
458  }
459 
460 
462  template < template < typename > class ALLOC >
463  INLINE std::size_t
464  DBTranslatorSet< ALLOC >::domainSize(const std::size_t k) const {
465  return __translators[k]->domainSize();
466  }
467 
468 
470  template < template < typename > class ALLOC >
471  INLINE std::size_t
472  DBTranslatorSet< ALLOC >::domainSizeSafe(const std::size_t k) const {
473  if (__translators.size() <= k)
474  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
475  return __translators[k]->domainSize();
476  }
477 
478 
480  template < template < typename > class ALLOC >
481  INLINE const Variable&
482  DBTranslatorSet< ALLOC >::variable(const std::size_t k) const {
483  return *(__translators[k]->variable());
484  }
485 
486 
488  template < template < typename > class ALLOC >
489  INLINE const Variable&
490  DBTranslatorSet< ALLOC >::variableSafe(const std::size_t k) const {
491  if (__translators.size() <= k)
492  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
493  return *(__translators[k]->variable());
494  }
495 
496 
497  // indicates whether a reordering is needed to make the kth translator
498  // sorted by lexicographical order
499  template < template < typename > class ALLOC >
500  INLINE bool
501  DBTranslatorSet< ALLOC >::needsReordering(const std::size_t k) const {
502  return __translators[k]->needsReordering();
503  }
504 
505 
506  // indicates whether a reordering is needed to make the kth translator
507  // sorted by lexicographical order
508  template < template < typename > class ALLOC >
509  INLINE bool
510  DBTranslatorSet< ALLOC >::needsReorderingSafe(const std::size_t k) const {
511  if (__translators.size() <= k)
512  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
513  return __translators[k]->needsReordering();
514  }
515 
516 
517  // performs a reordering of the dictionary and returns a mapping
518  // from the old translated values to the new ones.
519  template < template < typename > class ALLOC >
520  INLINE HashTable< std::size_t,
521  std::size_t,
522  ALLOC< std::pair< std::size_t, std::size_t > > >
523  DBTranslatorSet< ALLOC >::reorder(const std::size_t k) {
524  return __translators[k]->reorder();
525  }
526 
527 
528  // performs a reordering of the dictionary and returns a mapping
529  // from the old translated values to the new ones.
530  template < template < typename > class ALLOC >
531  INLINE HashTable< std::size_t,
532  std::size_t,
533  ALLOC< std::pair< std::size_t, std::size_t > > >
534  DBTranslatorSet< ALLOC >::reorderSafe(const std::size_t k) {
535  if (__translators.size() <= k)
536  GUM_ERROR(UndefinedElement, "Variable #" << k << "could not be found");
537  return __translators[k]->reorder();
538  }
539 
540 
543  template < template < typename > class ALLOC >
544  INLINE std::size_t
545  DBTranslatorSet< ALLOC >::inputColumn(const std::size_t k) const {
546  return __columns[k];
547  }
548 
549 
552  template < template < typename > class ALLOC >
553  INLINE std::size_t
554  DBTranslatorSet< ALLOC >::inputColumnSafe(const std::size_t k) const {
555  if (__translators.size() <= k)
556  GUM_ERROR(UndefinedElement, "Column #" << k << "could not be found");
557  return __columns[k];
558  }
559 
560 
562  template < template < typename > class ALLOC >
563  INLINE std::size_t DBTranslatorSet< ALLOC >::highestInputColumn() const {
564  return __highest_column;
565  }
566 
567 
569  template < template < typename > class ALLOC >
570  INLINE std::size_t DBTranslatorSet< ALLOC >::nbTranslators() const {
571  return __columns.size();
572  }
573 
574 
576  template < template < typename > class ALLOC >
577  INLINE std::size_t DBTranslatorSet< ALLOC >::size() const {
578  return __columns.size();
579  }
580 
582  template < template < typename > class ALLOC >
583  INLINE const
584  std::vector< DBTranslator< ALLOC >*, ALLOC< DBTranslator< ALLOC >* > >&
586  return __translators;
587  }
588 
589  } /* namespace learning */
590 
591 } /* namespace gum */
592 
593 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorder(const std::size_t k)
performs a reordering of the dictionary and returns a mapping from the old translated values to the n...
const Variable & variable(const std::size_t k) const
returns the variable stored into the kth translator
const std::vector< DBTranslator< ALLOC > *, ALLOC< DBTranslator< ALLOC > *> > & translators() const
returns the set of translators
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< ALLOC > & operator=(const DBTranslatorSet< ALLOC > &from)
copy operator
std::string translateBack(const DBTranslatedValue translated_val, const std::size_t k) const
returns the original string that was translated into translated_val
const Variable & variableSafe(const std::size_t k) const
returns the variable stored into the kth translator
std::size_t domainSizeSafe(const std::size_t k) const
returns the domain size of the variable stored into the kth translator
std::size_t highestInputColumn() const
returns the largest input database column index read by the translators
DBTranslator< ALLOC > & translatorSafe(const std::size_t k)
returns the kth translator
STL namespace.
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
std::size_t inputColumnSafe(const std::size_t k) const
returns the column of the input database that will be read by the kth translator
std::size_t nbTranslators() const
returns the number of translators stored into the set
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
DBTranslatorSet(const allocator_type &alloc=allocator_type())
default constructor
DBTranslator< ALLOC > & operator[](const std::size_t k)
returns the kth translator
std::size_t domainSize(const std::size_t k) const
returns the domain size of the variable stored into the kth translator
DBTranslator< ALLOC > & translator(const std::size_t k)
returns the kth translator
bool isMissingValueSafe(const DBTranslatedValue translated_val, const std::size_t k) const
similar to method isMissingValue, except that it checks that the kth translator exists ...
allocator_type getAllocator() const
returns the allocator used by the translator set
std::size_t insertTranslator(const Translator< ALLOC > &translator, const std::size_t column, const bool unique_column=true)
inserts a new translator at the end of the translator set
bool needsReordering(const std::size_t k) const
indicates whether a reordering is needed to make the kth translator sorted
virtual ~DBTranslatorSet()
destructor
std::size_t inputColumn(const std::size_t k) const
returns the column of the input database that will be read by the kth translator
std::string translateBackSafe(const DBTranslatedValue translated_val, const std::size_t k) const
similar to method translateBack, except that it checks that the kth translator exists ...
std::size_t size() const
returns the number of translators stored into the set
DBTranslatedValue translateSafe(const std::vector< std::string, OTHER_ALLOC< std::string > > &row, const std::size_t k) const
similar to method translate, except that it checks that the kth translator exists ...
bool needsReorderingSafe(const std::size_t k) const
same as method needsReordering but checks that the kth translator exists
DBTranslatedValue translate(const std::vector< std::string, OTHER_ALLOC< std::string > > &row, const std::size_t k) const
ask the kth translator to translate a string in a row of the database
void clear()
remove all the translators
HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorderSafe(const std::size_t k)
same as method reorder but checks that the kth translator exists
virtual DBTranslatorSet< ALLOC > * clone() const
virtual copy constructor
bool isMissingValue(const DBTranslatedValue translated_val, const std::size_t k) const
indicates whether the kth translator considers a translated_val as a missing value ...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55