aGrUM  0.16.0
DBRowGeneratorParser_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  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 namespace gum {
29 
30  namespace learning {
31 
33  template <template<typename> class ALLOC>
35  const typename DatabaseTable<ALLOC>::Handler& handler,
36  const DBRowGeneratorSet<ALLOC>& generator_set,
37  const typename DBRowGeneratorParser<ALLOC>::allocator_type& alloc )
38  : __handler(handler)
39  , __generator_set(generator_set, alloc)
40  , __generator_size ( generator_set.size () ) {
41  GUM_CONSTRUCTOR(DBRowGeneratorParser);
42  }
43 
44 
46  template <template<typename> class ALLOC>
48  const DBRowGeneratorParser<ALLOC>& from,
49  const typename DBRowGeneratorParser<ALLOC>::allocator_type& alloc )
50  : __handler(from.__handler)
53  GUM_CONS_CPY(DBRowGeneratorParser);
54  }
55 
56 
58  template <template<typename> class ALLOC>
60  const DBRowGeneratorParser<ALLOC>& from)
61  : DBRowGeneratorParser<ALLOC> ( from, from.getAllocator () ) {}
62 
63 
65  template <template<typename> class ALLOC>
67  DBRowGeneratorParser<ALLOC>&& from,
68  const typename DBRowGeneratorParser<ALLOC>::allocator_type& alloc )
69  : __handler ( std::move(from.__handler) )
70  , __generator_set(std::move(from.__generator_set))
72  GUM_CONS_MOV(DBRowGeneratorParser);
73  }
74 
75 
77  template <template<typename> class ALLOC>
79  DBRowGeneratorParser<ALLOC>&& from)
80  : DBRowGeneratorParser<ALLOC> ( std::move(from), from.getAllocator() ) {}
81 
82 
84  template <template<typename> class ALLOC>
85  DBRowGeneratorParser<ALLOC>* DBRowGeneratorParser<ALLOC>::clone (
86  const typename DBRowGeneratorParser<ALLOC>::allocator_type& alloc) const {
87  ALLOC<DBRowGeneratorParser<ALLOC>> allocator ( alloc );
88  DBRowGeneratorParser<ALLOC>* new_parser = allocator.allocate(1);
89  try {
90  allocator.construct ( new_parser, *this, alloc );
91  }
92  catch ( ... ) {
93  allocator.deallocate ( new_parser, 1 );
94  throw;
95  }
96  return new_parser;
97  }
98 
99 
101  template <template<typename> class ALLOC>
102  INLINE DBRowGeneratorParser<ALLOC>*
104  return clone ( this->getAllocator () );
105  }
106 
107 
109  template <template<typename> class ALLOC>
111  GUM_DESTRUCTOR(DBRowGeneratorParser);
112  }
113 
114 
116  template <template<typename> class ALLOC>
117  INLINE DBRowGeneratorParser<ALLOC>&
119  const DBRowGeneratorParser<ALLOC>& from) {
120  if (this != &from) {
121  __handler = from.__handler;
122  __generator_set = from.__generator_set;
123  __generator_size = from.__generator_size;
124  }
125  return *this;
126  }
127 
128 
130  template <template<typename> class ALLOC>
131  INLINE DBRowGeneratorParser<ALLOC>&
132  DBRowGeneratorParser<ALLOC>::operator=(DBRowGeneratorParser<ALLOC>&& from) {
133  if (this != &from) {
134  __handler = std::move(from.__handler);
135  __generator_set = std::move(from.__generator_set);
136  __generator_size = from.__generator_size;
137  }
138  return *this;
139  }
140 
141 
143  template <template<typename> class ALLOC>
145  if ( ! __generator_size ) return __handler.hasRows();
146 
147  while ( ! __generator_set.hasRows() ) {
148  if ( ! __handler.hasRows () ) return false;
149  __generator_set.setInputRow( *__handler );
150  ++__handler;
151  }
152  return true;
153  }
154 
155 
157  template <template<typename> class ALLOC>
158  INLINE const DBRow<DBTranslatedValue,ALLOC>&
160  // if there exists no generator in the generator set, we just return
161  // the row pointed to by the handler
162  if ( ! __generator_size ) {
163  const auto& pointed_row = *__handler;
164  ++__handler;
165  return pointed_row;
166  }
167  else {
168  return __generator_set.generate();
169  }
170  }
171 
172 
174  template <template<typename> class ALLOC>
175  INLINE void DBRowGeneratorParser<ALLOC>::reset() {
176  __handler.reset();
177  __generator_set.reset();
178  }
179 
180 
181  // returns the handler used by the filter
182  template <template<typename> class ALLOC>
183  INLINE typename DatabaseTable<ALLOC>::Handler&
185  return __handler;
186  }
187 
188 
189  // returns the handler used by the filter
190  template <template<typename> class ALLOC>
191  INLINE const typename DatabaseTable<ALLOC>::Handler&
193  return __handler;
194  }
195 
196 
198  template <template<typename> class ALLOC>
199  INLINE const DatabaseTable< ALLOC >&
201  return static_cast<const DatabaseTable< ALLOC >&>
202  (__handler.database ());
203  }
204 
205 
207  template <template<typename> class ALLOC>
208  INLINE DBRowGeneratorSet<ALLOC>&
210  return __generator_set;
211  }
212 
213 
215  template <template<typename> class ALLOC>
216  INLINE void DBRowGeneratorParser<ALLOC>::setRange(std::size_t begin,
217  std::size_t end) {
218  __handler.setRange (begin,end);
219  }
220 
221 
223  template <template<typename> class ALLOC>
224  INLINE const DBRowGeneratorSet<ALLOC>&
226  return __generator_set;
227  }
228 
229 
232  template <template<typename> class ALLOC>
234  const std::vector<std::size_t,ALLOC<std::size_t>>& cols_of_interest ) {
235  __generator_set.setColumnsOfInterest ( cols_of_interest );
236  }
237 
238 
241  template <template<typename> class ALLOC>
243  std::vector<std::size_t,ALLOC<std::size_t>>&& cols_of_interest ) {
244  __generator_set.setColumnsOfInterest ( std::move ( cols_of_interest ) );
245  }
246 
247 
249  template <template<typename> class ALLOC>
250  template < typename GUM_SCALAR >
252  const BayesNet<GUM_SCALAR>& new_bn) {
253  __generator_set.setBayesNet(new_bn);
254  }
255 
256 
258  template <template<typename> class ALLOC>
261  return __generator_set.getAllocator ();
262  }
263 
264 
265  } /* namespace learning */
266 
267 } /* namespace gum */
268 
269 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
DatabaseTable< ALLOC >::Handler & handler()
returns the handler used by the parser
std::size_t __generator_size
the size of the generator set
virtual DBRowGeneratorParser< ALLOC > * clone() const
virtual copy constructor
void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to all the generators that depend on a BN
STL namespace.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
DBRowGeneratorParser< ALLOC > & operator=(const DBRowGeneratorParser< ALLOC > &from)
copy operator
void setColumnsOfInterest(const std::vector< std::size_t, ALLOC< std::size_t >> &cols_of_interest)
sets the columns of interest: the output DBRow needs only contain values fot these columns ...
typename IDatabaseTable< DBTranslatedValue, ALLOC >::Handler Handler
the unsafe handler type
allocator_type getAllocator() const
returns the allocator used
virtual ~DBRowGeneratorParser()
destructor
const DBRow< DBTranslatedValue, ALLOC > & row()
returns a new output row with its corresponding weight
bool hasRows()
returns true if there are still rows that can be output by the DBRowGeneratorParser ...
DBRowGeneratorSet< ALLOC > & generatorSet()
returns the generator set that is actually used
const DatabaseTable< ALLOC > & database() const
returns a reference on the database
ALLOC< DBTranslatedValue > allocator_type
type for the allocators passed in arguments of methods
DBRowGeneratorSet< ALLOC > __generator_set
the set of DBRow generators (might be empty)
void reset()
resets the parser
void setRange(std::size_t begin, std::size_t end)
sets the area in the database the handler will handle
DBRowGeneratorParser(const typename DatabaseTable< ALLOC >::Handler &handler, const DBRowGeneratorSet< ALLOC > &generator_set, const allocator_type &alloc=allocator_type())
default constructor
DatabaseTable< ALLOC >::Handler __handler
the handler that is really used to parse the database