aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DBRow.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
54  template < typename > class ALLOC = std::allocator >
55  class DBRow {
56  public:
57  using allocator_type = ALLOC< T_DATA >;
58 
59  // ##########################################################################
60  /// @name Constructors / Destructors
61  // ##########################################################################
62 
63  /// @{
64 
65  /// default constructor
66  DBRow();
67 
68  /// default constructor with specific allocator
69  DBRow(const ALLOC< T_DATA >& alloc);
70 
71  /// constructs a row of a given size with missing values
72  DBRow(const std::size_t size,
73  const double weight = 1.0,
74  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
75 
76  /// constructor with a given size for the row
77  DBRow(const std::size_t size,
78  const T_DATA default_value,
79  const double weight,
80  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
81 
82  /// initializer list constructor
83  DBRow(std::initializer_list< T_DATA > list,
84  const double weight = 1.0,
85  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
86 
87  /// initializer from a vector of cells
88  template < template < typename > class OTHER_ALLOC >
89  DBRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& cells,
90  const double weight = 1.0,
91  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
92 
93  /// initializer with a move from a vector of cells
94  DBRow(std::vector< T_DATA, ALLOC< T_DATA > >&& cells,
95  const double weight = 1.0,
96  const ALLOC< T_DATA >& alloc = ALLOC< T_DATA >());
97 
98  /// copy constructor
99  DBRow(const DBRow< T_DATA, ALLOC >& from);
100 
101  /// copy constructor with a given allocator
102  DBRow(const DBRow< T_DATA, ALLOC >& from, const ALLOC< T_DATA >& alloc);
103 
104  /// move constructor
105  DBRow(DBRow< T_DATA, ALLOC >&& from);
106 
107  /// move constructor with a given allocator
108  DBRow(DBRow< T_DATA, ALLOC >&& from, const ALLOC< T_DATA >& alloc);
109 
110  /// virtual copy constructor
111  DBRow< T_DATA, ALLOC >* clone() const;
112 
113  /// virtual copy constructor with a given allocator
114  DBRow< T_DATA, ALLOC >* clone(const ALLOC< T_DATA >& alloc) const;
115 
116  /// destructor
117  ~DBRow();
118 
119  /// @}
120 
121 
122  // ##########################################################################
123  /// @name Operators
124  // ##########################################################################
125 
126  /// @{
127 
128  /// copy operator
129  DBRow< T_DATA, ALLOC >& operator=(const DBRow< T_DATA, ALLOC >& from);
130 
131  /// move operator
133 
134  /// returns the ith content of the row
135  /** @warning This method does not check that there are at least i+1
136  * elements in the row. If this is not the case, you will probably get
137  * a segmentation fault. */
138  T_DATA& operator[](const std::size_t i);
139 
140  /// returns the ith content of the row
141  /** @warning This method does not check that there are at least i+1
142  * elements in the row. If this is not the case, you will probably get
143  * a segmentation fault. */
144  const T_DATA& operator[](const std::size_t i) const;
145 
146  /// @}
147 
148 
149  // ##########################################################################
150  /// @name Accessors / Modifiers
151  // ##########################################################################
152 
153  /// @{
154 
155  /// returns the current row (without the weight)
156  const std::vector< T_DATA, ALLOC< T_DATA > >& row() const noexcept;
157 
158  /// returns the current row (without the weight)
159  std::vector< T_DATA, ALLOC< T_DATA > >& row() noexcept;
160 
161  /// returns the weight assigned to the DBRow
162  const double& weight() const noexcept;
163 
164  /// returns the weight assigned to the DBRow
165  double& weight() noexcept;
166 
167  /// sets a new row (without changing the weight)
168  template < template < typename > class OTHER_ALLOC >
169  void setRow(const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& new_row);
170 
171  /// sets a new row (without changing the weight)
172  void setRow(std::vector< T_DATA, ALLOC< T_DATA > >&& new_row);
173 
174  /// sets a new weight
175  void setWeight(const double new_weight);
176 
177  /// returns the number of elements in the row
178  std::size_t size() const noexcept;
179 
180  /// resize a given row, i.e., its number of elements
181  void resize(const std::size_t new_size);
182 
183  /// reserve a size for the elements of a given row
184  void reserve(const std::size_t new_size);
185 
186  /// adds a new element at the end of the row
187  void pushBack(const T_DATA& elt);
188 
189  /// adds a new element at the end of the row
190  void pushBack(T_DATA&& elt);
191 
192  /// returns the allocator used by the DBRow
193  ALLOC< T_DATA > getAllocator() const;
194 
195  /// @}
196 
197 
198  protected:
199  /// the row itself
200  std::vector< T_DATA, ALLOC< T_DATA > > row_;
201 
202  /// the weight of the row
203  double weight_{1.0f};
204 
205  // used for constructors and operators
206  template < typename TX_DATA, template < typename > class OTHER_ALLOC >
207  friend class DBRow;
208  };
209 
210  } /* namespace learning */
211 
212 } /* namespace gum */
213 
214 /// always include the templated implementations
215 #include <agrum/tools/database/DBRow_tpl.h>
216 
217 #endif /* GUM_LEARNING_DB_ROW_H */
std::vector< T_DATA, ALLOC< T_DATA > > row_
the row itself
Definition: DBRow.h:200
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:669
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:207
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:203
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