30 #include <boost/foreach.hpp>
33 #include "../utilities/container_utils.hpp"
34 #include "../error/exception_utils.hpp"
35 #include "../algorithms.hpp"
101 template<
template<
typename ...>
class CTYPE,
typename ...OTS>
103 const CTYPE<OTS...> &ooffset,
104 const CTYPE<OTS...> &ostride):
105 _oshape(oshape.
size()),
106 _offset(ooffset.
size()),
107 _stride(ostride.
size())
109 std::copy(oshape.begin(),oshape.end(),_oshape.begin());
110 std::copy(ooffset.begin(),ooffset.end(),_offset.begin());
111 std::copy(ostride.begin(),ostride.end(),_stride.begin());
132 index_type &&ostride):
133 _oshape(
std::move(oshape)),
134 _offset(
std::move(ooffset)),
135 _stride(
std::move(ostride))
149 const index_type &ooffset,
150 const index_type &ostride):
172 _oshape(
std::move(s._oshape)),
173 _offset(
std::move(s._offset)),
174 _stride(
std::move(s._stride))
183 if(
this == &s)
return *
this;
198 if(
this == &s)
return *
this;
200 _oshape = std::move(s._oshape);
201 _offset = std::move(s._offset);
202 _stride = std::move(s._stride);
210 template<
typename CTYPE,
212 std::is_same<typename CTYPE::value_type,pni::core::slice>::value
218 auto shape = cutils_type::create(s.size());
219 auto offset = cutils_type::create(s.size());
220 auto stride = cutils_type::create(s.size());
224 BOOST_FOREACH(
auto sl,s)
229 offset[
index] = sl.first();
230 stride[
index] = sl.stride();
254 return _oshape.size() - std::count(_oshape.begin(),_oshape.end(),1);
268 template<
typename CTYPE> CTYPE
shape()
const
271 auto c = cutils_type::create(
rank());
275 std::copy_if(_oshape.begin(),_oshape.end(),c.begin(),
276 [](
size_t i){
return i!=1; });
292 typedef typename index_type::value_type value_type;
293 if(_oshape.empty())
return 0;
295 if(
rank() == 0)
return 1;
298 return std::accumulate(_oshape.begin(),_oshape.end(),
300 std::multiplies<value_type>());
327 auto c = cutils_type::create(_oshape.size());
328 std::copy(_oshape.begin(),_oshape.end(),c.begin());
351 template<
typename CTYPE> CTYPE
offset()
const
354 auto c = cutils_type::create(_offset.size());
355 std::copy(_offset.begin(),_offset.end(),c.begin());
377 template<
typename CTYPE> CTYPE
stride()
const
380 auto c = cutils_type::create(_stride.size());
382 std::copy(_stride.begin(),_stride.end(),c.begin());
428 void index(
const ITYPE &sindex,OITYPE &oindex)
const
441 auto os_iter = _oshape.begin();
442 auto st_iter = _stride.begin();
443 auto si_iter = sindex.begin();
444 auto of_iter = _offset.begin();
447 for(
auto &oi: oindex)
449 size_t index = *of_iter++;
451 if(*os_iter++ != 1) index += (*st_iter)*(*si_iter++);
476 OITYPE
index(
const ITYPE &sindex)
const
479 auto oindex = cutils_type::create(_oshape.size());
481 try{
index(sindex,oindex); }
515 s.
index(index,orig_index);
517 return map.offset(orig_index);
534 template<
typename MAPT>
537 typedef std::vector<size_t> index_type;
539 auto index = s.template shape<index_type>();
541 std::fill(index.begin(),index.end(),size_t(0));
543 return offset(map,s,index);
560 template<
typename MAPT>
563 typedef std::vector<size_t> index_type;
565 auto index = s.template shape<index_type>();
566 std::transform(index.begin(),index.end(),index.begin(),
567 [](
const size_t &i){
return i-size_t(1); });
569 return offset(map,s,index);
586 template<
typename MAPT>
595 if( (orig_last_offset - orig_first_offset + 1) == s.
size())
index_type _offset
offset in the original array
Definition: array_selection.hpp:84
static container_type create(size_t n, value_type default_value=value_type())
create container of given size
Definition: container_utils.hpp:85
array_selection(array_selection &&s)
move constructor
Definition: array_selection.hpp:171
CTYPE full_shape() const
get full shape
Definition: array_selection.hpp:324
Size mismatch error.
Definition: exceptions.hpp:399
size_t size() const
get selection size
Definition: array_selection.hpp:290
OITYPE index(const ITYPE &sindex) const
compute the original index
Definition: array_selection.hpp:476
array_selection(const index_type &oshape, const index_type &ooffset, const index_type &ostride)
constructor
Definition: array_selection.hpp:148
array_selection & operator=(const array_selection &s)
copy assignment operator
Definition: array_selection.hpp:181
index_type _oshape
shape of the selection in the original array
Definition: array_selection.hpp:82
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
size_t rank() const
get effective rank
Definition: array_selection.hpp:252
size_t last_offset(const MAPT &map, const array_selection &s)
compute last element offset
Definition: array_selection.hpp:561
array_selection()
default constructor
Definition: array_selection.hpp:118
container utility
Definition: container_utils.hpp:59
selection from a multidimensional array
Definition: array_selection.hpp:72
size_t start_offset(const MAPT &map, const array_selection &s)
compute first element offset
Definition: array_selection.hpp:535
index_type _stride
stride in the original array
Definition: array_selection.hpp:86
Definition: add_op.hpp:29
array_selection(const CTYPE< OTS...> &oshape, const CTYPE< OTS...> &ooffset, const CTYPE< OTS...> &ostride)
constructor
Definition: array_selection.hpp:102
invoke< std::enable_if< C::value >> enable_if
shortcut for std::enable_if
Definition: sfinae_macros.hpp:108
const index_type & full_shape() const
get full shape reference
Definition: array_selection.hpp:312
#define EXCEPTION_FORWARD(ETYPE)
forward an exception
Definition: exceptions.hpp:67
array_selection(const array_selection &s)
copy constructor
Definition: array_selection.hpp:161
const index_type & offset() const
get reference to offset
Definition: array_selection.hpp:341
bool check_equal_size(const A &a, const B &b)
check if two container have equal size
Definition: exception_utils.hpp:77
array_selection(index_type &&oshape, index_type &ooffset, index_type &&ostride)
constructor
Definition: array_selection.hpp:131
const index_type & stride() const
get reference to stride
Definition: array_selection.hpp:366
void index(const ITYPE &sindex, OITYPE &oindex) const
compute original index
Definition: array_selection.hpp:428
CTYPE offset() const
get offset
Definition: array_selection.hpp:351
std::vector< size_t > index_type
private index type
Definition: array_selection.hpp:76
CTYPE stride() const
get stride
Definition: array_selection.hpp:377
CTYPE shape() const
get effective shape
Definition: array_selection.hpp:268
array_selection & operator=(array_selection &&s)
move asignment operator
Definition: array_selection.hpp:196
size_t offset(const MAPT &map, const array_selection &s, const CTYPE &index)
compute offset
Definition: array_selection.hpp:510
size_t size(const slice &s)
compute slice size
bool is_contiguous(const MAPT &map, const array_selection &s)
check continuity of a selection
Definition: array_selection.hpp:587
static array_selection create(const CTYPE &s)
static creation function
Definition: array_selection.hpp:215