aGrUM  0.14.2
DBRow_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
26 
27 # include <algorithm>
29 
30 namespace gum {
31 
32  namespace learning {
33 
35  template < typename T_DATA, template < typename > class ALLOC >
36  INLINE ALLOC< T_DATA > DBRow< T_DATA, ALLOC >::getAllocator() const {
37  return _row.get_allocator();
38  }
39 
40 
42  template < typename T_DATA, template < typename > class ALLOC >
43  INLINE DBRow< T_DATA, ALLOC >::DBRow(const ALLOC< T_DATA >& alloc) :
44  _row(alloc) {
45  GUM_CONSTRUCTOR(DBRow);
46  }
47 
48 
50  template < typename T_DATA, template < typename > class ALLOC >
51  INLINE DBRow< T_DATA, ALLOC >::DBRow() : DBRow(ALLOC< T_DATA >()) {}
52 
53 
55  template < typename T_DATA, template < typename > class ALLOC >
56  INLINE DBRow< T_DATA, ALLOC >::DBRow(const std::size_t size,
57  const T_DATA default_cell,
58  const double weight,
59  const ALLOC< T_DATA >& alloc) :
60  _row(size, default_cell, alloc),
61  _weight(weight) {
62  GUM_CONSTRUCTOR(DBRow);
63  }
64 
65 
67  template < typename T_DATA, template < typename > class ALLOC >
68  INLINE DBRow< T_DATA, ALLOC >::DBRow(const std::size_t size,
69  const double weight,
70  const ALLOC< T_DATA >& alloc) :
71  DBRow(size, T_DATA(), weight, alloc) {}
72 
74  template < typename T_DATA, template < typename > class ALLOC >
75  INLINE DBRow< T_DATA, ALLOC >::DBRow(std::initializer_list< T_DATA > list,
76  const double weight,
77  const ALLOC< T_DATA >& alloc) :
78  _row(list, alloc),
79  _weight(weight) {
80  GUM_CONSTRUCTOR(DBRow);
81  }
82 
83 
85  template < typename T_DATA, template < typename > class ALLOC >
86  template < template < typename > class OTHER_ALLOC >
88  const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& new_row) {
89  const std::size_t size = new_row.size();
90  if (size) {
91  _row.resize(size);
92  std::copy(new_row.begin(), new_row.end(), _row.begin());
93  } else {
94  _row.clear();
95  }
96  }
97 
98 
100  template < typename T_DATA, template < typename > class ALLOC >
101  INLINE void DBRow< T_DATA, ALLOC >::setRow(
102  std::vector< T_DATA, ALLOC< T_DATA > >&& new_row) {
103  _row = std::move(new_row);
104  }
105 
106 
108  template < typename T_DATA, template < typename > class ALLOC >
109  template < template < typename > class OTHER_ALLOC >
111  const std::vector< T_DATA, OTHER_ALLOC< T_DATA > >& cells,
112  const double weight,
113  const ALLOC< T_DATA >& alloc) :
114  _row(alloc),
115  _weight(weight) {
116  setRow(cells);
117  GUM_CONSTRUCTOR(DBRow);
118  }
119 
120 
122  template < typename T_DATA, template < typename > class ALLOC >
124  std::vector< T_DATA, ALLOC< T_DATA > >&& cells,
125  const double weight,
126  const ALLOC< T_DATA >& alloc) :
127  _row(std::move(cells), alloc),
128  _weight(weight) {
129  GUM_CONSTRUCTOR(DBRow);
130  }
131 
132 
134  template < typename T_DATA, template < typename > class ALLOC >
135  INLINE DBRow< T_DATA, ALLOC >::DBRow(const DBRow< T_DATA, ALLOC >& from,
136  const ALLOC< T_DATA >& alloc) :
137  _row(from._row, alloc),
138  _weight(from._weight) {
139  GUM_CONS_CPY(DBRow);
140  }
141 
142 
144  template < typename T_DATA, template < typename > class ALLOC >
145  INLINE DBRow< T_DATA, ALLOC >::DBRow(const DBRow< T_DATA, ALLOC >& from) :
146  DBRow< T_DATA, ALLOC >(from, from.getAllocator()) {}
147 
148 
150  template < typename T_DATA, template < typename > class ALLOC >
151  INLINE DBRow< T_DATA, ALLOC >::DBRow(DBRow< T_DATA, ALLOC >&& from,
152  const ALLOC< T_DATA >& alloc) :
153  _row(std::move(from._row), alloc),
154  _weight(from._weight) {
155  GUM_CONS_MOV(DBRow);
156  }
157 
158 
160  template < typename T_DATA, template < typename > class ALLOC >
161  INLINE DBRow< T_DATA, ALLOC >::DBRow(DBRow< T_DATA, ALLOC >&& from) :
162  DBRow< T_DATA, ALLOC >(from, from.getAllocator()) {}
163 
164 
166  template < typename T_DATA, template < typename > class ALLOC >
167  DBRow< T_DATA, ALLOC >*
168  DBRow< T_DATA, ALLOC >::clone(const ALLOC< T_DATA >& alloc) const {
169  ALLOC< DBRow< T_DATA, ALLOC > > allocator(alloc);
170  DBRow< T_DATA, ALLOC >* row = allocator.allocate(1);
171  try {
172  allocator.construct(row, *this, alloc);
173  } catch (...) {
174  allocator.deallocate(row, 1);
175  throw;
176  }
177  return row;
178  }
179 
180 
182  template < typename T_DATA, template < typename > class ALLOC >
183  DBRow< T_DATA, ALLOC >* DBRow< T_DATA, ALLOC >::clone() const {
184  return clone(this->getAllocator());
185  }
186 
187 
189  template < typename T_DATA, template < typename > class ALLOC >
191  GUM_DESTRUCTOR(DBRow);
192  }
193 
194 
196  template < typename T_DATA, template < typename > class ALLOC >
197  INLINE DBRow< T_DATA, ALLOC >& DBRow< T_DATA, ALLOC >::
198  operator=(const DBRow< T_DATA, ALLOC >& from) {
199  if (this != &from) {
200  _row = from._row;
201  _weight = from._weight;
202  }
203  return *this;
204  }
205 
206 
208  template < typename T_DATA, template < typename > class ALLOC >
209  INLINE DBRow< T_DATA, ALLOC >& DBRow< T_DATA, ALLOC >::
210  operator=(DBRow< T_DATA, ALLOC >&& from) {
211  if (this != &from) {
212  _row = std::move(from._row);
213  _weight = from._weight;
214  }
215  return *this;
216  }
217 
219  template < typename T_DATA, template < typename > class ALLOC >
220  INLINE T_DATA& DBRow< T_DATA, ALLOC >::operator[](const std::size_t i) {
221  return _row[i];
222  }
223 
225  template < typename T_DATA, template < typename > class ALLOC >
226  INLINE const T_DATA& DBRow< T_DATA, ALLOC >::
227  operator[](const std::size_t i) const {
228  return _row[i];
229  }
230 
232  template < typename T_DATA, template < typename > class ALLOC >
233  INLINE const std::vector< T_DATA, ALLOC< T_DATA > >&
234  DBRow< T_DATA, ALLOC >::row() const noexcept {
235  return _row;
236  }
237 
239  template < typename T_DATA, template < typename > class ALLOC >
240  INLINE std::vector< T_DATA, ALLOC< T_DATA > >&
241  DBRow< T_DATA, ALLOC >::row() noexcept {
242  return _row;
243  }
244 
246  template < typename T_DATA, template < typename > class ALLOC >
247  INLINE const double& DBRow< T_DATA, ALLOC >::weight() const noexcept {
248  return _weight;
249  }
250 
252  template < typename T_DATA, template < typename > class ALLOC >
253  INLINE double& DBRow< T_DATA, ALLOC >::weight() noexcept {
254  return _weight;
255  }
256 
258  template < typename T_DATA, template < typename > class ALLOC >
259  INLINE void DBRow< T_DATA, ALLOC >::setWeight(const double new_weight) {
260  _weight = new_weight;
261  }
262 
264  template < typename T_DATA, template < typename > class ALLOC >
265  INLINE std::size_t DBRow< T_DATA, ALLOC >::size() const noexcept {
266  return _row.size();
267  }
268 
270  template < typename T_DATA, template < typename > class ALLOC >
271  INLINE void DBRow< T_DATA, ALLOC >::resize(const std::size_t new_size) {
272  _row.resize(new_size);
273  }
274 
276  template < typename T_DATA, template < typename > class ALLOC >
277  INLINE void DBRow< T_DATA, ALLOC >::reserve(const std::size_t new_size) {
278  _row.reserve(new_size);
279  }
280 
282  template < typename T_DATA, template < typename > class ALLOC >
283  INLINE void DBRow< T_DATA, ALLOC >::pushBack(const T_DATA& elt) {
284  _row.push_back(elt);
285  }
286 
288  template < typename T_DATA, template < typename > class ALLOC >
289  INLINE void DBRow< T_DATA, ALLOC >::pushBack(T_DATA&& elt) {
290  _row.push_back(std::move(elt));
291  }
292 
293 
294  } /* namespace learning */
295 
296 } /* namespace gum */
297 
298 
299 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
DBRow< T_DATA, ALLOC > * clone() const
virtual copy constructor
~DBRow()
destructor
double _weight
the weight of the row
Definition: DBRow.h:201
STL namespace.
DBRow()
default constructor
DBRow< T_DATA, ALLOC > & operator=(const DBRow< T_DATA, ALLOC > &from)
copy operator
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class representing a record stored in a tabular database.
std::vector< T_DATA, ALLOC< T_DATA > > _row
the row itself
Definition: DBRow.h:198
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
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
void resize(const std::size_t new_size)
resize a given row, i.e., its number of elements
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
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