Skip to content

Commit 62c2a81

Browse files
authored
SmallMatrix: Structured binding support (#4189)
1 parent e64ffef commit 62c2a81

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Src/Base/AMReX_SmallMatrix.H

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <algorithm>
1212
#include <initializer_list>
1313
#include <iostream>
14+
#include <tuple>
1415
#include <type_traits>
1516

1617
namespace amrex {
@@ -388,6 +389,14 @@ namespace amrex {
388389
return r;
389390
}
390391

392+
template <int N, std::enable_if_t<(N<NRows*NCols),int> = 0>
393+
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
394+
constexpr T const& get () const { return m_mat[N]; }
395+
396+
template <int N, std::enable_if_t<(N<NRows*NCols),int> = 0>
397+
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
398+
constexpr T& get () { return m_mat[N]; }
399+
391400
private:
392401
T m_mat[NRows*NCols];
393402
};
@@ -447,6 +456,16 @@ namespace amrex {
447456
using SmallRowVector = SmallMatrix<T,1,N,Order::F,StartIndex>;
448457
}
449458

459+
template <class T, int NRows, int NCols, amrex::Order ORDER, int StartIndex>
460+
struct std::tuple_size<amrex::SmallMatrix<T,NRows,NCols,ORDER,StartIndex> >
461+
: std::integral_constant<std::size_t,NRows*NCols> {};
462+
463+
template <std::size_t N, class T, int NRows, int NCols, amrex::Order ORDER, int StartIndex>
464+
struct std::tuple_element<N, amrex::SmallMatrix<T,NRows,NCols,ORDER,StartIndex> >
465+
{
466+
using type = T;
467+
};
468+
450469
#endif
451470

452471
/*

Tests/SmallMatrix/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ int main (int argc, char* argv[])
141141
b.setVal(-1);
142142
AMREX_ALWAYS_ASSERT(a.dot(b) == -30);
143143
}
144+
{
145+
SmallVector<int, 3> v{10,20,30};
146+
auto const& [x,y,z] = v;
147+
AMREX_ALWAYS_ASSERT(x == 10 && y == 20 && z == 30);
148+
149+
auto& [a,b,c] = v;
150+
a = 100; b = 200; c = 300;
151+
AMREX_ALWAYS_ASSERT(v[0] == 100 && v[1] == 200 && v[2] == 300);
152+
153+
auto const [i,j,k] = v;
154+
AMREX_ALWAYS_ASSERT(i == 100 && j == 200 && k == 300);
155+
156+
auto [d,e,f] = v;
157+
AMREX_ALWAYS_ASSERT(d == 100 && e == 200 && f == 300);
158+
}
144159

145160
// 1-based indexing
146161
{
@@ -271,5 +286,21 @@ int main (int argc, char* argv[])
271286
b.setVal(-1);
272287
AMREX_ALWAYS_ASSERT(a.dot(b) == -30);
273288
}
289+
{
290+
SmallVector<int, 3, 1> v{10,20,30};
291+
auto const& [x,y,z] = v;
292+
AMREX_ALWAYS_ASSERT(x == 10 && y == 20 && z == 30);
293+
294+
auto& [a,b,c] = v;
295+
a = 100; b = 200; c = 300;
296+
AMREX_ALWAYS_ASSERT(v[1] == 100 && v[2] == 200 && v[3] == 300);
297+
298+
auto const [i,j,k] = v;
299+
AMREX_ALWAYS_ASSERT(i == 100 && j == 200 && k == 300);
300+
301+
auto [d,e,f] = v;
302+
AMREX_ALWAYS_ASSERT(d == 100 && e == 200 && f == 300);
303+
}
304+
274305
amrex::Finalize();
275306
}

0 commit comments

Comments
 (0)