From 62c2a81eac7862d526e5861ef2befc00b7f5b759 Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Fri, 11 Oct 2024 20:10:51 -0500 Subject: [PATCH] SmallMatrix: Structured binding support (#4189) --- Src/Base/AMReX_SmallMatrix.H | 19 +++++++++++++++++++ Tests/SmallMatrix/main.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/Src/Base/AMReX_SmallMatrix.H b/Src/Base/AMReX_SmallMatrix.H index 4fe673816d..05305d1839 100644 --- a/Src/Base/AMReX_SmallMatrix.H +++ b/Src/Base/AMReX_SmallMatrix.H @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace amrex { @@ -388,6 +389,14 @@ namespace amrex { return r; } + template = 0> + [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE + constexpr T const& get () const { return m_mat[N]; } + + template = 0> + [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE + constexpr T& get () { return m_mat[N]; } + private: T m_mat[NRows*NCols]; }; @@ -447,6 +456,16 @@ namespace amrex { using SmallRowVector = SmallMatrix; } +template +struct std::tuple_size > + : std::integral_constant {}; + +template +struct std::tuple_element > +{ + using type = T; +}; + #endif /* diff --git a/Tests/SmallMatrix/main.cpp b/Tests/SmallMatrix/main.cpp index de5732101f..505ae50bd6 100644 --- a/Tests/SmallMatrix/main.cpp +++ b/Tests/SmallMatrix/main.cpp @@ -141,6 +141,21 @@ int main (int argc, char* argv[]) b.setVal(-1); AMREX_ALWAYS_ASSERT(a.dot(b) == -30); } + { + SmallVector v{10,20,30}; + auto const& [x,y,z] = v; + AMREX_ALWAYS_ASSERT(x == 10 && y == 20 && z == 30); + + auto& [a,b,c] = v; + a = 100; b = 200; c = 300; + AMREX_ALWAYS_ASSERT(v[0] == 100 && v[1] == 200 && v[2] == 300); + + auto const [i,j,k] = v; + AMREX_ALWAYS_ASSERT(i == 100 && j == 200 && k == 300); + + auto [d,e,f] = v; + AMREX_ALWAYS_ASSERT(d == 100 && e == 200 && f == 300); + } // 1-based indexing { @@ -271,5 +286,21 @@ int main (int argc, char* argv[]) b.setVal(-1); AMREX_ALWAYS_ASSERT(a.dot(b) == -30); } + { + SmallVector v{10,20,30}; + auto const& [x,y,z] = v; + AMREX_ALWAYS_ASSERT(x == 10 && y == 20 && z == 30); + + auto& [a,b,c] = v; + a = 100; b = 200; c = 300; + AMREX_ALWAYS_ASSERT(v[1] == 100 && v[2] == 200 && v[3] == 300); + + auto const [i,j,k] = v; + AMREX_ALWAYS_ASSERT(i == 100 && j == 200 && k == 300); + + auto [d,e,f] = v; + AMREX_ALWAYS_ASSERT(d == 100 && e == 200 && f == 300); + } + amrex::Finalize(); }