Brought in Amit's latest and will now merge in my fixing from last night into his to get them synced up.

This commit is contained in:
Zed A. Shaw 2025-01-17 11:00:49 -05:00
parent adfb6367d7
commit c91e8fc543
8 changed files with 116 additions and 68 deletions

View file

@ -1,5 +1,4 @@
#ifndef AMT_MATRIX_HPP
#define AMT_MATRIX_HPP
#pragma once
#include <cassert>
#include <cstddef>
@ -10,6 +9,17 @@
namespace amt {
namespace detail {
[[nodiscard]] constexpr auto cal_index(
std::size_t r,
std::size_t c,
[[maybe_unused]] std::size_t rs,
[[maybe_unused]] std::size_t cs
) -> std::size_t {
return r * cs + c;
}
}
template <typename T>
struct Matrix {
using value_type = T;
@ -28,16 +38,20 @@ namespace amt {
struct View {
using base_type = std::conditional_t<IsConst, const_pointer, pointer>;
base_type data;
size_type size;
size_type r;
size_type rows;
size_type cols;
constexpr reference operator[](size_type k) noexcept requires (!IsConst) {
assert(k < size && "Out of bound access");
return data[k];
constexpr reference operator[](size_type c) noexcept requires (!IsConst) {
assert(c < cols && "Out of bound access");
auto const index = detail::cal_index(r, c, rows, cols);
return data[index];
}
constexpr const_reference operator[](size_type k) const noexcept {
assert(k < size && "Out of bound access");
return data[k];
constexpr const_reference operator[](size_type c) const noexcept {
assert(c < cols && "Out of bound access");
auto const index = detail::cal_index(r, c, rows, cols);
return data[index];
}
};
@ -83,7 +97,7 @@ namespace amt {
}
Matrix(std::initializer_list<std::initializer_list<value_type>> li)
: m_row(li.size())
: m_row(li.size())
{
for (auto const& row: li) {
m_col = std::max(m_col, row.size());
@ -120,28 +134,26 @@ 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 + c * m_row; // row-major;
constexpr auto operator()(size_type r, size_type c) noexcept -> reference {
auto const index = detail::cal_index(r, c, rows(), cols());
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 + c * m_row; // row-major;
auto const index = detail::cal_index(r, c, rows(), cols());
assert(index < size() && "Out of bound access");
return m_data[index];
}
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) noexcept -> View<false> {
assert(r < rows() && "Out of bound access");
return { .data = m_data, .r = r, .rows = m_row, .cols = 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 };
constexpr auto operator[](size_type r) const noexcept -> View<true> {
assert(r < rows() && "Out of bound access");
return { .data = m_data, .r = r, .rows = m_row, .cols = m_col };
}
friend void swap(Matrix& lhs, Matrix& rhs) noexcept {
@ -159,7 +171,7 @@ namespace amt {
} // namespace amt
#if 0
#include <format>
namespace std {
template <typename T>
@ -167,7 +179,7 @@ namespace std {
constexpr auto parse(format_parse_context& ctx) {
return ctx.begin();
}
auto format(amt::Matrix<T> const& m, auto& ctx) const {
std::string s = "[\n";
for (auto r = std::size_t{}; r < m.rows(); ++r) {
@ -175,12 +187,11 @@ namespace std {
s += std::format("{}, ", m(r, c));
}
s += '\n';
}
}
s += "]";
return format_to(ctx.out(), "{}", s);
}
};
} // namespace std
#endif // AMT_MATRIX_HPP
#endif