MCPcopy Create free account
hub / github.com/android/ndk-samples / Matrix

Class Matrix

vectorization/src/main/cpp/matrix.h:30–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29template <size_t Rows, size_t Columns, typename T = float>
30class Matrix {
31 public:
32 Matrix() = default;
33 Matrix(T (&&cells)[Rows][Columns]) {
34 for (size_t row = 0; row < Rows; row++) {
35 for (size_t column = 0; column < Columns; column++) {
36 (*this)[row, column] = cells[row][column];
37 }
38 }
39 }
40
41 // Convenience constructor for vectors so callers don't need to use
42 // `{{x}, {y}, {z}}`.
43 Matrix(const std::array<T, Rows> cells)
44 requires(Columns == 1)
45 : cells_(cells) {}
46
47 [[nodiscard, clang::always_inline]] constexpr const T* _Nonnull data() const {
48 return cells_.data();
49 }
50
51 [[nodiscard, clang::always_inline]] constexpr T* _Nonnull data() {
52 return cells_.data();
53 }
54
55 [[nodiscard, clang::always_inline]] constexpr T& get(size_t row,
56 size_t column) {
57 return cells_[column * Rows + row];
58 }
59
60 [[nodiscard, clang::always_inline]] constexpr const T& get(
61 size_t row, size_t column) const {
62 return cells_[column * Rows + row];
63 }
64
65 [[nodiscard, clang::always_inline]] constexpr T& operator[](size_t row,
66 size_t column) {
67 return get(row, column);
68 }
69
70 [[nodiscard, clang::always_inline]] constexpr const T& operator[](
71 size_t row, size_t column) const {
72 return get(row, column);
73 }
74
75 [[nodiscard, clang::always_inline]] constexpr const std::span<const T> column(
76 size_t column) const {
77 return std::span{&get(0, column), Rows};
78 }
79
80 [[nodiscard, clang::always_inline]] constexpr std::span<T> column(
81 size_t column) {
82 return std::span{&get(0, column), Rows};
83 }
84
85 bool operator==(const Matrix<Rows, Columns, T>& rhs) const {
86 return cells_ == rhs.cells_;
87 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected