aGrUM  0.16.0
IDatabaseTable.h
Go to the documentation of this file.
1 
31 #ifndef GUM_IDATABASE_TABLE_H
32 #define GUM_IDATABASE_TABLE_H
33 
34 #include <cstddef>
35 #include <utility>
36 #include <string>
37 #include <cstring>
38 #include <memory>
39 #include <vector>
40 #include <mutex>
41 
42 #include <agrum/agrum.h>
43 #include <agrum/core/thread.h>
44 #include <agrum/core/OMPThreads.h>
49 
50 
51 namespace gum {
52 
53  namespace learning {
54 
55  template < template < typename > class ALLOC, bool ENABLE_INSERT >
57 
58  template < template < typename > class ALLOC >
59  struct IDatabaseTableInsert4DBCell< ALLOC, true > {
60  template < typename TX_DATA >
61  using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
62 
63  template < typename TX_DATA >
65 
66  template < typename TX_DATA >
67  using Matrix =
68  std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > >;
69 
70 
72 
74  virtual void insertRow(Row< DBCell >&& new_row) = 0;
75 
77 
79  virtual void insertRow(const Row< DBCell >& new_row) = 0;
80 
82 
84  virtual void insertRows(Matrix< DBCell >&& new_rows) = 0;
85 
87 
89  virtual void insertRows(const Matrix< DBCell >& new_rows) = 0;
90 
92 
94  virtual void insertRow(
95  const std::vector< std::string, ALLOC< std::string > >& new_row) = 0;
96 
98 
100  virtual void insertRows(const DBVector< DBVector< std::string > >& new_rows);
101  };
102 
103 
104  template < template < typename > class ALLOC >
105  struct IDatabaseTableInsert4DBCell< ALLOC, false > {
106  template < typename TX_DATA >
107  using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
108 
109  template < typename TX_DATA >
111 
112  template < typename TX_DATA >
113  using Matrix =
114  std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > >;
115 
117 
119  virtual void insertRow(
120  const std::vector< std::string, ALLOC< std::string > >& new_row) = 0;
121 
123 
125  virtual void insertRows(const DBVector< DBVector< std::string > >& new_rows);
126  };
127 
128 
255  template < typename T_DATA,
256  template < typename > class ALLOC = std::allocator >
259  ALLOC,
260  !std::is_same< T_DATA, DBCell >::value >
261  , private ALLOC< T_DATA > {
262  public:
264  template < typename TX_DATA >
265  using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
266 
268  template < typename TX_DATA >
270 
272  template < typename TX_DATA >
273  using Matrix =
274  std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > >;
275 
276  template < template < typename > class XALLOC >
277  using MissingValType = std::vector< std::string, XALLOC< std::string > >;
278 
279 
280  enum IsMissing : char { False, True };
281 
282 
374  class Handler : public DBHandler< T_DATA, ALLOC > {
375  public:
378  using iterator_category = std::random_access_iterator_tag;
381  using const_reference = const value_type&;
382  using pointer = value_type*;
383  using const_pointer = const value_type*;
384  using difference_type = std::ptrdiff_t;
385  using allocator_type = ALLOC< T_DATA >;
387 
388 
389  template < typename TX_DATA >
390  using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
391 
392  template < typename TX_DATA >
394 
395  template < typename TX_DATA >
396  using Matrix = std::vector< DBRow< TX_DATA, ALLOC >,
397  ALLOC< DBRow< TX_DATA, ALLOC > > >;
398 
399 
400  // ########################################################################
402  // ########################################################################
404 
406 
409 
411 
412  Handler(const Handler& h);
413 
415 
416  Handler(Handler&& h);
417 
419  virtual ~Handler();
420 
422 
423  // ########################################################################
425  // ########################################################################
427 
429  virtual Handler& operator=(const Handler&);
430 
432  virtual Handler& operator=(Handler&&);
433 
435 
437  virtual Handler& operator++() final;
438 
440 
443  virtual Handler& operator--() final;
444 
446 
449  virtual Handler& operator+=(const std::size_t i) final;
450 
452 
455  virtual Handler& operator-=(const std::size_t i) final;
456 
458  virtual bool operator==(const Handler& handler) const final;
459 
461  virtual bool operator!=(const Handler& handler) const final;
462 
464 
468  virtual const_reference operator*() const final;
469 
471 
475  virtual const_pointer operator->() const final;
476 
478 
479 
480  // ########################################################################
482  // ########################################################################
484 
486 
493  virtual std::size_t size() const final;
494 
496  virtual std::size_t DBSize() const final;
497 
499 
500  virtual const_reference rowSafe() const final;
501 
503 
504  virtual reference rowSafe() final;
505 
507 
511  virtual const_reference row() const final;
512 
514 
518  virtual reference row() final;
519 
521  virtual void nextRow() final;
522 
524  virtual std::size_t numRow() const final;
525 
527  virtual bool hasRows() const final;
528 
530  virtual void reset() final;
531 
537  virtual Handler begin() const;
538 
544  virtual Handler end() const;
545 
547 
557  virtual void setRange(std::size_t begin, std::size_t end) final;
558 
560  virtual std::pair< std::size_t, std::size_t > range() const final;
561 
563  virtual const DBVector< std::string >& variableNames() const final;
564 
566  virtual std::size_t nbVariables() const final;
567 
569 
571  virtual const IDatabaseTable< T_DATA, ALLOC >& database() const;
572 
574 
575 
576 #ifndef DOXYGEN_SHOULD_SKIP_THIS
577 
578  protected:
581 
583 
585  const Matrix< T_DATA >* __row;
586 
588  std::size_t __index{std::size_t(0)};
589 
591  std::size_t __begin_index{std::size_t(0)};
592 
594  std::size_t __end_index{std::size_t(0)};
595 
596  friend class IDatabaseTable< T_DATA, ALLOC >;
597 
598 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
599  };
600 
601 
693  class HandlerSafe : public Handler {
694  public:
697  using iterator_category = std::random_access_iterator_tag;
698  using value_type = typename Handler::value_type;
700  using const_reference = const value_type&;
701  using pointer = value_type*;
702  using const_pointer = const value_type*;
703  using difference_type = std::ptrdiff_t;
704  using allocator_type = ALLOC< T_DATA >;
706 
707  // ########################################################################
709  // ########################################################################
711 
713 
716 
718  HandlerSafe(const HandlerSafe& h);
719 
722 
724  virtual ~HandlerSafe();
725 
727 
728  // ########################################################################
730  // ########################################################################
732 
734  virtual HandlerSafe& operator=(const HandlerSafe&);
735 
737  virtual HandlerSafe& operator=(const Handler&);
738 
740  virtual HandlerSafe& operator=(HandlerSafe&&);
741 
743  virtual HandlerSafe& operator=(Handler&&);
744 
746 
747 
748 #ifndef DOXYGEN_SHOULD_SKIP_THIS
749 
750  private:
752  void __attachHandler();
753 
755  void __detachHandler();
756 
757  friend class IDatabaseTable< T_DATA, ALLOC >;
758 
759 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
760  };
761 
762 
767  using const_reference = const value_type&;
768  using pointer = value_type*;
769  using const_pointer = const value_type*;
770  using size_type = std::size_t;
771  using difference_type = std::ptrdiff_t;
772  using iterator = Handler;
773  using iterator_safe = HandlerSafe;
774  using const_iterator = const Handler;
775  using const_iterator_safe = const HandlerSafe;
776  using allocator_type = ALLOC< T_DATA >;
778 
779 
780  // ##########################################################################
782  // ##########################################################################
784 
786  template < template < typename > class VARALLOC,
787  template < typename >
788  class MISSALLOC >
790  const MissingValType< MISSALLOC >& missing_symbols,
791  const std::vector< std::string, VARALLOC< std::string > >& var_names,
792  const ALLOC< T_DATA >& alloc);
793 
796 
799  const allocator_type& alloc);
800 
803 
806  const allocator_type& alloc);
807 
809  virtual IDatabaseTable< T_DATA, ALLOC >* clone() const = 0;
810 
813  clone(const allocator_type& alloc) const = 0;
814 
816  virtual ~IDatabaseTable();
817 
819 
820 
821  // ##########################################################################
823  // ##########################################################################
825 
827  iterator begin() const;
828 
830  iterator_safe beginSafe() const;
831 
833  const iterator& end() const noexcept;
834 
836  const iterator_safe& endSafe() const noexcept;
837 
839 
840 
841  // ##########################################################################
843  // ##########################################################################
845 
847  const Matrix< T_DATA >& content() const noexcept;
848 
850  iterator handler() const;
851 
853  iterator_safe handlerSafe() const;
854 
856 
857  const DBVector< std::string >& variableNames() const noexcept;
858 
860 
885  virtual void setVariableNames(
886  const std::vector< std::string, ALLOC< std::string > >& names,
887  const bool from_external_object = true) = 0;
888 
890 
915  template < template < typename > class OTHER_ALLOC >
916  void setVariableNames(
917  const std::vector< std::string, OTHER_ALLOC< std::string > >& names,
918  const bool from_external_object = true);
919 
921 
923  const std::string& variableName(const std::size_t k) const;
924 
926 
931  std::size_t columnFromVariableName(const std::string& name) const;
932 
934 
937  DBVector< std::size_t >
938  columnsFromVariableName(const std::string& name) const;
939 
941  std::size_t nbVariables() const noexcept;
942 
944  std::size_t nbRows() const noexcept;
945 
947  std::size_t size() const noexcept;
948 
950  bool empty() const noexcept;
951 
953 
982  virtual void ignoreColumn(const std::size_t k,
983  const bool from_external_object = true) = 0;
984 
986  virtual const DBVector< std::size_t > ignoredColumns() const = 0;
987 
990  virtual const DBVector< std::size_t > inputColumns() const = 0;
991 
993  ALLOC,
994  !std::is_same< T_DATA, DBCell >::value >::insertRow;
995 
997 
1002  template < template < typename > class OTHER_ALLOC >
1003  void insertRow(
1004  const std::vector< std::string, OTHER_ALLOC< std::string > >& new_row);
1005 
1007 
1013  virtual void insertRow(Row< T_DATA >&& new_row,
1014  const IsMissing contains_missing_data);
1015 
1017 
1023  virtual void insertRow(const Row< T_DATA >& new_row,
1024  const IsMissing contains_missing_data);
1025 
1027  ALLOC,
1028  !std::is_same< T_DATA, DBCell >::value >::insertRows;
1029 
1031 
1043  virtual void insertRows(Matrix< T_DATA >&& new_rows,
1044  const DBVector< IsMissing >& rows_have_missing_vals);
1045 
1047 
1059  virtual void insertRows(const Matrix< T_DATA >& new_rows,
1060  const DBVector< IsMissing >& rows_have_missing_vals);
1061 
1063 
1066  void eraseRow(std::size_t index);
1067 
1069 
1071  void eraseFirstRow();
1072 
1074 
1076  void eraseLastRow();
1077 
1079 
1081  void eraseFirstRows(const std::size_t k);
1082 
1084 
1086  void eraseLastRows(const std::size_t k);
1087 
1089 
1090  void eraseRows(std::size_t deb, std::size_t end);
1091 
1093  void eraseAllRows();
1094 
1096  virtual void clear();
1097 
1099  ALLOC< T_DATA > getAllocator() const;
1100 
1102  const DBVector< std::string >& missingSymbols() const;
1103 
1105  bool hasMissingValues() const;
1106 
1108  bool hasMissingValues(const std::size_t k) const;
1109 
1111 
1114  void setMaxNbThreads(const std::size_t nb) const;
1115 
1117  std::size_t nbThreads() const;
1118 
1128  void setMinNbRowsPerThread(const std::size_t nb) const;
1129 
1131  std::size_t minNbRowsPerThread() const;
1132 
1134  void setAllRowsWeight(const double new_weight);
1135 
1137 
1139  void setWeight(const std::size_t i, const double weight);
1140 
1142 
1144  double weight(const std::size_t i) const;
1145 
1147  double weight() const;
1148 
1150 
1151 
1152  protected:
1154  DBVector< std::string > _variable_names;
1155 
1156  // the vector of DBRows containing all the raw data
1157  Matrix< T_DATA > _rows;
1158 
1159  // the set of string corresponding to missing values
1160  DBVector< std::string > _missing_symbols;
1161 
1162  // a vector indicating which rows have missing values (char != 0)
1163  DBVector< IsMissing > _has_row_missing_val;
1164 
1165  // the maximal number of threads that the database can use
1166  mutable std::size_t _max_nb_threads{
1167  std::size_t(gum::getMaxNumberOfThreads())};
1168 
1169  // the min number of rows that a thread should process in a
1170  // multithreading context
1171  mutable std::size_t _min_nb_rows_per_thread{100};
1172 
1173 
1176  bool _isRowSizeOK(const std::size_t size) const;
1177 
1180  operator=(const IDatabaseTable< T_DATA, ALLOC >& from);
1181 
1184  operator=(IDatabaseTable< T_DATA, ALLOC >&& from);
1185 
1186 
1187 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1188 
1189  private:
1190  // the list of handlers currently attached to the database
1191  /* this is useful when the database is resized */
1192  mutable DBVector< HandlerSafe* > __list_of_safe_handlers;
1193 
1194  // a mutex to safely add/remove handlers in __list_of_safe_handlers
1195  mutable std::mutex __safe_handlers_mutex;
1196 
1197  // the end iterator for the database
1198  Handler* __end{nullptr};
1199 
1200  // the safe end iterator for the database
1201  iterator_safe* __end_safe{nullptr};
1202 
1204  void __attachHandler(HandlerSafe* handler) const;
1205 
1207  void __detachHandler(HandlerSafe* handler) const;
1208 
1210  void __updateHandlers(std::size_t new_size) const;
1211 
1212  // create the end iterators
1213  void __createEndIterators();
1214 
1215 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
1216 
1217 
1219  friend class Handler;
1220  friend class HandlerSafe;
1221  };
1222 
1223  } /* namespace learning */
1224 
1225 } /* namespace gum */
1226 
1229 
1230 #endif /* GUM_IDATABASE_TABLE_H */
The common class for the tabular database tables.
std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > > Matrix
std::vector< TX_DATA, ALLOC< TX_DATA > > DBVector
the type for the vectors used in the IDatabaseTable
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > > Matrix
The class representing the original values of the cells of databases.
Definition: DBCell.h:72
std::ptrdiff_t difference_type
Types for STL compliance.
Definition: DBHandler.h:132
the (unsafe) handler for the tabular databases
std::vector< TX_DATA, ALLOC< TX_DATA > > DBVector
std::vector< TX_DATA, ALLOC< TX_DATA > > DBVector
STL namespace.
std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > > Matrix
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
std::vector< DBRow< TX_DATA, ALLOC >, ALLOC< DBRow< TX_DATA, ALLOC > > > Matrix
the type for the matrices stored into the database
std::vector< std::string, XALLOC< std::string > > MissingValType
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::vector< TX_DATA, ALLOC< TX_DATA > > DBVector
the type for the allocated vectors in IDatabases
Definition: DBHandler.h:138
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The base class for all database handlers.
Definition: DBHandler.h:121
std::random_access_iterator_tag iterator_category
Types for STL compliance.
Definition: DBHandler.h:125
the safe handler of the tabular databases
The class for storing a record in a database.
Definition: DBRow.h:56
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
typename DBHandler< T_DATA, ALLOC >::value_type value_type
Types for STL compliance.
ALLOC< void > allocator_type
Types for STL compliance.
Definition: DBHandler.h:133
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.