Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename t, int n=1, bool proxy=false>
- struct carray_iface{
- static_assert(n>0, "");
- typedef carray_iface<t, n, proxy> self_t;
- typedef typename conditional
- <proxy, size_t*, size_t[n]>::type shape_t;
- typedef typename conditional
- <(n>1), carray_iface<t, n-1, true>, t&>::type out_t;
- t* data;
- shape_t offset;
- public:
- //base constructor
- template <bool p = proxy, enable_if(not p)>
- $both carray_iface():
- data{nullptr},offset{0} {}
- //proxy constructor
- template <bool p = proxy, enable_if(p)>
- $both carray_iface(t* data, size_t* offset):
- data(data), offset(offset) {}
- /* multidimensional proxy[i][j][k][..] access */
- template <size_t d = n, enable_if(d > 1)>
- $both inline out_t operator [] (const size_t& i){
- return out_t(data + i*offset[1], offset+1);
- }
- template <size_t d = n, enable_if(d == 1)>
- $both inline out_t operator [] (const size_t& i){
- return data[i];
- }
- $both inline t& at(const size_t& pos){
- return data[pos];
- }
- /* relink data <- ptr; modify link */
- template <bool p = proxy, enable_if(not p)>
- $both inline self_t& relink
- (void*& link){
- data = (t*)link;
- link = data + offset[0];
- return self;
- }
- /* reshape (nx, ny, nz, ...) */
- template<int m=n, enable_if(m>1 and not proxy), typename ...tp>
- $both inline self_t&
- reshape(const size_t& i, tp... args){
- reshape<m-1>(args...);
- offset[n-m] = i * offset[n-m+1];
- return self;
- }
- template<int m=n, enable_if(m==1 and not proxy)>
- $both inline self_t&
- reshape(const size_t& i){
- offset[n-m]=i;
- return self;
- }
- /* len(gth) 0:full size, n:axis */
- $both inline size_t len
- (const size_t& i) const{
- if(i){
- return i<n ? offset[i-1]/offset[i] : offset[i-1];
- } else {
- return offset[0];
- }
- }
- /* size in bytes */
- $both inline size_t size() const{
- return offset[0]*sizeof(t);
- }
- /* is filled */
- $both inline explicit
- operator bool () const{
- return (data != nullptr);
- }
- };
- /**********************************************************************/
- template <typename t, int n>
- using carray_t = carray_iface<t,n,false>;
Advertisement
Add Comment
Please, Sign In to add comment