|
| 1 | +#ifndef GEMMI_CORE_MATRIX_VIEW_HPP |
| 2 | +#define GEMMI_CORE_MATRIX_VIEW_HPP |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <stdexcept> |
| 6 | +#include <type_traits> |
| 7 | + |
| 8 | +namespace gemmi::core { |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief Enum to specify the layout of the matrix in memory. |
| 12 | + */ |
| 13 | +enum class matrixLayout { |
| 14 | + rowMajor, ///< Matrix stored in row-major order. |
| 15 | + columnMajor ///< Matrix stored in column-major order. |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief Lightweight view of a dense matrix. |
| 20 | + * |
| 21 | + * It does not own the underlying memory but provides read and write access to it. |
| 22 | + * |
| 23 | + * @tparam value_t Element type. |
| 24 | + */ |
| 25 | +template <typename value_t> |
| 26 | +struct MatrixView { |
| 27 | + value_t* data; ///< Pointer to the matrix data. |
| 28 | + size_t rows; ///< Number of rows in the matrix. |
| 29 | + size_t cols; ///< Number of columns in the matrix. |
| 30 | + matrixLayout layout; ///< Layout of the matrix in memory. |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Default constructor. |
| 34 | + */ |
| 35 | + MatrixView() : |
| 36 | + data(nullptr), rows(0), cols(0), layout(matrixLayout::rowMajor) {} |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Construct a matrix view from raw parts. |
| 40 | + * |
| 41 | + * @param data Pointer to the first matrix element. |
| 42 | + * @param rows Number of rows. |
| 43 | + * @param cols Number of columns. |
| 44 | + * @param layout Memory layout. |
| 45 | + */ |
| 46 | + MatrixView(value_t* data, size_t rows, size_t cols, matrixLayout layout) : |
| 47 | + data(data), rows(rows), cols(cols), layout(layout) {} |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Construct a mutable matrix view from a vector. |
| 51 | + * |
| 52 | + * @param vec Vector containing the matrix data. |
| 53 | + * @param rows Number of rows. |
| 54 | + * @param cols Number of columns. |
| 55 | + * @param layout Memory layout. |
| 56 | + */ |
| 57 | + MatrixView(std::vector<value_t>& vec, size_t rows, size_t cols, matrixLayout layout) : |
| 58 | + data(vec.data()), rows(rows), cols(cols), layout(layout) {} |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Construct a read-only matrix view from a const vector. |
| 62 | + * |
| 63 | + * Only participates in overload resolution when @c value_t is const-qualified. |
| 64 | + * |
| 65 | + * @param vec Const vector containing the matrix data. |
| 66 | + * @param rows Number of rows. |
| 67 | + * @param cols Number of columns. |
| 68 | + * @param layout Memory layout. |
| 69 | + */ |
| 70 | + template <typename = std::enable_if_t<std::is_const_v<value_t>>> |
| 71 | + MatrixView(const std::vector<std::remove_const_t<value_t>>& vec, size_t rows, size_t cols, matrixLayout layout) : |
| 72 | + data(vec.data()), rows(rows), cols(cols), layout(layout) {} |
| 73 | + |
| 74 | + template <typename other_t, |
| 75 | + typename = std::enable_if_t< |
| 76 | + std::is_const_v<value_t> && |
| 77 | + std::is_same_v<std::remove_const_t<value_t>, other_t>>> |
| 78 | + MatrixView(const MatrixView<other_t>& other) : |
| 79 | + data(other.data), rows(other.rows), cols(other.cols), layout(other.layout) {} |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Return the number of stored elements. |
| 83 | + * |
| 84 | + * @return Number of rows by number of columns. |
| 85 | + */ |
| 86 | + size_t size() const { |
| 87 | + return rows * cols; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Return true if the view is empty. |
| 92 | + * |
| 93 | + * @return `true` if `rows == 0` or `cols == 0` |
| 94 | + */ |
| 95 | + bool empty() const { |
| 96 | + return rows == 0 || cols == 0; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @brief Compute the linear index of element (i, j). |
| 101 | + * |
| 102 | + * @param i Row index. |
| 103 | + * @param j Column index. |
| 104 | + * @return Linear index into the underlying data array. |
| 105 | + */ |
| 106 | + size_t index(size_t i, size_t j) const { |
| 107 | + return (layout == matrixLayout::rowMajor) |
| 108 | + ? (i * cols + j) |
| 109 | + : (j * rows + i); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @brief Access element (i, j). |
| 114 | + * |
| 115 | + * The `const` qualifier applies to the view metadata (pointer, dimensions, |
| 116 | + * and layout), and not to the pointed-to data. Mutability of the returned |
| 117 | + * reference is controlled by `value_t`. `MatrixView<const T>` should be |
| 118 | + * used for a read-only view. |
| 119 | + * |
| 120 | + * @param i Row index. |
| 121 | + * @param j Column index. |
| 122 | + * @return Reference to the element. |
| 123 | + */ |
| 124 | + value_t& operator()(size_t i, size_t j) const { |
| 125 | + return data[index(i, j)]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @brief Access element (i, j) explicitly. |
| 130 | + * |
| 131 | + * This method throws an exception if the access is out of bounds. |
| 132 | + * It is a safe alternative to operator(), which does not perform |
| 133 | + * bounds checking. |
| 134 | + * |
| 135 | + * @param i Row index. |
| 136 | + * @param j Column index. |
| 137 | + * @return Reference to the element. |
| 138 | + */ |
| 139 | + value_t& at(size_t i, size_t j) const { |
| 140 | + if (i >= rows || j >= cols) { |
| 141 | + throw std::out_of_range("MatrixView index out of range"); |
| 142 | + } |
| 143 | + return data[index(i, j)]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @brief Access element by linear index. |
| 148 | + * @param idx Linear index. |
| 149 | + * @return Reference to the element. |
| 150 | + */ |
| 151 | + value_t& linear(size_t idx) const { |
| 152 | + return data[idx]; |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +} // namespace gemmi::core |
| 157 | + |
| 158 | +#endif // GEMMI_CORE_MATRIX_VIEW_HPP |
0 commit comments