aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
IDatabaseTable.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
50#ifndef GUM_IDATABASE_TABLE_H
51#define GUM_IDATABASE_TABLE_H
52
53#include <cstddef>
54#include <cstring>
55#include <memory>
56#include <mutex>
57#include <string>
58#include <utility>
59#include <vector>
60
61#include <agrum/agrum.h>
62
68
69#include <string_view>
70
71namespace gum {
72
73 namespace learning {
74
75 template < bool ENABLE_INSERT >
77
78 template <>
80 template < typename TX_DATA >
81 using DBVector = std::vector< TX_DATA >;
82
83 template < typename TX_DATA >
85
86 template < typename TX_DATA >
87 using Matrix = std::vector< DBRow< TX_DATA > >;
88
89
91
93 virtual void insertRow(Row< DBCell >&& new_row) = 0;
94
96
98 virtual void insertRow(const Row< DBCell >& new_row) = 0;
99
101
103 virtual void insertRows(Matrix< DBCell >&& new_rows) = 0;
104
106
108 virtual void insertRows(const Matrix< DBCell >& new_rows) = 0;
109
111
113 virtual void insertRow(const std::vector< std::string >& new_row) = 0;
114
116
118 virtual void insertRows(const DBVector< DBVector< std::string > >& new_rows);
119 };
120
121 template <>
123 template < typename TX_DATA >
124 using DBVector = std::vector< TX_DATA >;
125
126 template < typename TX_DATA >
128
129 template < typename TX_DATA >
130 using Matrix = std::vector< DBRow< TX_DATA > >;
131
133
135 virtual void insertRow(const std::vector< std::string >& new_row) = 0;
136
138
140 virtual void insertRows(const DBVector< DBVector< std::string > >& new_rows);
141 };
142
269 template < typename T_DATA >
271 public IDatabaseTableInsert4DBCell< !std::is_same< T_DATA, DBCell >::value > {
272 public:
274 template < typename TX_DATA >
275 using DBVector = std::vector< TX_DATA >;
276
278 template < typename TX_DATA >
280
282 template < typename TX_DATA >
283 using Matrix = std::vector< DBRow< TX_DATA > >;
284
285 using MissingValType = std::vector< std::string >;
286
287 enum IsMissing : char { False, True };
288
380 class Handler: public DBHandler< T_DATA > {
381 public:
384 using iterator_category = std::random_access_iterator_tag;
389 using const_pointer = const value_type*;
390 using difference_type = std::ptrdiff_t;
392
393 template < typename TX_DATA >
394 using DBVector = std::vector< TX_DATA >;
395
396 template < typename TX_DATA >
398
399 template < typename TX_DATA >
400 using Matrix = std::vector< DBRow< TX_DATA > >;
401
402
403 // ########################################################################
405 // ########################################################################
407
409
412
414
415 Handler(const Handler& h);
416
418
420
422 virtual ~Handler();
423
425
426 // ########################################################################
428 // ########################################################################
430
432 virtual Handler& operator=(const Handler&);
433
436
438
440 virtual Handler& operator++() final;
441
443
446 virtual Handler& operator--() final;
447
449
452 virtual Handler& operator+=(const std::size_t i) final;
453
455
458 virtual Handler& operator-=(const std::size_t i) final;
459
461 virtual bool operator==(const Handler& handler) const final;
462
464 virtual bool operator!=(const Handler& handler) const final;
465
467
471 virtual const_reference operator*() const final;
472
474
478 virtual const_pointer operator->() const final;
479
481
482
483 // ########################################################################
485 // ########################################################################
487
489
496 std::size_t size() const final;
497
499 std::size_t DBSize() const final;
500
502
504
506
508
510
514 const_reference row() const final;
515
517
521 reference row() final;
522
524 void nextRow() final;
525
527 std::size_t numRow() const final;
528
530 bool hasRows() const final;
531
533 void reset() final;
534
540 virtual Handler begin() const;
541
547 virtual Handler end() const;
548
550
560 void setRange(std::size_t first, std::size_t last) final;
561
563 std::pair< std::size_t, std::size_t > range() const final;
564
566 const DBVector< std::string >& variableNames() const final;
567
569 std::size_t nbVariables() const final;
570
572
574 virtual const IDatabaseTable< T_DATA >& database() const;
575
577
578
579#ifndef DOXYGEN_SHOULD_SKIP_THIS
580
581 protected:
583 const IDatabaseTable< T_DATA >* _db_;
584
586
588 const Matrix< T_DATA >* _row_;
589
591 std::size_t _index_{std::size_t(0)};
592
594 std::size_t _begin_index_{std::size_t(0)};
595
597 std::size_t _end_index_{std::size_t(0)};
598
599 friend class IDatabaseTable< T_DATA >;
600
601#endif /* DOXYGEN_SHOULD_SKIP_THIS */
602 };
603
695 class HandlerSafe final: public Handler {
696 public:
699 using iterator_category = std::random_access_iterator_tag;
704 using const_pointer = const value_type*;
705 using difference_type = std::ptrdiff_t;
707
708 // ########################################################################
710 // ########################################################################
712
714
717
720
723
725 ~HandlerSafe() override;
726
728
729 // ########################################################################
731 // ########################################################################
733
736
738 HandlerSafe& operator=(const Handler&) override;
739
742
745
747
748
749#ifndef DOXYGEN_SHOULD_SKIP_THIS
750
751 private:
753 void _attachHandler_();
754
756 void _detachHandler_();
757
758 friend class IDatabaseTable< T_DATA >;
759
760#endif /* DOXYGEN_SHOULD_SKIP_THIS */
761 };
762
769 using const_pointer = const value_type*;
770 using size_type = std::size_t;
771 using difference_type = std::ptrdiff_t;
774 using const_iterator = const Handler;
777
778
779 // ##########################################################################
781 // ##########################################################################
783
785 IDatabaseTable(const MissingValType& missing_symbols,
786 const std::vector< std::string >& var_names);
787
790
793
795 [[nodiscard]] virtual IDatabaseTable< T_DATA >* clone() const = 0;
796
799
801
802
803 // ##########################################################################
805 // ##########################################################################
807
810
813
815 const iterator& end() const noexcept;
816
818 const iterator_safe& endSafe() const noexcept;
819
821
822
823 // ##########################################################################
825 // ##########################################################################
827
829 const Matrix< T_DATA >& content() const noexcept;
830
833
836
838
839 const DBVector< std::string >& variableNames() const noexcept;
840
842
867 virtual void setVariableNames(const std::vector< std::string >& names,
868 const bool from_external_object = true) = 0;
869
871
873 const std::string& variableName(const std::size_t k) const;
874
876
881 std::size_t columnFromVariableName(std::string_view name) const;
882
884
887 DBVector< std::size_t > columnsFromVariableName(std::string_view name) const;
888
890 std::size_t nbVariables() const noexcept;
891
893 std::size_t nbRows() const noexcept;
894
896 std::size_t size() const noexcept;
897
899 bool empty() const noexcept;
900
902
931 virtual void ignoreColumn(const std::size_t k, const bool from_external_object = true) = 0;
932
934 virtual const DBVector< std::size_t > ignoredColumns() const = 0;
935
938 virtual const DBVector< std::size_t > inputColumns() const = 0;
939
940 using IDatabaseTableInsert4DBCell< !std::is_same< T_DATA, DBCell >::value >::insertRow;
941
943
948 void insertRow(const std::vector< std::string >& new_row) override = 0;
949
951
957 virtual void insertRow(Row< T_DATA >&& new_row, const IsMissing contains_missing_data);
958
960
966 virtual void insertRow(const Row< T_DATA >& new_row, const IsMissing contains_missing_data);
967
968 using IDatabaseTableInsert4DBCell< !std::is_same< T_DATA, DBCell >::value >::insertRows;
969
971
983 virtual void insertRows(Matrix< T_DATA >&& new_rows,
984 const DBVector< IsMissing >& rows_have_missing_vals);
985
987
999 virtual void insertRows(const Matrix< T_DATA >& new_rows,
1000 const DBVector< IsMissing >& rows_have_missing_vals);
1001
1003
1006 void eraseRow(std::size_t index);
1007
1009
1012
1014
1017
1019
1021 void eraseFirstRows(const std::size_t k);
1022
1024
1026 void eraseLastRows(const std::size_t k);
1027
1029
1030 void eraseRows(std::size_t deb, std::size_t end);
1031
1034
1036 virtual void clear();
1037
1039 const DBVector< std::string >& missingSymbols() const;
1040
1042 bool hasMissingValues() const;
1043
1045 bool hasMissingValues(const std::size_t k) const;
1046
1048
1051 void setMaxNbThreads(const std::size_t nb) const;
1052
1054 std::size_t nbThreads() const;
1055
1065 void setMinNbRowsPerThread(const std::size_t nb) const;
1066
1068 std::size_t minNbRowsPerThread() const;
1069
1071 void setAllRowsWeight(const double new_weight);
1072
1074
1076 void setWeight(const std::size_t i, const double weight);
1077
1079
1081 double weight(const std::size_t i) const;
1082
1084 double weight() const;
1085
1087
1088
1089 protected:
1092
1093 // the vector of DBRows containing all the raw data
1094 Matrix< T_DATA > rows_;
1095
1096 // the set of string corresponding to missing values
1098
1099 // a vector indicating which rows have missing values (char != 0)
1101
1102 // the maximal number of threads that the database can use
1103 mutable std::size_t max_nb_threads_{std::size_t(gum::getNumberOfThreads())};
1104
1105 // the min number of rows that a thread should process in a
1106 // multithreading context
1107 mutable std::size_t min_nb_rows_per_thread_{100};
1108
1109
1112 bool isRowSizeOK_(const std::size_t size) const;
1113
1115 std::size_t nbProcessingThreads_() const;
1116
1118 std::vector< std::pair< std::size_t, std::size_t > >
1119 rangesProcessingThreads_(const std::size_t nb_threads) const;
1120
1123
1126
1127
1128#ifndef DOXYGEN_SHOULD_SKIP_THIS
1129
1130 private:
1131 // the list of handlers currently attached to the database
1132 /* this is useful when the database is resized */
1133 mutable DBVector< HandlerSafe* > _list_of_safe_handlers_;
1134
1135 // a mutex to safely add/remove handlers in _list_of_safe_handlers_
1136 mutable std::mutex _safe_handlers_mutex_;
1137
1138 // the end iterator for the database
1139 Handler* _end_{nullptr};
1140
1141 // the safe end iterator for the database
1142 iterator_safe* _end_safe_{nullptr};
1143
1145 void _attachHandler_(HandlerSafe* handler) const;
1146
1148 void _detachHandler_(HandlerSafe* handler) const;
1149
1151 void _updateHandlers_(std::size_t new_size) const;
1152
1153 // create the end iterators
1154 void _createEndIterators_();
1155
1156#endif /* DOXYGEN_SHOULD_SKIP_THIS */
1157
1158
1160 friend class Handler;
1161 friend class HandlerSafe;
1162 };
1163
1164 } /* namespace learning */
1165
1166} /* namespace gum */
1167
1170
1171#endif /* GUM_IDATABASE_TABLE_H */
The class representing the original values of the cells of databases.
The base class for all database handlers.
The base class for all the tabular databases' cell translators.
The implementation of the common class for tabular databases.
The class representing the original values of the cells of databases.
Definition DBCell.h:94
The base class for all database handlers.
Definition DBHandler.h:140
DBRow< T_DATA > value_type
Types for STL compliance.
Definition DBHandler.h:145
the safe handler of the tabular databases
HandlerSafe & operator=(Handler &&) override
move operator
HandlerSafe & operator=(const Handler &) override
copy operator
std::ptrdiff_t difference_type
Types for STL compliance.
virtual HandlerSafe & operator=(HandlerSafe &&)
move operator
value_type * pointer
Types for STL compliance.
HandlerSafe(const IDatabaseTable< T_DATA > &db)
default constructor
virtual HandlerSafe & operator=(const HandlerSafe &)
copy operator
const value_type * const_pointer
Types for STL compliance.
std::random_access_iterator_tag iterator_category
Types for STL compliance.
value_type & reference
Types for STL compliance.
HandlerSafe(HandlerSafe &&h)
move constructor
const value_type & const_reference
Types for STL compliance.
HandlerSafe(const HandlerSafe &h)
copy constructor
typename Handler::value_type value_type
Types for STL compliance.
the (unsafe) handler for the tabular databases
virtual Handler & operator++() final
makes the operator point to the next row in the database
const value_type * const_pointer
Types for STL compliance.
virtual Handler begin() const
returns a new handler that points to the beginning of the database's area of the current handler
std::size_t nbVariables() const final
returns the number of variables (columns) of the database
Handler(const IDatabaseTable< T_DATA > &db)
default constructor
const value_type & const_reference
Types for STL compliance.
bool hasRows() const final
indicates whether the handler has reached its end or not
void setRange(std::size_t first, std::size_t last) final
sets the area in the database the handler will handle
value_type & reference
Types for STL compliance.
const_reference rowSafe() const final
returns the current row pointed to by the handler (safe version)
void nextRow() final
makes the handler point to the next row, equivalent to operator++
virtual Handler end() const
returns a new handler that points to the end of the database's area of the current handler
virtual Handler & operator=(Handler &&)
move operator
std::size_t DBSize() const final
returns the number of rows of the whole database
const DBVector< std::string > & variableNames() const final
returns the names of the variables
void reset() final
puts the handler to the beginning of the database's area it handles
virtual const IDatabaseTable< T_DATA > & database() const
returns a pointer on the database
virtual Handler & operator=(const Handler &)
copy operator
typename DBHandler< T_DATA >::value_type value_type
Types for STL compliance.
std::size_t size() const final
returns the number of rows managed by the handler
std::ptrdiff_t difference_type
Types for STL compliance.
std::pair< std::size_t, std::size_t > range() const final
returns the current range of the handler [begin,end)
const_reference row() const final
returns the current row pointed to by the handler (unsafe version)
std::random_access_iterator_tag iterator_category
Types for STL compliance.
Handler(Handler &&h)
move constructor
Handler(const Handler &h)
copy constructor
std::size_t numRow() const final
the number of the current row (0 = the 1st row managed by the handler)
std::vector< DBRow< TX_DATA > > Matrix
value_type * pointer
Types for STL compliance.
const iterator & end() const noexcept
returns a new unsafe handler pointing to the end of the database
void setAllRowsWeight(const double new_weight)
assign a given weight to all the rows of the database
IDatabaseTable(IDatabaseTable< T_DATA > &&from)
move constructor
IDatabaseTable< T_DATA > & operator=(const IDatabaseTable< T_DATA > &from)
copy operator
value_type & reference
Types for STL compliance.
std::size_t nbProcessingThreads_() const
returns the number of threads used to process the current database content
const Handler const_iterator
Types for STL compliance.
DBVector< IsMissing > has_row_missing_val_
IDatabaseTable< T_DATA > & operator=(IDatabaseTable< T_DATA > &&from)
move operator
const iterator_safe & endSafe() const noexcept
returns a new safe handler pointing to the end of the database
std::size_t nbRows() const noexcept
returns the number of records (rows) in the database
IDatabaseTable(const MissingValType &missing_symbols, const std::vector< std::string > &var_names)
default constructor
DBVector< std::string > variable_names_
the names of the variables for each column
bool empty() const noexcept
indicates whether the database contains some records or not
void eraseRow(std::size_t index)
erase a given row specified by its index in the table
std::ptrdiff_t difference_type
Types for STL compliance.
virtual const DBVector< std::size_t > ignoredColumns() const =0
returns the set of columns of the original dataset that are ignored
void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context
void eraseFirstRows(const std::size_t k)
erase the k first rows
DBVector< std::string > missing_symbols_
virtual void clear()
erase the content of the database, including the names of the variables
void eraseLastRow()
erase the last row
const DBVector< std::string > & missingSymbols() const
returns the set of missing symbols
IDatabaseTable(const IDatabaseTable< T_DATA > &from)
copy constructor
std::vector< std::pair< std::size_t, std::size_t > > rangesProcessingThreads_(const std::size_t nb_threads) const
returns the ranges that threads should process
std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
std::vector< std::string > MissingValType
const Matrix< T_DATA > & content() const noexcept
returns the content (the records) of the database
const DBVector< std::string > & variableNames() const noexcept
returns the variable names for all the columns of the database
Row< T_DATA > value_type
Types for STL compliance.
const HandlerSafe const_iterator_safe
Types for STL compliance.
void eraseLastRows(const std::size_t k)
erase the k last rows
iterator begin() const
returns a new unsafe handler pointing to the beginning of the database
void eraseRows(std::size_t deb, std::size_t end)
erase the rows from the debth to the endth (not included)
virtual void setVariableNames(const std::vector< std::string > &names, const bool from_external_object=true)=0
sets the names of the variables
iterator handler() const
returns a new unsafe handler pointing to the 1st record of the database
virtual IDatabaseTable< T_DATA > * clone() const =0
virtual copy constructor
const value_type * const_pointer
Types for STL compliance.
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
void setMaxNbThreads(const std::size_t nb) const
changes the max number of threads that a database can use
double weight(const std::size_t i) const
returns the weight of the ith record
std::size_t size_type
Types for STL compliance.
std::size_t size() const noexcept
returns the number of records (rows) in the database
bool hasMissingValues() const
indicates whether the database contains some missing values
std::vector< DBRow< TX_DATA > > Matrix
the type for the matrices stored into the database
value_type * pointer
Types for STL compliance.
void eraseAllRows()
erase all the rows
std::size_t nbThreads() const
returns the number of threads used to parse the database
iterator_safe handlerSafe() const
returns a new safe handler pointing to the 1st record of the database
const std::string & variableName(const std::size_t k) const
returns the name of the kth column of the IDatabaseTable
std::size_t nbVariables() const noexcept
returns the number of variables (columns) of the database
void eraseFirstRow()
erase the first row
void setWeight(const std::size_t i, const double weight)
assigns a given weight to the ith row of the database
std::size_t columnFromVariableName(std::string_view name) const
returns the index of the column whose name is passed in argument
std::vector< TX_DATA > DBVector
the type for the vectors used in the IDatabaseTable
Handler iterator
Types for STL compliance.
void insertRow(const std::vector< std::string > &new_row) override=0
insert a new row at the end of the database
const value_type & const_reference
Types for STL compliance.
virtual ~IDatabaseTable()
destructor
DBRow< TX_DATA > Row
a row of the database
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
bool isRowSizeOK_(const std::size_t size) const
checks whether a size corresponds to the number of columns of the database
iterator_safe beginSafe() const
returns a new safe handler pointing to the beginning 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
DBVector< std::size_t > columnsFromVariableName(std::string_view name) const
returns the indices of all the columns whose name is passed in argument
HandlerSafe iterator_safe
Types for STL compliance.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
unsigned int getNumberOfThreads()
returns the max number of threads used by default when entering the next parallel region
STL namespace.
virtual void insertRows(const DBVector< DBVector< std::string > > &new_rows)
insert new rows at the end of the database
virtual void insertRow(const std::vector< std::string > &new_row)=0
insert a new row at the end of the database
virtual void insertRows(const Matrix< DBCell > &new_rows)=0
insert a set of new DBRows at the end of the database
virtual void insertRow(const std::vector< std::string > &new_row)=0
insert a new row at the end of the database
virtual void insertRow(Row< DBCell > &&new_row)=0
insert a new DBRow at the end of the database
virtual void insertRows(Matrix< DBCell > &&new_rows)=0
insert a set of new DBRows at the end of the database
virtual void insertRows(const DBVector< DBVector< std::string > > &new_rows)
insert new rows at the end of the database
virtual void insertRow(const Row< DBCell > &new_row)=0
insert a new row at the end of the database
The class to use to execute a function by several threads.
Utility functions used for exploiting OpenMP/STL parallelism.