aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
databaseTable_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
46
47namespace gum {
48
49 namespace learning {
50
51 // copy constructor
53 IDatabaseTable< DBTranslatedValue >(from), _translators_(from._translators_),
54 _ignored_cols_(from._ignored_cols_) {
55 GUM_CONS_CPY(DatabaseTable);
56 }
57
58 // move constructor
59 INLINE DatabaseTable::DatabaseTable(DatabaseTable&& from) noexcept :
60 IDatabaseTable< DBTranslatedValue >(std::move(from)),
61 _translators_(std::move(from._translators_)),
62 _ignored_cols_(std::move(from._ignored_cols_)) {
63 GUM_CONS_MOV(DatabaseTable);
64 }
65
66 // destructor
67 INLINE DatabaseTable::~DatabaseTable() { GUM_DESTRUCTOR(DatabaseTable); }
68
73 INLINE typename DatabaseTable::template DBVector< std::size_t >
74 DatabaseTable::_getKthIndices_(const std::size_t k, const bool k_is_input_col) const {
75 const std::size_t nb_trans = _translators_.size();
76 if (!k_is_input_col) {
77 if (k < nb_trans) return DBVector< std::size_t >{k};
78 else return DBVector< std::size_t >();
79 } else {
80 DBVector< std::size_t > trans;
81 for (std::size_t i = std::size_t(0), kk = nb_trans - 1; i < nb_trans; ++i, --kk) {
82 if (_translators_.inputColumn(kk) == k) trans.push_back(kk);
83 }
84 return trans;
85 }
86 }
87
89 INLINE const DBTranslatorSet& DatabaseTable::translatorSet() const { return _translators_; }
90
94 INLINE std::size_t DatabaseTable::_getKthIndex_(const std::size_t k,
95 const bool k_is_input_col) const {
96 if (k_is_input_col) {
97 const std::size_t nb_trans = _translators_.size();
98 for (std::size_t i = std::size_t(0); i < nb_trans; ++i) {
99 if (_translators_.inputColumn(i) == k) { return i; }
100 }
101 return nb_trans + 1;
102 } else {
103 return k;
104 }
105 }
106
108 INLINE std::vector< std::size_t > DatabaseTable::domainSizes() const {
109 const std::size_t nb_trans = _translators_.size();
110 std::vector< std::size_t > dom(nb_trans);
111 for (std::size_t i = std::size_t(0); i < nb_trans; ++i) {
112 dom[i] = _translators_.domainSize(i);
113 }
114 return dom;
115 }
116
118 INLINE void DatabaseTable::reorder() {
119 const std::size_t nb_trans = _translators_.size();
120 for (std::size_t i = std::size_t(0); i < nb_trans; ++i)
121 reorder(i, false);
122 }
123
125 INLINE void DatabaseTable::insertRow(
126 typename DatabaseTable::template Row< DBTranslatedValue >&& new_row,
127 const typename DatabaseTable::IsMissing contains_missing_data) {
128 // check that the new rows values are compatible with the values of
129 // the variables stored within the translators
130 if (!_isRowCompatible_(new_row)) {
131 if (new_row.size() != _translators_.size()) {
133 "The new row has " << new_row.size()
134 << " elements whereas the database table has "
135 << _translators_.size() << " columns")
136 } else {
137 GUM_ERROR(InvalidArgument, "the new row is not compatible with the current translators")
138 }
139 }
140
141 IDatabaseTable< DBTranslatedValue >::insertRow(std::move(new_row), contains_missing_data);
142 }
143
145 INLINE void DatabaseTable::insertRow(
146 const typename DatabaseTable::template Row< DBTranslatedValue >& new_row,
147 const typename DatabaseTable::IsMissing contains_missing_data) {
148 // check that the new rows values are compatible with the values of
149 // the variables stored within the translators
150 if (!_isRowCompatible_(new_row)) {
151 if (new_row.size() != _translators_.size()) {
153 "The new row has " << new_row.size()
154 << " elements whereas the database table has "
155 << _translators_.size() << " columns")
156 } else {
157 GUM_ERROR(InvalidArgument, "the new row is not compatible with the current translators")
158 }
159 }
160
161 IDatabaseTable< DBTranslatedValue >::insertRow(new_row, contains_missing_data);
162 }
163
164 } /* namespace learning */
165
166} /* namespace gum */
167
168#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Exception: at least one argument passed to a function is not what was expected.
Exception : problem with size.
The class representing a tabular database as used by learning tasks.
DatabaseTable(const MissingValType &missing_symbols, const DBTranslatorSet &translators=DBTranslatorSet())
default constructor
The common class for the tabular database tables.
The class representing a tabular database stored in RAM.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
The union class for storing the translated values in learning databases.