Advertisement
JeWe37

Untitled

Nov 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3. #include <initializer_list>
  4. #include <iterator>
  5. #include <array>
  6. #include <algorithm>
  7.  
  8. namespace linAlg {
  9.   template<typename T, unsigned ...dims> class Tensor {
  10.   private:
  11.     template<unsigned N, typename TT, unsigned ...dims1> struct Type;
  12.  
  13.     template<unsigned N, unsigned dim, unsigned ...dims1, unsigned ...dims2> struct Type<N, std::integer_sequence<unsigned, dim, dims1...>, dims2...> {
  14.       using type = typename Type<N-1, std::integer_sequence<unsigned, dims1...>, dims2..., dim>::type;
  15.     };
  16.  
  17.     template<unsigned dim, unsigned ...dims1, unsigned ...dims2> struct Type<0, std::integer_sequence<unsigned, dim, dims1...>, dims2...> {
  18.       using type = Tensor<Tensor<T, dim, dims1...>, dims2...>;
  19.     };
  20.  
  21.     template<unsigned N, typename dims1> struct getDim;
  22.  
  23.     template<unsigned N, unsigned dim, unsigned ...dims1> struct getDim<N, std::integer_sequence<unsigned, dim, dims1...>> {
  24.       static const unsigned value = getDim<N-1, std::integer_sequence<unsigned, dims1...>>::value;
  25.     };
  26.  
  27.     template<unsigned dim, unsigned ...dims1> struct getDim<0, std::integer_sequence<unsigned, dim, dims1...>> {
  28.       static const unsigned value = dim;
  29.     };
  30.  
  31.   public:
  32.     std::array<T, (dims*...)> scalar;
  33.  
  34.     Tensor() = default;
  35.  
  36.     Tensor(const Tensor<T, dims...>& tensorIn) {
  37.       for (unsigned i = 0; i < (dims*...); i++)
  38.         scalar[i] = tensorIn.scalar[i];
  39.     }
  40.  
  41.     Tensor(Tensor<T, dims...>&& tensorIn) {
  42.       for (unsigned i = 0; i < (dims*...); i++)
  43.         scalar[i] = std::move(tensorIn.scalar[i]);
  44.     }
  45.  
  46.     template<typename ...TT> Tensor(const TT&... args)
  47.     : scalar{args...} {}
  48.  
  49.     Tensor(std::initializer_list<T> list) {
  50.       std::copy(list.begin(), list.end(), std::inserter(scalar, scalar.begin()));
  51.     }
  52.  
  53.     template<unsigned ...dims1> Tensor(Tensor<T, dims1...> tensorIn) {
  54.       // TODO: constructor from higher and lower dimensional tensor
  55.     }
  56.  
  57.     static constexpr unsigned getOrder() {
  58.       return sizeof...(dims);
  59.     }
  60.  
  61.     template<unsigned N> static constexpr unsigned getNthDim() {
  62.       return getDim<N, std::integer_sequence<unsigned, dims...>>::value;
  63.     }
  64.  
  65.     template<unsigned dimInt> auto getLower() {
  66.       return typename Type<dimInt, std::integer_sequence<unsigned, dims...>>::type(*this);
  67.     }
  68.  
  69.     using type = T;
  70.   };
  71.  
  72.   template<typename T, unsigned dim1> using Vector = Tensor<T, dim1>;
  73.   template<typename T, unsigned dim1, unsigned dim2> using Matrix = Tensor<T, dim1, dim2>;
  74. }
  75.  
  76. int main() {
  77.   std::cout << linAlg::Tensor<float, 1, 2, 3, 4, 5, 6, 7, 8>().getLower<5>().scalar.at(0).getOrder() << std::endl;
  78.   return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement