aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DBRow.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The class representing a record stored in a tabular database
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_LEARNING_DB_ROW_H
28 #define GUM_LEARNING_DB_ROW_H
29 
30 #include <cstddef>
31 #include <cstring>
32 #include <initializer_list>
33 #include <memory>
34 #include <vector>
35 
36 #include <agrum/agrum.h>
37 
38 namespace gum {
39 
40  namespace learning {
41 
42  /** @class DBRow
43  * @headerfile DBRow.h <agrum/tools/database/DBRow.h>
44  * @brief The class for storing a record in a database
45  *
46  * Learning algorithms use DatabaseTable instances as their input data.
47  * Those are represented essentially as vectors of DBRow instances. Each
48  * DBRow is encoded as a vector of elements plus a weight (which is a
49  * double). To be quite general, the types of the elements stored into the
50  * DBRow are specified as template parameters of the DBRow.
51  *
52  * @ingroup learning_database */
53  template < typename T_DATA, template < typename > class ALLOC = std::allocator >
54  class DBRow {
55  public:
56  using allocator_type = ALLOC< T_DATA >;
57 
58  // ##########################################################################
59  /// @name Constructors / Destructors
60  // ##########################################################################
61 
62  /// @{
63 
64  /// default constructor
65  DBRow();
66 
67  /// default constructor with specific allocator
68  DBRow(const ALLOC< T_DATA >& alloc);
69 
70  /// constructs a row of a given size with missing values
71  DBRow(const std::size_t size,
72  const double weight = 1.0,
73  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
74 
75  /// constructor with a given size for the row
76  DBRow(const std::size_t size,
77  const T_DATA default_value,
78  const double weight,
79  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
80 
81  /// initializer list constructor
82  DBRow(std::initializer_list< T_DATA > list,
83  const double weight = 1.0,
84  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
85 
86  /// initializer from a vector of cells
87  template < template < typename > class OTHER_ALLOC >
88  DBRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& cells,
89  const double weight = 1.0,
90  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
91 
92  /// initializer with a move from a vector of cells
93  DBRow(std::vector< T_DATA, ALLOC< T_DATA > >&& cells,
94  const double weight = 1.0,
95  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
96 
97  /// copy constructor
98  DBRow(const DBRow< T_DATA, ALLOC >& from);
99 
100  /// copy constructor with a given allocator
101  DBRow(const DBRow< T_DATA, ALLOC >& from, const ALLOC< T_DATA >& alloc);
102 
103  /// move constructor
104  DBRow(DBRow< T_DATA, ALLOC >&& from);
105 
106  /// move constructor with a given allocator
107  DBRow(DBRow< T_DATA, ALLOC >&& from, const ALLOC< T_DATA >& alloc);
108 
109  /// virtual copy constructor
110  DBRow< T_DATA, ALLOC >* clone() const;
111 
112  /// virtual copy constructor with a given allocator
113  DBRow< T_DATA, ALLOC >* clone(const ALLOC< T_DATA >& alloc) const;
114 
115  /// destructor
116  ~DBRow();
117 
118  /// @}
119 
120 
121  // ##########################################################################
122  /// @name Operators
123  // ##########################################################################
124 
125  /// @{
126 
127  /// copy operator
128  DBRow< T_DATA, ALLOC >& operator=(const DBRow< T_DATA, ALLOC >& from);
129 
130  /// move operator
132 
133  /// returns the ith content of the row
134  /** @warning This method does not check that there are at least i+1
135  * elements in the row. If this is not the case, you will probably get
136  * a segmentation fault. */
137  T_DATA& operator[](const std::size_t i);
138 
139  /// returns the ith content of the row
140  /** @warning This method does not check that there are at least i+1
141  * elements in the row. If this is not the case, you will probably get
142  * a segmentation fault. */
143  const T_DATA& operator[](const std::size_t i) const;
144 
145  /// @}
146 
147 
148  // ##########################################################################
149  /// @name Accessors / Modifiers
150  // ##########################################################################
151 
152  /// @{
153 
154  /// returns the current row (without the weight)
155  const std::vector< T_DATA, ALLOC< T_DATA > >& row() const noexcept;
156 
157  /// returns the current row (without the weight)
158  std::vector< T_DATA, ALLOC< T_DATA > >& row() noexcept;
159 
160  /// returns the weight assigned to the DBRow
161  const double& weight() const noexcept;
162 
163  /// returns the weight assigned to the DBRow
164  double& weight() noexcept;
165 
166  /// sets a new row (without changing the weight)
167  template < template < typename > class OTHER_ALLOC >
168  void setRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& new_row);
169 
170  /// sets a new row (without changing the weight)
171  void setRow(std::vector< T_DATA, ALLOC< T_DATA > >&& new_row);
172 
173  /// sets a new weight
174  void setWeight(const double new_weight);
175 
176  /// returns the number of elements in the row
177  std::size_t size() const noexcept;
178 
179  /// resize a given row, i.e., its number of elements
180  void resize(const std::size_t new_size);
181 
182  /// reserve a size for the elements of a given row
183  void reserve(const std::size_t new_size);
184 
185  /// adds a new element at the end of the row
186  void pushBack(const T_DATA& elt);
187 
188  /// adds a new element at the end of the row
189  void pushBack(T_DATA&& elt);
190 
191  /// returns the allocator used by the DBRow
192  ALLOC< T_DATA > getAllocator() const;
193 
194  /// @}
195 
196 
197  protected:
198  /// the row itself
199  std::vector< T_DATA, ALLOC< T_DATA > > row_;
200 
201  /// the weight of the row
202  double weight_{1.0f};
203 
204  // used for constructors and operators
205  template < typename TX_DATA, template < typename > class OTHER_ALLOC >
206  friend class DBRow;
207  };
208 
209  } /* namespace learning */
210 
211 } /* namespace gum */
212 
213 /// always include the templated implementations
214 #include <agrum/tools/database/DBRow_tpl.h>
215 
216 #endif /* GUM_LEARNING_DB_ROW_H */
std::vector< T_DATA, ALLOC< T_DATA > > row_
the row itself
Definition: DBRow.h:199
double & weight() noexcept
returns the weight assigned to the DBRow
DBRow< T_DATA, ALLOC > * clone() const
virtual copy constructor
~DBRow()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
DBRow(const DBRow< T_DATA, ALLOC > &from)
copy constructor
void setRow(std::vector< T_DATA, ALLOC< T_DATA > > &&new_row)
sets a new row (without changing the weight)
DBRow(DBRow< T_DATA, ALLOC > &&from)
move constructor
DBRow(std::vector< T_DATA, ALLOC< T_DATA > > &&cells, const double weight=1.0, const ALLOC< T_DATA > &alloc=ALLOC< T_DATA >())
initializer with a move from a vector of cells
friend class DBRow
Definition: DBRow.h:206
DBRow()
default constructor
DBRow< T_DATA, ALLOC > & operator=(const DBRow< T_DATA, ALLOC > &from)
copy operator
void pushBack(T_DATA &&elt)
adds a new element at the end of the row
DBRow(const ALLOC< T_DATA > &alloc)
default constructor with specific allocator
DBRow< T_DATA, ALLOC > * clone(const ALLOC< T_DATA > &alloc) const
virtual copy constructor with a given allocator
void setRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > > &new_row)
sets a new row (without changing the weight)
void setWeight(const double new_weight)
sets a new weight
double weight_
the weight of the row
Definition: DBRow.h:202
DBRow(const std::size_t size, const double weight=1.0, const ALLOC< T_DATA > &alloc=ALLOC< T_DATA >())
constructs a row of a given size with missing values
DBRow(const DBRow< T_DATA, ALLOC > &from, const ALLOC< T_DATA > &alloc)
copy constructor with a given allocator
const T_DATA & operator[](const std::size_t i) const
returns the ith content of the row
const std::vector< T_DATA, ALLOC< T_DATA > > & row() const noexcept
returns the current row (without the weight)
const double & weight() const noexcept
returns the weight assigned to the DBRow
DBRow(DBRow< T_DATA, ALLOC > &&from, const ALLOC< T_DATA > &alloc)
move constructor with a given allocator
void resize(const std::size_t new_size)
resize a given row, i.e., its number of elements
DBRow< T_DATA, ALLOC > & operator=(DBRow< T_DATA, ALLOC > &&from)
move operator
std::vector< T_DATA, ALLOC< T_DATA > > & row() noexcept
returns the current row (without the weight)
DBRow(const std::size_t size, const T_DATA default_value, const double weight, const ALLOC< T_DATA > &alloc=ALLOC< T_DATA >())
constructor with a given size for the row
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
std::size_t size() const noexcept
returns the number of elements in the row
T_DATA & operator[](const std::size_t i)
returns the ith content of the row
void reserve(const std::size_t new_size)
reserve a size for the elements of a given row
DBRow(std::initializer_list< T_DATA > list, const double weight=1.0, const ALLOC< T_DATA > &alloc=ALLOC< T_DATA >())
initializer list constructor
DBRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > > &cells, const double weight=1.0, const ALLOC< T_DATA > &alloc=ALLOC< T_DATA >())
initializer from a vector of cells
ALLOC< T_DATA > getAllocator() const
returns the allocator used by the DBRow
void pushBack(const T_DATA &elt)
adds a new element at the end of the row