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

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

@ -1,4 +1,5 @@
#pragma once #ifndef AMT_MATRIX_HPP
#define AMT_MATRIX_HPP
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
@ -82,7 +83,7 @@ namespace amt {
} }
Matrix(std::initializer_list<std::initializer_list<value_type>> li) Matrix(std::initializer_list<std::initializer_list<value_type>> li)
: m_row(li.size()) : m_row(li.size())
{ {
for (auto const& row: li) { for (auto const& row: li) {
m_col = std::max(m_col, row.size()); m_col = std::max(m_col, row.size());
@ -119,28 +120,28 @@ namespace amt {
constexpr const_reverse_iterator rbegin() const noexcept { return std::reverse_iterator(end()); } 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 const_reverse_iterator rend() const noexcept { return std::reverse_iterator(begin()); }
constexpr auto operator()(size_type r, size_type c) noexcept -> reference { constexpr auto operator()(size_type r, size_type c) noexcept -> 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"); assert(index < size() && "Out of bound access");
return m_data[index]; return m_data[index];
} }
constexpr auto operator()(size_type r, size_type c) const noexcept -> const_reference { 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"); assert(index < size() && "Out of bound access");
return m_data[index]; return m_data[index];
} }
constexpr auto operator[](size_type r) noexcept -> View<false> { constexpr auto operator[](size_type c) noexcept -> View<false> {
auto const base = r * m_col; auto const base = c * m_row;
assert(r < rows() && "Out of bound access"); assert(c < cols() && "Out of bound access");
return { .data = m_data + base, .size = m_col }; return { .data = m_data + base, .size = m_row };
} }
constexpr auto operator[](size_type r) const noexcept -> View<true> { constexpr auto operator[](size_type c) const noexcept -> View<true> {
auto const base = r * m_col; auto const base = c * m_row;
assert(r < rows() && "Out of bound access"); assert(c < cols() && "Out of bound access");
return { .data = m_data + base, .size = m_col }; return { .data = m_data + base, .size = m_row };
} }
friend void swap(Matrix& lhs, Matrix& rhs) noexcept { friend void swap(Matrix& lhs, Matrix& rhs) noexcept {
@ -158,7 +159,7 @@ namespace amt {
} // namespace amt } // namespace amt
#if 0
#include <format> #include <format>
namespace std { namespace std {
template <typename T> template <typename T>
@ -166,7 +167,7 @@ namespace std {
constexpr auto parse(format_parse_context& ctx) { constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); return ctx.begin();
} }
auto format(amt::Matrix<T> const& m, auto& ctx) const { auto format(amt::Matrix<T> const& m, auto& ctx) const {
std::string s = "[\n"; std::string s = "[\n";
for (auto r = std::size_t{}; r < m.rows(); ++r) { for (auto r = std::size_t{}; r < m.rows(); ++r) {
@ -174,11 +175,12 @@ namespace std {
s += std::format("{}, ", m(r, c)); s += std::format("{}, ", m(r, c));
} }
s += '\n'; s += '\n';
} }
s += "]"; s += "]";
return format_to(ctx.out(), "{}", s); return format_to(ctx.out(), "{}", s);
} }
}; };
} // namespace std } // namespace std
#endif
#endif // AMT_MATRIX_HPP

Loading…
Cancel
Save