32 #ifndef SHARK_DATA_BATCHINTERFACE_H 33 #define SHARK_DATA_BATCHINTERFACE_H 38 #include <boost/preprocessor.hpp> 40 #include <boost/type_traits/is_arithmetic.hpp> 41 #include <boost/type_traits/is_same.hpp> 42 #include <boost/utility/enable_if.hpp> 43 #include <boost/range/algorithm_ext/iota.hpp> 44 #include <boost/mpl/if.hpp> 56 typedef T
const& const_reference;
58 typedef std::vector<T> type;
64 typedef typename type::iterator iterator;
66 typedef typename type::const_iterator const_iterator;
71 return type(
size,input);
76 static type createBatchFromRange(
Range const& range){
77 return type(range.begin(),range.end());
80 static void resize(type& batch, std::size_t batchSize, std::size_t elements){
81 batch.resize(batchSize);
87 struct ArithmeticBatch{
91 typedef T
const& const_reference;
99 typedef typename type::iterator iterator;
101 typedef typename type::const_iterator const_iterator;
106 return type(
size,input);
109 template<
class Range>
110 static type createBatchFromRange(
Range const& range){
111 type batch(range.size());
112 std::copy(range.begin(),range.end(),batch.begin());
116 static void resize(type& batch, std::size_t batchSize, std::size_t elements){
117 ensure_size(batch,batchSize);
123 template<
class Matrix,
class Vector>
124 class MatrixRowReference:
public blas::temporary_proxy<blas::matrix_row<Matrix> >{
126 typedef blas::temporary_proxy<blas::matrix_row<Matrix> > base_type;
128 MatrixRowReference( Matrix& matrix, std::size_t i)
129 :base_type(blas::matrix_row<Matrix>(matrix,i)){}
131 MatrixRowReference(T
const& matrixrow)
132 :base_type(blas::matrix_row<Matrix>(matrixrow.expression().expression(),matrixrow.index())){}
135 const MatrixRowReference& operator=(
const T& argument){
136 static_cast<base_type&
>(*this)=argument;
141 return Vector(*
this);
145 template<
class M,
class V>
146 void swap(MatrixRowReference<M,V> ref1, MatrixRowReference<M,V> ref2){
147 swap_rows(ref1.expression().expression(),ref1.index(),ref2.expression().expression(),ref2.index());
162 :
public boost::mpl::if_<
163 boost::is_arithmetic<T>,
164 detail::ArithmeticBatch<T>,
165 detail::DefaultBatch<T>
170 template<
class T,
class Range>
175 template<
class Range>
184 struct Batch<blas::vector<T> >{
192 typedef detail::MatrixRowReference<type,value_type> reference;
194 typedef detail::MatrixRowReference<const type,value_type> const_reference;
203 template<
class Element>
205 return type(
size,input.size());
208 template<
class Range>
209 static type createBatchFromRange(
Range const& range){
210 type batch(range.size(),range.begin()->size());
211 std::copy(range.begin(),range.end(),boost::begin(batch));
215 static void resize(type& batch, std::size_t batchSize, std::size_t elements){
216 ensure_size(batch,batchSize,elements);
232 typedef detail::MatrixRowReference<type,value_type> reference;
235 typedef detail::MatrixRowReference<const type,value_type> const_reference;
244 template<
class Element>
246 return type(
size,input.size());
249 template<
class Range>
250 static type createBatchFromRange(
Range const& range){
252 std::size_t nonzeros = 0;
253 for(
typename Range::const_iterator pos = range.begin(); pos != range.end(); ++pos){
254 nonzeros += pos->nnz();
257 type batch(range.size(),range.begin()->size(),nonzeros);
258 std::copy(range.begin(),range.end(),boost::begin(batch));
263 static void resize(type& batch, std::size_t batchSize, std::size_t elements){
264 ensure_size(batch,batchSize,elements);
269 struct Batch<blas::matrix<T> >{
277 typedef typename type::reference reference;
279 typedef typename type::const_reference const_reference;
283 typedef typename type::iterator iterator;
285 typedef typename type::const_iterator const_iterator;
288 template<
class Element>
290 return type(
size,input);
293 template<
class Range>
294 static type createBatchFromRange(
Range const& range){
295 type batch(range.size());
296 std::copy(range.begin(),range.end(),batch.begin());
316 struct range_mutable_iterator< shark::
blas::matrix<T> >{
342 typedef typename range_mutable_iterator<M>::type type;
347 typedef typename range_const_iterator<M>::type type;
353 typedef typename range_mutable_iterator<M>::type type;
358 typedef typename range_const_iterator<M>::type type;
365 typedef shark::detail::MatrixRowReference<shark::blas::dense_matrix_adaptor<T>,Vector> reference;
372 typedef shark::detail::MatrixRowReference<shark::blas::dense_matrix_adaptor<T>
const,Vector> reference;
377 namespace shark{
namespace blas{
381 typename boost::range_iterator<matrix<T>
const>::type
382 range_begin( matrix<T>
const& m )
384 typedef typename boost::range_iterator<matrix<T>
const>::type Iter;
388 typename boost::range_iterator<matrix<T> >::type
389 range_begin( matrix<T>& m )
391 typedef typename boost::range_iterator<matrix<T> >::type Iter;
396 typename boost::range_iterator<matrix<T>
const>::type
397 range_end( matrix<T>
const& m )
399 typedef typename boost::range_iterator<matrix<T>
const>::type Iter;
400 return Iter(m,m.size1());
403 typename boost::range_iterator<matrix<T> >::type
404 range_end( matrix<T>& m )
406 typedef typename boost::range_iterator<matrix<T> >::type Iter;
407 return Iter(m,m.size1());
412 typename boost::range_iterator<compressed_matrix<T>
const>::type
413 range_begin( compressed_matrix<T>
const& m )
415 typedef typename boost::range_iterator<compressed_matrix<T>
const>::type Iter;
419 typename boost::range_iterator<compressed_matrix<T> >::type
420 range_begin( compressed_matrix<T>& m )
422 typedef typename boost::range_iterator<compressed_matrix<T> >::type Iter;
427 typename boost::range_iterator<compressed_matrix<T>
const>::type
428 range_end( compressed_matrix<T>
const& m )
430 typedef typename boost::range_iterator<compressed_matrix<T>
const>::type Iter;
431 return Iter(m,m.size1());
434 typename boost::range_iterator<compressed_matrix<T> >::type
435 range_end( compressed_matrix<T>& m )
437 typedef typename boost::range_iterator<compressed_matrix<T> >::type Iter;
438 return Iter(m,m.size1());
443 typename boost::range_iterator<M const>::type
444 range_begin( matrix_container<M>
const& m )
446 return range_begin(m());
449 typename boost::range_iterator<M>::type
450 range_begin( matrix_container<M>& m )
452 return range_begin(m());
456 typename boost::range_iterator<M const>::type
457 range_end( matrix_container<M>
const& m )
459 return range_end(m());
462 typename boost::range_iterator<M>::type
463 range_end( matrix_container<M>& m )
465 return range_end(m());
470 typename boost::range_iterator<M const>::type
471 range_begin( matrix_expression<M>
const& m )
473 return range_begin(m());
476 typename boost::range_iterator<M>::type
477 range_begin( matrix_expression<M>& m )
479 return range_begin(m());
483 typename boost::range_iterator<M const>::type
484 range_end( matrix_expression<M>
const& m )
486 return range_end(m());
489 typename boost::range_iterator<M>::type
490 range_end( matrix_expression<M>& m )
492 return range_end(m());
497 typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T>
const>::type
500 typedef typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T>
const>::type Iter;
504 typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T> >::type
507 typedef typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T> >::type Iter;
512 typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T>
const>::type
515 typedef typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T>
const>::type Iter;
516 return Iter(m,m.
size1());
519 typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T> >::type
522 typedef typename boost::range_iterator<shark::blas::dense_matrix_adaptor<T> >::type Iter;
523 return Iter(m,m.
size1());