Bring in a row major version of Amit's matrix.

master
Zed A. Shaw 2 months ago
parent 5e63272f24
commit d47f6f996d
  1. 30
      amt/matrix.hpp

@ -1,4 +1,5 @@
#pragma once
#ifndef AMT_MATRIX_HPP
#define AMT_MATRIX_HPP
#include <cassert>
#include <cstddef>
@ -119,28 +120,28 @@ namespace amt {
constexpr const_reverse_iterator rbegin() const noexcept { return std::reverse_iterator(end()); }
constexpr const_reverse_iterator rend() const noexcept { return std::reverse_iterator(begin()); }
constexpr auto operator()(size_type r, size_type c) noexcept -> reference {
auto const index = r * m_col + c; // column-major;
constexpr auto operator()(size_type r, size_type c) noexcept -> reference {
auto const index = r + c * m_row; // row-major;
assert(index < size() && "Out of bound access");
return m_data[index];
}
constexpr auto operator()(size_type r, size_type c) const noexcept -> const_reference {
auto const index = r * m_col + c; // column-major;
auto const index = r + c * m_row; // row-major;
assert(index < size() && "Out of bound access");
return m_data[index];
}
constexpr auto operator[](size_type r) noexcept -> View<false> {
auto const base = r * m_col;
assert(r < rows() && "Out of bound access");
return { .data = m_data + base, .size = m_col };
constexpr auto operator[](size_type c) noexcept -> View<false> {
auto const base = c * m_row;
assert(c < cols() && "Out of bound access");
return { .data = m_data + base, .size = m_row };
}
constexpr auto operator[](size_type r) const noexcept -> View<true> {
auto const base = r * m_col;
assert(r < rows() && "Out of bound access");
return { .data = m_data + base, .size = m_col };
constexpr auto operator[](size_type c) const noexcept -> View<true> {
auto const base = c * m_row;
assert(c < cols() && "Out of bound access");
return { .data = m_data + base, .size = m_row };
}
friend void swap(Matrix& lhs, Matrix& rhs) noexcept {
@ -158,7 +159,7 @@ namespace amt {
} // namespace amt
#if 0
#include <format>
namespace std {
template <typename T>
@ -181,4 +182,5 @@ namespace std {
};
} // namespace std
#endif
#endif // AMT_MATRIX_HPP

Loading…
Cancel
Save