aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DBRow_tpl.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 DOXYGEN_SHOULD_SKIP_THIS
28 
29 # include <algorithm>
30 # include <agrum/tools/database/DBRow.h>
31 
32 namespace gum {
33 
34  namespace learning {
35 
36  /// returns the allocator used by the DBRow
37  template < typename T_DATA, template < typename > class ALLOC >
38  INLINE ALLOC< T_DATA > DBRow< T_DATA, ALLOC >::getAllocator() const {
39  return row_.get_allocator();
40  }
41 
42 
43  /// default constructor with specific allocator
44  template < typename T_DATA, template < typename > class ALLOC >
45  INLINE DBRow< T_DATA, ALLOC >::DBRow(const ALLOC< T_DATA >& alloc) :
46  row_(alloc) {
48  }
49 
50 
51  /// default constructor
52  template < typename T_DATA, template < typename > class ALLOC >
53  INLINE DBRow< T_DATA, ALLOC >::DBRow() : DBRow(ALLOC< T_DATA >()) {}
54 
55 
56  /// constructor with a given size for the row
57  template < typename T_DATA, template < typename > class ALLOC >
59  const T_DATA default_cell,
60  const double weight,
61  const ALLOC< T_DATA >& alloc) :
63  weight_(weight) {
65  }
66 
67 
68  /// constructor with a given size for the row
69  template < typename T_DATA, template < typename > class ALLOC >
71  const double weight,
72  const ALLOC< T_DATA >& alloc) :
73  DBRow(size, T_DATA(), weight, alloc) {}
74 
75  /// initializer list constructor
76  template < typename T_DATA, template < typename > class ALLOC >
78  const double weight,
79  const ALLOC< T_DATA >& alloc) :
80  row_(list, alloc),
81  weight_(weight) {
83  }
84 
85 
86  /// sets a new row
87  template < typename T_DATA, template < typename > class ALLOC >
88  template < template < typename > class OTHER_ALLOC >
89  INLINE void DBRow< T_DATA, ALLOC >::setRow(
90  const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& new_row) {
91  const std::size_t size = new_row.size();
92  if (size) {
93  row_.resize(size);
95  } else {
96  row_.clear();
97  }
98  }
99 
100 
101  /// sets a new row
102  template < typename T_DATA, template < typename > class ALLOC >
103  INLINE void DBRow< T_DATA, ALLOC >::setRow(
104  std::vector< T_DATA, ALLOC< T_DATA > >&& new_row) {
105  row_ = std::move(new_row);
106  }
107 
108 
109  /// initializer from a vector of cells
110  template < typename T_DATA, template < typename > class ALLOC >
111  template < template < typename > class OTHER_ALLOC >
113  const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& cells,
114  const double weight,
115  const ALLOC< T_DATA >& alloc) :
116  row_(alloc),
117  weight_(weight) {
118  setRow(cells);
120  }
121 
122 
123  /// initializer from a vector of cells
124  template < typename T_DATA, template < typename > class ALLOC >
126  std::vector< T_DATA, ALLOC< T_DATA > >&& cells,
127  const double weight,
128  const ALLOC< T_DATA >& alloc) :
129  row_(std::move(cells), alloc),
130  weight_(weight) {
132  }
133 
134 
135  /// copy constructor with a given allocator
136  template < typename T_DATA, template < typename > class ALLOC >
137  INLINE DBRow< T_DATA, ALLOC >::DBRow(const DBRow< T_DATA, ALLOC >& from,
138  const ALLOC< T_DATA >& alloc) :
139  row_(from.row_, alloc),
140  weight_(from.weight_) {
142  }
143 
144 
145  /// copy constructor
146  template < typename T_DATA, template < typename > class ALLOC >
147  INLINE DBRow< T_DATA, ALLOC >::DBRow(const DBRow< T_DATA, ALLOC >& from) :
149 
150 
151  /// move constructor with a given allocator
152  template < typename T_DATA, template < typename > class ALLOC >
154  const ALLOC< T_DATA >& alloc) :
155  row_(std::move(from.row_), alloc),
156  weight_(from.weight_) {
158  }
159 
160 
161  /// move constructor
162  template < typename T_DATA, template < typename > class ALLOC >
165 
166 
167  /// virtual copy constructor with a given allocator
168  template < typename T_DATA, template < typename > class ALLOC >
169  DBRow< T_DATA, ALLOC >*
170  DBRow< T_DATA, ALLOC >::clone(const ALLOC< T_DATA >& alloc) const {
173  try {
174  allocator.construct(row, *this, alloc);
175  } catch (...) {
177  throw;
178  }
179  return row;
180  }
181 
182 
183  /// virtual copy constructor
184  template < typename T_DATA, template < typename > class ALLOC >
185  DBRow< T_DATA, ALLOC >* DBRow< T_DATA, ALLOC >::clone() const {
186  return clone(this->getAllocator());
187  }
188 
189 
190  /// destructor
191  template < typename T_DATA, template < typename > class ALLOC >
192  INLINE DBRow< T_DATA, ALLOC >::~DBRow() {
194  }
195 
196 
197  /// copy operator
198  template < typename T_DATA, template < typename > class ALLOC >
199  INLINE DBRow< T_DATA, ALLOC >&
200  DBRow< T_DATA, ALLOC >::operator=(const DBRow< T_DATA, ALLOC >& from) {
201  if (this != &from) {
202  row_ = from.row_;
203  weight_ = from.weight_;
204  }
205  return *this;
206  }
207 
208 
209  /// move operator
210  template < typename T_DATA, template < typename > class ALLOC >
211  INLINE DBRow< T_DATA, ALLOC >&
213  if (this != &from) {
214  row_ = std::move(from.row_);
215  weight_ = from.weight_;
216  }
217  return *this;
218  }
219 
220  /// returns the ith T_DATA of the row
221  template < typename T_DATA, template < typename > class ALLOC >
222  INLINE T_DATA& DBRow< T_DATA, ALLOC >::operator[](const std::size_t i) {
223  return row_[i];
224  }
225 
226  /// returns the ith T_DATA of the row
227  template < typename T_DATA, template < typename > class ALLOC >
228  INLINE const T_DATA&
229  DBRow< T_DATA, ALLOC >::operator[](const std::size_t i) const {
230  return row_[i];
231  }
232 
233  /// returns the current row
234  template < typename T_DATA, template < typename > class ALLOC >
235  INLINE const std::vector< T_DATA, ALLOC< T_DATA > >&
236  DBRow< T_DATA, ALLOC >::row() const noexcept {
237  return row_;
238  }
239 
240  /// returns the current row
241  template < typename T_DATA, template < typename > class ALLOC >
243  DBRow< T_DATA, ALLOC >::row() noexcept {
244  return row_;
245  }
246 
247  /// returns the weight
248  template < typename T_DATA, template < typename > class ALLOC >
249  INLINE const double& DBRow< T_DATA, ALLOC >::weight() const noexcept {
250  return weight_;
251  }
252 
253  /// returns the weight
254  template < typename T_DATA, template < typename > class ALLOC >
255  INLINE double& DBRow< T_DATA, ALLOC >::weight() noexcept {
256  return weight_;
257  }
258 
259  /// sets a new weight
260  template < typename T_DATA, template < typename > class ALLOC >
261  INLINE void DBRow< T_DATA, ALLOC >::setWeight(const double new_weight) {
263  }
264 
265  /// returns the size of the row
266  template < typename T_DATA, template < typename > class ALLOC >
267  INLINE std::size_t DBRow< T_DATA, ALLOC >::size() const noexcept {
268  return row_.size();
269  }
270 
271  /// resize a given row
272  template < typename T_DATA, template < typename > class ALLOC >
273  INLINE void DBRow< T_DATA, ALLOC >::resize(const std::size_t new_size) {
275  }
276 
277  /// reserve a size for the elements of a given row
278  template < typename T_DATA, template < typename > class ALLOC >
279  INLINE void DBRow< T_DATA, ALLOC >::reserve(const std::size_t new_size) {
281  }
282 
283  /// adds a new element at the end of the row
284  template < typename T_DATA, template < typename > class ALLOC >
285  INLINE void DBRow< T_DATA, ALLOC >::pushBack(const T_DATA& elt) {
286  row_.push_back(elt);
287  }
288 
289  /// adds a new element at the end of the row
290  template < typename T_DATA, template < typename > class ALLOC >
291  INLINE void DBRow< T_DATA, ALLOC >::pushBack(T_DATA&& elt) {
293  }
294 
295 
296  } /* namespace learning */
297 
298 } /* namespace gum */
299 
300 
301 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)