/usr/include/shark/Data/BatchInterfaceAdaptStruct.h is in libshark-dev 3.1.3+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | /*!
*
*
* \brief Defines an batch adptor for structures.
*
*
*
* \author O.Krause
* \date 2012
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SHARK_DATA_BATCHINTERFACEADAPTSTRUCT_H
#define SHARK_DATA_BATCHINTERFACEADAPTSTRUCT_H
#include <boost/fusion/sequence/intrinsic/swap.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <shark/Data/BatchInterface.h>
#include <boost/preprocessor/seq/transform.hpp>
#include "Impl/BoostFusion151DefineStructInl.hpp"
namespace shark{
namespace detail{
///serializes the object into the archive
template<class Archive>
struct ItemSerializer {
ItemSerializer(Archive& ar):m_ar(ar) {}
template<typename T>
void operator()(T& o)const{
m_ar & o;
}
private:
Archive& m_ar;
};
struct CreateBatch{
CreateBatch(std::size_t size):m_size(size) {}
template<class> struct result;
template<class T>
struct result<CreateBatch(T const&)> {
typedef typename shark::Batch<T>::type type;
};
template<class T>
typename result<CreateBatch(T const&)>::type operator()(T const& value)const{
return shark::Batch<T>::createBatch(value,m_size);
}
private:
std::size_t m_size;
};
struct resize{
resize(std::size_t size1, std::size_t size2):m_size1(size1),m_size2(size2){};
template<class T>
void operator()(T& batch)const{
shark::Batch<typename boost::range_value<T>::type>::resize(batch,m_size1,m_size2);
}
private:
std::size_t m_size1;
std::size_t m_size2;
};
///calls get(container,index) on a container. Used as boost fusion functor in the creation of references in the Batch Interface
struct MakeRef{
template<class> struct result;
template<class T>
struct result<MakeRef(T const&)> {
typedef typename boost::range_reference<T>::type type;
};
MakeRef(std::size_t index):m_index(index){}
template<class T>
typename result<MakeRef(T const&) >::type operator()(T const& container)const{
return get(const_cast<T&>(container),m_index);//we need the const cast since the argument type must be a const ref.
}
private:
std::size_t m_index;
};
///calls get(container,index) on a container. Used as boost fusion functor in the cration of references in the Batch Interface
struct MakeConstRef{
template<class> struct result;
template<class T>
struct result<MakeConstRef(T const&)> {
typedef typename boost::range_reference<T const>::type type;
};
MakeConstRef(std::size_t index):m_index(index){}
template<class T>
typename result<MakeConstRef(T const&) >::type operator()(T const& container)const{
return get(container,m_index);
}
private:
std::size_t m_index;
};
template<class FusionSequence>
struct FusionFacade: public FusionSequence{
FusionFacade(){}
template<class Sequence>
FusionFacade(Sequence const& sequence):FusionSequence(sequence){}
};
template<class Type>
struct isFusionFacade{
private:
struct Big{ int big[100]; };
template <class S>
static Big tester(FusionFacade<S>*);
template <class S>
static Big tester(FusionFacade<S> const*);
static char tester(...);
static Type* generator();
BOOST_STATIC_CONSTANT(std::size_t, size = sizeof(tester(generator())));
public:
BOOST_STATIC_CONSTANT(bool, value = (size!= 1));
typedef boost::mpl::bool_<value> type;
};
}
template<class S>
S& fusionize(detail::FusionFacade<S> & facade){
return static_cast<S&>(facade);
}
template<class S>
S const& fusionize(detail::FusionFacade<S> const& facade){
return static_cast<S const&>(facade);
}
template<class S>
typename boost::disable_if<detail::isFusionFacade<S>,S&>::type
fusionize(S& facade){
return facade;
}
template<class S>
typename boost::disable_if<detail::isFusionFacade<S>,S const& >::type
fusionize(S const& facade){
return facade;
}
}
#define SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL_IMPL(s,TYPE,ELEM)\
( typename Batch<BOOST_PP_TUPLE_ELEM(2, 0, ELEM)>::TYPE,BOOST_PP_TUPLE_ELEM(2, 1, ELEM))
#define SHARK_TRANSFORM_TUPLELIST_IMPL(s, data,ELEM)\
BOOST_PP_TUPLE_ELEM(2, 0, ELEM),BOOST_PP_TUPLE_ELEM(2, 1, ELEM)
#define SHARK_TRANSFORM_TUPLELIST(ELEMS)\
BOOST_PP_SEQ_TRANSFORM(SHARK_TRANSFORM_TUPLELIST_IMPL, _ , ELEMS)
#define SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL(TYPE,ATTRIBUTES)\
SHARK_TRANSFORM_TUPLELIST(BOOST_PP_SEQ_TRANSFORM(\
SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL_IMPL,\
TYPE, BOOST_PP_CAT(SHARK_FUSION_ADAPT_STRUCT_FILLER_0 ATTRIBUTES,_END)))
#define SHARK_TRANSFORM_BATCH_ATTRIBUTES_IMPL(s,TYPE,ELEM)\
( Batch<BOOST_PP_TUPLE_ELEM(2, 0, ELEM)>::TYPE,BOOST_PP_TUPLE_ELEM(2, 1, ELEM))
#define SHARK_TRANSFORM_BATCH_ATTRIBUTES(TYPE,ATTRIBUTES)\
SHARK_TRANSFORM_TUPLELIST(BOOST_PP_SEQ_TRANSFORM(\
SHARK_TRANSFORM_BATCH_ATTRIBUTES_IMPL,\
TYPE, BOOST_PP_CAT(SHARK_FUSION_ADAPT_STRUCT_FILLER_0 ATTRIBUTES,_END)))
///\brief creates default implementation for reference or const_reference types of Batches
#define SHARK_CREATE_BATCH_REFERENCES_TPL(ATTRIBUTES)\
private:\
SHARK_FUSION_DEFINE_STRUCT_REF_INLINE(FusionRef, SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL(reference,ATTRIBUTES))\
SHARK_FUSION_DEFINE_STRUCT_CONST_REF_INLINE(FusionConstRef, SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL(const_reference,ATTRIBUTES))\
public:\
struct reference: public detail::FusionFacade<FusionRef>{\
template<class Batch>\
reference(Batch& batch, std::size_t i)\
:detail::FusionFacade<FusionRef>(boost::fusion::transform(fusionize(batch),detail::MakeRef(i))){}\
template<class Other>\
reference& operator= (Other const& other ){\
fusionize(*this) = other;\
return *this;\
}\
reference& operator= (reference const& other ){\
fusionize(*this) = other;\
return *this;\
}\
friend void swap(reference op1, reference op2){\
boost::fusion::swap(op1,op2);\
}\
operator value_type()const{\
value_type ret;\
boost::fusion::copy(fusionize(*this), ret);\
return ret;\
}\
};\
struct const_reference: public detail::FusionFacade<FusionConstRef>{\
private:\
const_reference& operator= (const_reference const& other );\
public:\
template<class Batch>\
const_reference(Batch& batch, std::size_t i)\
:detail::FusionFacade<FusionConstRef>(boost::fusion::transform(fusionize(batch),detail::MakeConstRef(i))){}\
operator value_type()const{\
value_type ret;\
boost::fusion::copy(fusionize(*this), ret);\
return ret;\
}\
};
#define SHARK_CREATE_BATCH_REFERENCES(ATTRIBUTES)\
private:\
SHARK_FUSION_DEFINE_STRUCT_REF_INLINE(FusionRef, SHARK_TRANSFORM_BATCH_ATTRIBUTES(reference,ATTRIBUTES))\
SHARK_FUSION_DEFINE_STRUCT_CONST_REF_INLINE(FusionConstRef, SHARK_TRANSFORM_BATCH_ATTRIBUTES(const_reference,ATTRIBUTES))\
public:\
struct reference: public detail::FusionFacade<FusionRef>{\
template<class Batch>\
reference(Batch& batch, std::size_t i)\
:detail::FusionFacade<FusionRef>(boost::fusion::transform(fusionize(batch),detail::MakeRef(i))){}\
template<class Other>\
reference& operator= (Other const& other ){\
fusionize(*this) = other;\
return *this;\
}\
reference& operator= (reference const& other ){\
fusionize(*this) = other;\
return *this;\
}\
friend void swap(reference& op1, reference& op2){\
boost::fusion::swap(op1,op2);\
}\
operator value_type()const{\
value_type ret;\
boost::fusion::copy(fusionize(*this), ret);\
return ret;\
}\
};\
struct const_reference: public detail::FusionFacade<FusionConstRef>{\
template<class Batch>\
const_reference(Batch& batch, std::size_t i)\
:detail::FusionFacade<FusionConstRef>(boost::fusion::transform(fusionize(batch),detail::MakeConstRef(i))){}\
template<class Other>\
const_reference& operator= (Other const& other ){\
fusionize(*this) = other;\
return *this;\
}\
operator value_type()const{\
value_type ret;\
boost::fusion::copy(fusionize(*this), ret);\
return ret;\
}\
};
///\brief creates default typedefs for iterator or const_iterator types of Batches
#define SHARK_CREATE_BATCH_ITERATORS()\
typedef ProxyIterator<type, value_type, reference > iterator;\
typedef ProxyIterator<const type, value_type, const_reference > const_iterator;\
iterator begin(){\
return iterator(*this,0);\
}\
const_iterator begin()const{\
return const_iterator(*this,0);\
}\
iterator end(){\
return iterator(*this,size());\
}\
const_iterator end()const{\
return const_iterator(*this,size());\
}\
///\brief This macro can be used to specialize a structure type easily to a batch type.
///
///Assume, that your input Data looks like:
///<code>
///template<class T>
///struct DataType{
/// RealVector A;
/// T B;
///};
///</code>
///Than the Batch type should propably look like
///<code>
///struct DataTypeBatch{
/// RealMatrix A;
/// RealVector B;
///};
///</code>
///In this case the macro can be used to generate a complete specialisation of Batch<DataType>
///<code>
///#define DataVars (RealVector, A)(double B)
///
///SHARK_CREATE_BATCH_INTERFACE( DataType,DataVars)
///};
///As any other batch model th result also offers iterators over the range of elements.
///In this case also boost::fusion support is added to the sequence. e.g. it is
///handled similar to any other tuple type (RealMatrix,RealVector). This is useful for MKL or Transfer
///kernels
///</code>
#define SHARK_CREATE_BATCH_INTERFACE(NAME,ATTRIBUTES)\
private:\
SHARK_FUSION_DEFINE_STRUCT_INLINE(FusionType, SHARK_TRANSFORM_BATCH_ATTRIBUTES_TPL(type,ATTRIBUTES))\
public:\
struct type: public detail::FusionFacade<FusionType>{\
typedef NAME value_type;\
\
SHARK_CREATE_BATCH_REFERENCES_TPL(ATTRIBUTES)\
SHARK_CREATE_BATCH_ITERATORS()\
\
type(){}\
type(std::size_t size1, std::size_t size2){\
resize(size1,size2);\
}\
void resize(std::size_t batchSize, std::size_t elementSize){\
boost::fusion::for_each(fusionize(*this), detail::resize(batchSize,elementSize));\
}\
\
friend void swap(type& op1, type& op2){\
boost::fusion::swap(fusionize(op1),fusionize(op2));\
}\
std::size_t size()const{\
return shark::size(boost::fusion::at_c<0>(fusionize(*this)));\
}\
template<class Archive>\
void serialize(Archive & archive,unsigned int version)\
{\
boost::fusion::for_each(fusionize(*this), detail::ItemSerializer<Archive>(archive));\
}\
};\
typedef NAME value_type;\
typedef typename type::reference reference;\
typedef typename type::const_reference const_reference;\
typedef typename type::iterator iterator;\
typedef typename type::const_iterator const_iterator;\
\
static type createBatch(value_type const& input, std::size_t size = 1){\
type batch;\
boost::fusion::copy(boost::fusion::transform(input,detail::CreateBatch(size)),fusionize(batch));\
return batch;\
}\
template<class Range>\
static type createBatchFromRange(Range const& range){\
std::size_t points = shark::size(range);\
type batch = createBatch(*range.begin(),points);\
typename boost::range_iterator<Range>::type pos = range.begin();\
for(std::size_t i = 0; i != points; ++i,++pos){\
get(batch,i) = *pos;\
}\
return batch;\
}\
static void resize(type& batch, std::size_t batchSize, std::size_t elements){\
batch.resize(batchSize,elements);\
}
///\brief This macro can be used to specialize a structure type easily to a batch type.
///
///Assume, thjat your input Data looks like:
///<code>
///struct DataType{
/// RealVector A;
/// double B;
///};
///</code>
///Than the Batch type should propably look like
///<code>
///struct DataTypeBatch{
/// RealMatrix A;
/// RealVector B;
///};
///</code>
///In this case the macro can be used to generate a complete specialisation of Batch<DataType>
///<code>
///#define DataVars (RealVector, A)(double B)
///
///SHARK_CREATE_BATCH_INTERFACE( DataType,DataVars)
///};
///As any other batch model the result also offers iterators over the range of elements.
///In this case also boost::fusion support is added to the sequence. e.g. it is
///handled similar to any other tuple type (RealMatrix,RealVector). This is useful for MKL or Transfer
///kernels
///</code>
#define SHARK_CREATE_BATCH_INTERFACE_NO_TPL(NAME,ATTRIBUTES)\
private:\
SHARK_FUSION_DEFINE_STRUCT_INLINE(FusionType, SHARK_TRANSFORM_BATCH_ATTRIBUTES(type,ATTRIBUTES))\
public:\
struct type: public detail::FusionFacade<FusionType>{\
typedef NAME value_type;\
\
SHARK_CREATE_BATCH_REFERENCES(ATTRIBUTES)\
SHARK_CREATE_BATCH_ITERATORS()\
\
type(){}\
type(std::size_t size1, std::size_t size2){\
resize(size1,size2);\
}\
void resize(std::size_t batchSize, std::size_t elementSize){\
boost::fusion::for_each(fusionize(*this), detail::resize(batchSize,elementSize));\
}\
\
friend void swap(type& op1, type& op2){\
boost::fusion::swap(fusionize(op1),fusionize(op2));\
}\
std::size_t size()const{\
return shark::size(boost::fusion::at_c<0>(fusionize(*this)));\
}\
template<class Archive>\
void serialize(Archive & archive,unsigned int version)\
{\
boost::fusion::for_each(fusionize(*this), detail::ItemSerializer<Archive>(archive));\
}\
};\
typedef NAME value_type;\
typedef type::reference reference;\
typedef type::const_reference const_reference;\
typedef type::iterator iterator;\
typedef type::const_iterator const_iterator;\
\
static type createBatch(value_type const& input, std::size_t size = 1){\
type batch;\
boost::fusion::copy(boost::fusion::transform(input,detail::CreateBatch(size)),fusionize(batch));\
return batch;\
}\
template<class Range>\
static type createBatchFromRange(Range const& range){\
std::size_t points = shark::size(range);\
type batch = createBatch(*range.begin(),points);\
typename boost::range_iterator<Range>::type pos = range.begin();\
for(std::size_t i = 0; i != points; ++i,++pos){\
get(batch,i) = *pos;\
}\
return batch;\
}\
static void resize(type& batch, std::size_t batchSize, std::size_t elements){\
batch.resize(batchSize,elements);\
}
#endif
|