Guest User

Untitled

a guest
Mar 1st, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4. #include <algorithm>
  5. #include <memory>
  6.  
  7.  
  8. template<typename T>
  9. class as_matrix
  10. {
  11.     size_t nc = 0, nr = 0;
  12.     const bool col_major = true;
  13.  
  14.     // column major indexer
  15.     auto cm_indexer(size_t i, size_t j) -> T&
  16.     {
  17.         assert(i >= 0 && j >= 0 && i < n_rows && j < n_cols);
  18.         return ptr[j * n_rows + i];
  19.     }
  20.  
  21.     // row major indexer
  22.     auto rm_indexer(size_t i, size_t j) -> T&
  23.     {
  24.         assert(i >= 0 && j >= 0 && i < n_rows && j < n_cols);
  25.         return ptr[i * n_cols + j];
  26.     }
  27.  
  28. public:
  29.     T* ptr = nullptr;
  30.     T& (as_matrix<T>::*indexer) (size_t, size_t) = nullptr;
  31.  
  32.     const size_t& n_rows = nr, n_cols = nc;
  33.  
  34.     as_matrix<T>& operator=(const as_matrix<T> matrix)
  35.     {
  36.         nr = matrix.nr;
  37.         nc = matrix.nc;
  38.         ptr = matrix.ptr;
  39.         indexer = (matrix.col_major)? &as_matrix<T>::cm_indexer: &as_matrix<T>::rm_indexer;
  40.         return *this;
  41.     }
  42.  
  43.     as_matrix(T *ptr, size_t n_rows, size_t n_cols, bool col_major = true)
  44.         : ptr(ptr), col_major(col_major), indexer((col_major)? &as_matrix<T>::cm_indexer: &as_matrix<T>::rm_indexer),
  45.           nr(n_rows), nc(n_cols)
  46.     {
  47.  
  48.     }
  49.  
  50.     auto operator()(size_t i, size_t j) const -> const T&
  51.     {
  52.         return ptr[j * n_rows + i];
  53.     }
  54.  
  55.     auto operator()(size_t i, size_t j) -> T&
  56.     {
  57.         return ptr[j * n_rows + i];
  58.     }
  59. };
  60.  
  61.  
  62. template<typename T>
  63. auto copy(const as_matrix<T>& matrix)
  64. {
  65.     auto owner = std::make_unique<T[]>(matrix.n_rows * matrix.n_cols);
  66.     auto res   = as_matrix(owner.get(), matrix.n_rows, matrix.n_cols);
  67.     std::copy(matrix.ptr, matrix.ptr + matrix.n_rows * matrix.n_cols, res.ptr);
  68.     return std::make_tuple(std::move(owner), res);
  69. }
  70.  
  71. template<typename T>
  72. auto t(as_matrix<T>& matrix)
  73. {
  74.     auto [owner, temp] = copy(matrix);
  75.     for (size_t i = 0; i < matrix.n_rows; i++)
  76.     {
  77.         for (size_t j = 0; j < matrix.n_cols; j++)
  78.         {
  79.             matrix.ptr[i * matrix.n_cols + j] = temp.ptr[j * matrix.n_rows + i];
  80.         }
  81.     }
  82.     matrix = as_matrix(matrix.ptr, matrix.n_cols, matrix.n_rows);
  83. }
  84.  
  85.  
  86. template<typename T>
  87. auto get(as_matrix<T>& matrix, size_t index)
  88. {
  89.     return (matrix.col_major)? &matrix.ptr[matrix.n_cols * index]: &matrix.ptr[matrix.n_rows * index];
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment