30 #ifndef GUM_IDATABASE_TABLE_H 31 #define GUM_IDATABASE_TABLE_H 41 #include <agrum/agrum.h> 42 #include <agrum/tools/core/thread.h> 43 #include <agrum/tools/core/OMPThreads.h> 44 #include <agrum/tools/database/DBCell.h> 45 #include <agrum/tools/database/DBRow.h> 46 #include <agrum/tools/database/DBHandler.h> 47 #include <agrum/tools/database/DBTranslator.h> 54 template <
template <
typename >
class ALLOC,
bool ENABLE_INSERT >
55 struct IDatabaseTableInsert4DBCell;
57 template <
template <
typename >
class ALLOC >
104 template <
template <
typename >
class ALLOC >
256 template <
typename T_DATA,
257 template <
typename >
class ALLOC = std::allocator >
262 private ALLOC< T_DATA > {
265 template <
typename TX_DATA >
266 using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
277 template <
template <
typename >
class XALLOC >
278 using MissingValType = std::vector< std::string, XALLOC< std::string > >;
383 using iterator_category = std::random_access_iterator_tag;
389 using difference_type = std::ptrdiff_t;
390 using allocator_type = ALLOC< T_DATA >;
394 template <
typename TX_DATA >
395 using DBVector = std::vector< TX_DATA, ALLOC< TX_DATA > >;
413 Handler(
const IDatabaseTable< T_DATA, ALLOC >& db);
498 virtual std::size_t
size()
const final;
501 virtual std::size_t
DBSize()
const final;
529 virtual std::size_t
numRow()
const final;
532 virtual bool hasRows()
const final;
535 virtual void reset()
final;
562 virtual void setRange(std::size_t begin, std::size_t end)
final;
565 virtual std::pair< std::size_t, std::size_t >
range()
const final;
581 #ifndef DOXYGEN_SHOULD_SKIP_THIS 585 const IDatabaseTable< T_DATA, ALLOC >* db__;
590 const Matrix< T_DATA >* row__;
593 std::size_t index__{std::size_t(0)};
596 std::size_t begin_index__{std::size_t(0)};
599 std::size_t end_index__{std::size_t(0)};
702 using iterator_category = std::random_access_iterator_tag;
703 using value_type =
typename Handler::value_type;
704 using reference = value_type&;
705 using const_reference =
const value_type&;
706 using pointer = value_type*;
707 using const_pointer =
const value_type*;
708 using difference_type = std::ptrdiff_t;
709 using allocator_type = ALLOC< T_DATA >;
720 HandlerSafe(
const IDatabaseTable< T_DATA, ALLOC >& db);
753 #ifndef DOXYGEN_SHOULD_SKIP_THIS 757 void attachHandler__();
760 void detachHandler__();
775 using size_type = std::size_t;
776 using difference_type = std::ptrdiff_t;
781 using allocator_type = ALLOC< T_DATA >;
791 template <
template <
typename >
class VARALLOC,
792 template <
typename >
795 const MissingValType< MISSALLOC >& missing_symbols,
796 const std::vector< std::string, VARALLOC< std::string > >& var_names,
797 const ALLOC< T_DATA >& alloc);
804 const allocator_type& alloc);
811 const allocator_type& alloc);
832 iterator
begin()
const;
838 const iterator&
end()
const noexcept;
841 const iterator_safe&
endSafe()
const noexcept;
891 const std::vector< std::string, ALLOC< std::string > >& names,
892 const bool from_external_object =
true)
921 template <
template <
typename >
class OTHER_ALLOC >
923 const std::vector< std::string, OTHER_ALLOC< std::string > >& names,
924 const bool from_external_object =
true);
929 const std::string&
variableName(
const std::size_t k)
const;
950 std::size_t
nbRows()
const noexcept;
953 std::size_t
size()
const noexcept;
956 bool empty()
const noexcept;
989 const bool from_external_object =
true)
1009 template <
template <
typename >
class OTHER_ALLOC >
1011 const std::vector< std::string, OTHER_ALLOC< std::string > >& new_row);
1020 virtual void insertRow(Row< T_DATA >&& new_row,
1030 virtual void insertRow(
const Row< T_DATA >& new_row,
1050 virtual void insertRows(Matrix< T_DATA >&& new_rows,
1051 const DBVector< IsMissing >& rows_have_missing_vals);
1066 virtual void insertRows(
const Matrix< T_DATA >& new_rows,
1067 const DBVector< IsMissing >& rows_have_missing_vals);
1097 void eraseRows(std::size_t deb, std::size_t end);
1103 virtual void clear();
1146 void setWeight(
const std::size_t i,
const double weight);
1151 double weight(
const std::size_t i)
const;
1194 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1199 mutable DBVector< HandlerSafe* > list_of_safe_handlers__;
1202 mutable std::mutex safe_handlers_mutex__;
1208 iterator_safe* end_safe__{
nullptr};
1217 void updateHandlers__(std::size_t new_size)
const;
1220 void createEndIterators__();
1235 #include <agrum/tools/database/IDatabaseTable_tpl.h> virtual void insertRow(const Row< T_DATA > &new_row, const IsMissing contains_missing_data)
insert a new row at the end of the database
virtual HandlerSafe & operator=(const Handler &)
copy operator
virtual Handler & operator--() final
makes the operator point to the previous row in the database
HandlerSafe(const IDatabaseTable< T_DATA, ALLOC > &db)
default constructor
void eraseFirstRows(const std::size_t k)
erase the k first rows
void insertRow(const std::vector< std::string, OTHER_ALLOC< std::string > > &new_row)
insert a new row at the end of the database
virtual const_reference row() const final
returns the current row pointed to by the handler (unsafe version)
virtual bool hasRows() const final
indicates whether the handler has reached its end or not
IDatabaseTable(IDatabaseTable< T_DATA, ALLOC > &&from)
move constructor
virtual reference rowSafe() final
returns the current row pointed to by the handler (safe version)
void setMaxNbThreads(const std::size_t nb) const
changes the max number of threads that a database can use
virtual IDatabaseTable< T_DATA, ALLOC > * clone(const allocator_type &alloc) const =0
virtual copy constructor with a given allocator
std::size_t columnFromVariableName(const std::string &name) const
returns the index of the column whose name is passed in argument
virtual std::size_t DBSize() const final
returns the number of rows of the whole database
virtual void setRange(std::size_t begin, std::size_t end) final
sets the area in the database the handler will handle
virtual ~Handler()
destructor
ALLOC< T_DATA > getAllocator() const
returns the allocator of the database
DBVector< std::string > variable_names_
the names of the variables for each column
DBVector< std::size_t > columnsFromVariableName(const std::string &name) const
returns the indices of all the columns whose name is passed in argument
INLINE void emplace(Args &&... args)
void eraseRow(std::size_t index)
erase a given row specified by its index in the table
std::size_t nbThreads() const
returns the number of threads used to parse the database
virtual void insertRows(const Matrix< T_DATA > &new_rows, const DBVector< IsMissing > &rows_have_missing_vals)
insert a set of new DBRows at the end of the database
the (unsafe) handler for the tabular databases
std::size_t size() const noexcept
returns the number of records (rows) in the database
virtual const_pointer operator->() const final
Dereferences the value pointed to by the handler (unsafe version)
virtual reference row() final
returns the current row pointed to by the handler (unsafe version)
IDatabaseTable< T_DATA, ALLOC > & operator=(const IDatabaseTable< T_DATA, ALLOC > &from)
copy operator
virtual const_reference operator*() const final
returns the current row pointed to by the handler (unsafe version)
void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
const DBVector< std::string > & missingSymbols() const
returns the set of missing symbols
void eraseRows(std::size_t deb, std::size_t end)
erase the rows from the debth to the endth (not included)
virtual Handler begin() const
returns a new handler that points to the beginning of the database's area of the current handler ...
virtual std::size_t nbVariables() const final
returns the number of variables (columns) of the database
void setVariableNames(const std::vector< std::string, OTHER_ALLOC< std::string > > &names, const bool from_external_object=true)
sets the names of the variables
virtual Handler & operator-=(const std::size_t i) final
moves back the handler by i rows in the database
void eraseLastRow()
erase the last row
virtual const DBVector< std::size_t > ignoredColumns() const =0
returns the set of columns of the original dataset that are ignored
void eraseAllRows()
erase all the rows
Handler(Handler &&h)
move constructor
Handler(const Handler &h)
copy constructor
IDatabaseTable< T_DATA, ALLOC > & operator=(IDatabaseTable< T_DATA, ALLOC > &&from)
move operator
iterator_safe beginSafe() const
returns a new safe handler pointing to the beginning of the database
bool isRowSizeOK_(const std::size_t size) const
checks whether a size corresponds to the number of columns of the database
virtual void setVariableNames(const std::vector< std::string, ALLOC< std::string > > &names, const bool from_external_object=true)=0
sets the names of the variables
IDatabaseTable(const IDatabaseTable< T_DATA, ALLOC > &from)
copy constructor
virtual HandlerSafe & operator=(HandlerSafe &&)
move operator
void eraseLastRows(const std::size_t k)
erase the k last rows
const Matrix< T_DATA > & content() const noexcept
returns the content (the records) of the database
DBVector< IsMissing > has_row_missing_val_
virtual Handler end() const
returns a new handler that points to the end of the database's area of the current handler ...
std::size_t nbVariables() const noexcept
returns the number of variables (columns) of the database
virtual bool operator==(const Handler &handler) const final
checks whether two handlers point to the same row in the database
virtual HandlerSafe & operator=(Handler &&)
move operator
DBVector< std::string > missing_symbols_
friend class Handler
allow the handlers to access the database directly
IDatabaseTable(const MissingValType< MISSALLOC > &missing_symbols, const std::vector< std::string, VARALLOC< std::string > > &var_names, const ALLOC< T_DATA > &alloc)
default constructor
virtual void nextRow() final
makes the handler point to the next row, equivalent to operator++
virtual std::size_t numRow() const final
the number of the current row (0 = the 1st row managed by the handler)
const iterator_safe & endSafe() const noexcept
returns a new safe handler pointing to the end of the database
virtual bool operator!=(const Handler &handler) const final
checks whether two handlers point to different rows in the database
bool hasMissingValues() const
indicates whether the database contains some missing values
bool empty() const noexcept
indicates whether the database contains some records or not
double weight() const
returns the weight of the whole database
const iterator & end() const noexcept
returns a new unsafe handler pointing to the end of the database
virtual const_reference rowSafe() const final
returns the current row pointed to by the handler (safe version)
virtual IDatabaseTable< T_DATA, ALLOC > * clone() const =0
virtual copy constructor
std::size_t nbRows() const noexcept
returns the number of records (rows) in the database
virtual const IDatabaseTable< T_DATA, ALLOC > & database() const
returns a pointer on the database
virtual Handler & operator=(Handler &&)
move operator
virtual HandlerSafe & operator=(const HandlerSafe &)
copy operator
the safe handler of the tabular databases
virtual Handler & operator+=(const std::size_t i) final
advances the handler by i rows in the database
double weight(const std::size_t i) const
returns the weight of the ith record
virtual void insertRows(Matrix< T_DATA > &&new_rows, const DBVector< IsMissing > &rows_have_missing_vals)
insert a set of new DBRows at the end of the database
virtual void insertRow(Row< T_DATA > &&new_row, const IsMissing contains_missing_data)
insert a new DBRow at the end of the database
virtual ~HandlerSafe()
destructor
Handler(const IDatabaseTable< T_DATA, ALLOC > &db)
default constructor
IDatabaseTable(IDatabaseTable< T_DATA, ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
virtual Handler & operator=(const Handler &)
copy operator
const std::string & variableName(const std::size_t k) const
returns the name of the kth column of the IDatabaseTable
iterator_safe handlerSafe() const
returns a new safe handler pointing to the 1st record of the database
HandlerSafe(HandlerSafe &&h)
move constructor
virtual const DBVector< std::string > & variableNames() const final
returns the names of the variables
iterator begin() const
returns a new unsafe handler pointing to the beginning of the database
void setAllRowsWeight(const double new_weight)
assign a given weight to all the rows of the database
virtual const DBVector< std::size_t > inputColumns() const =0
returns the set of columns of the original dataset that are present in the IDatabaseTable ...
bool hasMissingValues(const std::size_t k) const
indicates whether the kth row contains some missing values
virtual std::size_t size() const final
returns the number of rows managed by the handler
std::size_t max_nb_threads_
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
HandlerSafe(const HandlerSafe &h)
copy constructor
virtual ~IDatabaseTable()
destructor
const DBVector< std::string > & variableNames() const noexcept
returns the variable names for all the columns of the database
virtual std::pair< std::size_t, std::size_t > range() const final
returns the current range of the handler [begin,end)
std::size_t min_nb_rows_per_thread_
virtual void ignoreColumn(const std::size_t k, const bool from_external_object=true)=0
makes the database table ignore from now on the kth column
IDatabaseTable(const IDatabaseTable< T_DATA, ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
virtual Handler & operator++() final
makes the operator point to the next row in the database
iterator handler() const
returns a new unsafe handler pointing to the 1st record of the database
void eraseFirstRow()
erase the first row
virtual void clear()
erase the content of the database, including the names of the variables
virtual void reset() final
puts the handler to the beginning of the database's area it handles
void setWeight(const std::size_t i, const double weight)
assigns a given weight to the ith row of the database