Guest User

Untitled

a guest
Jun 26th, 2020
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. template <typename t, int n=1, bool proxy=false>
  2. struct carray_iface{
  3.  
  4.     static_assert(n>0, "");
  5.    
  6.     typedef carray_iface<t, n, proxy>                self_t;
  7.     typedef typename conditional
  8.      <proxy, size_t*, size_t[n]>::type               shape_t;
  9.     typedef typename conditional
  10.      <(n>1), carray_iface<t, n-1, true>, t&>::type   out_t;
  11.    
  12.     t*        data;
  13.     shape_t offset;
  14. public:
  15.     //base constructor
  16.     template <bool p = proxy, enable_if(not p)>
  17.     $both carray_iface():
  18.         data{nullptr},offset{0} {}
  19.  
  20.     //proxy constructor
  21.     template <bool p = proxy, enable_if(p)>
  22.     $both carray_iface(t* data, size_t* offset):
  23.         data(data), offset(offset) {}
  24.  
  25.     /* multidimensional proxy[i][j][k][..] access */
  26.     template <size_t d = n, enable_if(d  > 1)>
  27.     $both inline out_t operator [] (const size_t& i){
  28.         return out_t(data + i*offset[1], offset+1);
  29.     }
  30.     template <size_t d = n, enable_if(d == 1)>
  31.     $both inline out_t operator [] (const size_t& i){
  32.         return data[i];
  33.     }
  34.     $both inline t& at(const size_t& pos){
  35.         return data[pos];
  36.     }
  37.    
  38.     /* relink data <- ptr; modify link */
  39.     template <bool p = proxy, enable_if(not p)>
  40.     $both inline self_t& relink
  41.     (void*& link){
  42.         data = (t*)link;
  43.         link = data + offset[0];
  44.         return self;
  45.     }
  46.    
  47.     /* reshape (nx, ny, nz, ...) */
  48.     template<int m=n, enable_if(m>1  and not proxy), typename ...tp>
  49.     $both inline self_t&
  50.     reshape(const size_t& i, tp... args){
  51.         reshape<m-1>(args...);
  52.         offset[n-m]  = i * offset[n-m+1];
  53.         return self;
  54.     }
  55.     template<int m=n, enable_if(m==1 and not proxy)>
  56.     $both inline self_t&
  57.     reshape(const size_t& i){
  58.         offset[n-m]=i;
  59.         return self;
  60.     }
  61.    
  62.     /* len(gth) 0:full size, n:axis */
  63.     $both inline size_t len
  64.     (const size_t& i) const{
  65.         if(i){
  66.             return i<n ? offset[i-1]/offset[i] : offset[i-1];
  67.         } else {
  68.             return offset[0];
  69.         }
  70.     }
  71.     /* size in bytes */
  72.     $both inline size_t size() const{
  73.         return offset[0]*sizeof(t);
  74.     }
  75.     /* is filled */
  76.     $both inline explicit
  77.     operator bool () const{
  78.         return (data != nullptr);
  79.     }
  80. };
  81.  
  82. /**********************************************************************/
  83.  
  84. template <typename t, int n>
  85. using carray_t = carray_iface<t,n,false>;
Advertisement
Add Comment
Please, Sign In to add comment