aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::learning::DBHandler< T_DATA > Class Template Referenceabstract

The base class for all database handlers. More...

#include <agrum/base/database/DBHandler.h>

Inheritance diagram for gum::learning::DBHandler< T_DATA >:

Public Types

template<typename TX_DATA>
using DBVector = std::vector< TX_DATA >
 the type for the allocated vectors in IDatabases
using iterator_category = std::random_access_iterator_tag
 Types for STL compliance.
using value_type = DBRow< T_DATA >
 Types for STL compliance.
using reference = value_type&
 Types for STL compliance.
using const_reference = const value_type&
 Types for STL compliance.
using pointer = value_type*
 Types for STL compliance.
using const_pointer = const value_type*
 Types for STL compliance.
using size_type = std::size_t
 Types for STL compliance.
using difference_type = std::ptrdiff_t
 Types for STL compliance.

Public Member Functions

Accessors / Modifiers
virtual std::size_t size () const =0
 returns the number of rows managed by the handler
virtual std::size_t DBSize () const =0
 the number of rows in the whole database
virtual const_reference rowSafe () const =0
 returns the current row of the database (safe version)
virtual reference rowSafe ()=0
 returns the current row of the database (safe version)
virtual const_reference row () const =0
 returns the current row pointed to by the handler (unsafe version)
virtual reference row ()=0
 returns the current row pointed to by the handler (unsafe version)
virtual void nextRow ()=0
 go to the next row in the database
virtual std::size_t numRow () const =0
 number of row the handler points to (from the beginning of the area)
virtual bool hasRows () const =0
 indicates wether there are still rows to parse in the database
virtual void reset ()=0
 puts the handler to the beginning of the database area it handles
virtual void setRange (std::size_t begin, std::size_t end)=0
 sets the range of rows in the database that the handler will parse
virtual std::pair< std::size_t, std::size_t > range () const =0
 returns the current range of rows of the handler
virtual const DBVector< std::string > & variableNames () const =0
 returns the names of the variables corresponding to the rows
virtual std::size_t nbVariables () const =0
 returns the number of variables (columns) of the database

Detailed Description

template<typename T_DATA>
class gum::learning::DBHandler< T_DATA >

The base class for all database handlers.

Here is an example of how to use this class, illustrated on handlers for a RawDatabaseTable:

// create the database
database.setVariableNames( std::vector<std::string> { "v1", "v2", "v3" } );
// add one row to the database
database.insertRow( row );
// create a safe and an unsafe handler. Those inherit from DBHandler
typename gum::learning::RawDatabaseTable::HandlerSafe handler( database );
typename gum::learning::RawDatabaseTable::Handler uhandler( database );
// by default, the handlers range over the whole database, which
// currently contains only one row
// here, we add 5 new rows into the database
for ( int i = 0; i < 5; ++i ) database.insertRow( row );
// due to the addition of the rows, the safe handler is aware that there
// are now 6 rows. The unsafe handler still thinks there is only one row
std::cout << handler.range().second; // displays 6 (the last area's element)
std::cout << handler.size (); // displays 6 (handler's range)
std::cout << handler.DBSize (); // displays 6 (database's size)
std::cout << uhandler.size (); // displays 1 (handler's range)
std::cout << uhandler.DBSize (); // displays 6 (database's size)
// change the range of rows handled by the DBHandler
std::cout << handler.setRange ( 1, 4 ); // now parses rows [1,4)
std::cout << handler.size (); // displays 3: rows 1, 2, and 3
std::cout << handler.DBSize (); // displays 6: database's size
std::cout << handler.numRow (); // displays 0: the handler currently
// points on the first row of its managed area [1,4)
uhandler.setRange ( 1, 4 ); // uhandler now parsed rows [1,4)
std::cout << uhandler.size (); // displays 3: rows 1, 2, and 3
std::cout << uhandler.DBSize (); // displays 6: database's size
std::cout << uhandler.numRow (); // displays 0: the handler currently
// points on the first row of its managed area [1,4)
// move the handler to the next row
handler.nextRow();
std::cout << handler.numRow (); // displays 1: the handler points now
// on the second row of its managed area. This corresponds to the third
// DBRow of the database since the range of handler is [1,4)
// get the DBRow pointed to by the handler: this is the 3rd DBRow
// of the database
auto& xrow2 = handler.row (); // get the DBRow, unsafe version
auto& yrow2 = handler.rowSafe (); // get the DBRow, safe version
const std::vector<gum::learning::DBCell>& xrow = xrow2.row ();
const double xweight = xrow2.weight ();
// check whether there exist other rows managed by the handler after
// the current row
bool has_rows = handler.hasRows (); // true: there is still the 4th row
handler.nextRow();
bool has_rows2 = handler.hasRows (); // false: the 4th row is the last one
// makes the handler point again on the 2nd row of the database
handler.reset ();
std::cout << handler.numRow (); // displays 0: the handler currently
// points on the first row of its managed area [1,4)
// see the variables' names, i.e., the names of the database's columns
const auto& vars = handler.variableNames();
The class representing the original values of the cells of databases.
Definition DBCell.h:93
virtual const_reference row() const =0
returns the current row pointed to by the handler (unsafe version)
The class for storing a record in a database.
Definition DBRow.h:75
The table containing the raw/original data of a database.
typename IDatabaseTable< DBCell >::Handler Handler
the unsafe handler type
void insertRow(const std::vector< std::string > &new_row) final
insert a new row at the end of the database
void setVariableNames(const std::vector< std::string > &names, const bool from_external_object=true) final
sets the names of the variables
typename IDatabaseTable< DBCell >::HandlerSafe HandlerSafe
the safe handler type

Definition at line 140 of file DBHandler.h.

Member Typedef Documentation

◆ const_pointer

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::const_pointer = const value_type*

Types for STL compliance.

Definition at line 149 of file DBHandler.h.

◆ const_reference

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::const_reference = const value_type&

Types for STL compliance.

Definition at line 147 of file DBHandler.h.

◆ DBVector

template<typename T_DATA>
template<typename TX_DATA>
using gum::learning::DBHandler< T_DATA >::DBVector = std::vector< TX_DATA >

the type for the allocated vectors in IDatabases

Definition at line 156 of file DBHandler.h.

◆ difference_type

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 151 of file DBHandler.h.

◆ iterator_category

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::iterator_category = std::random_access_iterator_tag

Types for STL compliance.

Definition at line 144 of file DBHandler.h.

◆ pointer

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::pointer = value_type*

Types for STL compliance.

Definition at line 148 of file DBHandler.h.

◆ reference

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::reference = value_type&

Types for STL compliance.

Definition at line 146 of file DBHandler.h.

◆ size_type

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::size_type = std::size_t

Types for STL compliance.

Definition at line 150 of file DBHandler.h.

◆ value_type

template<typename T_DATA>
using gum::learning::DBHandler< T_DATA >::value_type = DBRow< T_DATA >

Types for STL compliance.

Definition at line 145 of file DBHandler.h.

Member Function Documentation

◆ DBSize()

template<typename T_DATA>
virtual std::size_t gum::learning::DBHandler< T_DATA >::DBSize ( ) const
pure virtual

the number of rows in the whole database

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ hasRows()

template<typename T_DATA>
virtual bool gum::learning::DBHandler< T_DATA >::hasRows ( ) const
pure virtual

indicates wether there are still rows to parse in the database

Remember that the handler manages only a specified area of the database, so the method just indicates whether there still remains rows in the area.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ nbVariables()

template<typename T_DATA>
virtual std::size_t gum::learning::DBHandler< T_DATA >::nbVariables ( ) const
pure virtual

returns the number of variables (columns) of the database

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ nextRow()

template<typename T_DATA>
virtual void gum::learning::DBHandler< T_DATA >::nextRow ( )
pure virtual

go to the next row in the database

Warning
If there is no more row, i.e., you are already on the last DBRow managed or you point already to the end of the area, then the handler will point on the end of the area. In particular, this will not raise any exception.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ numRow()

template<typename T_DATA>
virtual std::size_t gum::learning::DBHandler< T_DATA >::numRow ( ) const
pure virtual

number of row the handler points to (from the beginning of the area)

This method assigns 0 to the first row in the area handled by the handler. So the number returned is the number of rows between the currently pointed one to the beginning of the area handled.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ range()

template<typename T_DATA>
virtual std::pair< std::size_t, std::size_t > gum::learning::DBHandler< T_DATA >::range ( ) const
pure virtual

returns the current range of rows of the handler

The range returned is of type [begin,end), i.e., the first row of the range managed by the DBHandler has index begin in the whole database, and the last row of the range has index end-1 in the whole database. The endth row is therefore the first one outside the range.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ reset()

template<typename T_DATA>
virtual void gum::learning::DBHandler< T_DATA >::reset ( )
pure virtual

puts the handler to the beginning of the database area it handles

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ row() [1/2]

template<typename T_DATA>
virtual const_reference gum::learning::DBHandler< T_DATA >::row ( ) const
pure virtual

returns the current row pointed to by the handler (unsafe version)

Warning
The method does not check whether the handler already points to the end of its area. It is thus faster than method rowSafe () but, when you call it, you must be sure that the row actually exists, i.e., that the handler has not reached its end.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ row() [2/2]

template<typename T_DATA>
virtual reference gum::learning::DBHandler< T_DATA >::row ( )
pure virtual

returns the current row pointed to by the handler (unsafe version)

Warning
The method does not check whether the handler already points to the end of its area. It is thus faster than method rowSafe () but, when you call it, you must be sure that the row actually exists, i.e., that the handler has not reached its end.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ rowSafe() [1/2]

template<typename T_DATA>
virtual const_reference gum::learning::DBHandler< T_DATA >::rowSafe ( ) const
pure virtual

returns the current row of the database (safe version)

Exceptions
OutOfBoundsis raised if the handler points outside of its area

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ rowSafe() [2/2]

template<typename T_DATA>
virtual reference gum::learning::DBHandler< T_DATA >::rowSafe ( )
pure virtual

returns the current row of the database (safe version)

Exceptions
OutOfBoundsis raised if the handler points outside of its area

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ setRange()

template<typename T_DATA>
virtual void gum::learning::DBHandler< T_DATA >::setRange ( std::size_t begin,
std::size_t end )
pure virtual

sets the range of rows in the database that the handler will parse

The range provided in arguments specifies that area [begin,end) is the one managed by the DBHandler, i.e., the first row of the area has index begin in the whole database, and the last row of the area has index end-1 in the whole database. The endth row is the first one outside the area.

Parameters
beginthe number of the row in the whole database that will be the first one in the area managed by the DBHandler.
endthe number of the row in the whole database from which the DBHandler considers it is outside of its area.

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ size()

template<typename T_DATA>
virtual std::size_t gum::learning::DBHandler< T_DATA >::size ( ) const
pure virtual

returns the number of rows managed by the handler

A handler needs not necessarily handle all the rows of the database. For instance, RecordCounters cut the database into several pieces and assign each piece to a handler. Then each handler is used in parallel to perform counts only on a subset of the database

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.

◆ variableNames()

template<typename T_DATA>
virtual const DBVector< std::string > & gum::learning::DBHandler< T_DATA >::variableNames ( ) const
pure virtual

returns the names of the variables corresponding to the rows

Implemented in gum::learning::IDatabaseTable< T_DATA >::Handler.


The documentation for this class was generated from the following file: