1616
1717namespace PHARE ::core
1818{
19- template <std::size_t dim, bool c_ordering = true , typename DataType = double >
19+ template <std::size_t dim, bool c_ordering = true >
2020struct NdArrayViewer
2121{
22- template <typename NCells, typename ... Indexes>
23- NO_DISCARD static DataType const & at (DataType const * data, NCells const & nCells,
24- Indexes const &... indexes)
25- {
26- auto params = std::forward_as_tuple (indexes...);
27- static_assert (sizeof ...(Indexes) == dim);
28- // static_assert((... && std::is_unsigned_v<decltype(indexes)>)); TODO : manage later if
29- // this test should be included
22+ using Id = std::uint16_t ;
23+ using Idx = Id const ;
3024
25+ template <typename NCells, template <typename , std::size_t > typename Indexes, typename Index>
26+ static inline std::uint32_t idx (NCells const & nCells, Indexes<Index, dim> const & indexes)
27+ {
3128 if constexpr (dim == 1 )
32- {
33- auto i = std::get<0 >(params);
29+ return idx (nCells, indexes[0 ]);
3430
35- return data[i];
36- }
31+ else if constexpr (dim == 2 )
32+ return idx (nCells, indexes[ 0 ], indexes[ 1 ]);
3733
38- if constexpr (dim == 2 )
39- {
40- auto i = std::get<0 >(params);
41- auto j = std::get<1 >(params);
34+ else if constexpr (dim == 3 )
35+ return idx (nCells, indexes[0 ], indexes[1 ], indexes[2 ]);
36+ }
4237
43- if constexpr (c_ordering)
44- return data[j + i * nCells[1 ]];
45- else
46- return data[i + j * nCells[0 ]];
47- }
38+ static inline std::uint32_t idx (auto const /* nCells*/ , Idx i) { return i; }
4839
49- if constexpr (dim == 3 )
50- {
51- auto i = std::get<0 >(params);
52- auto j = std::get<1 >(params);
53- auto k = std::get<2 >(params);
54-
55- if constexpr (c_ordering)
56- return data[k + j * nCells[2 ] + i * nCells[1 ] * nCells[2 ]];
57- else
58- return data[i + j * nCells[0 ] + k * nCells[1 ] * nCells[0 ]];
59- }
40+ static inline std::uint32_t idx (auto const nCells, Idx i, Idx j)
41+ {
42+ if constexpr (c_ordering)
43+ return j + i * nCells[1 ];
44+ else
45+ return i + j * nCells[0 ];
46+ }
47+ static inline std::uint32_t idx (auto const nCells, Idx i, Idx j, Idx k)
48+ {
49+ if constexpr (c_ordering)
50+ return k + j * nCells[2 ] + i * nCells[1 ] * nCells[2 ];
51+ else
52+ return i + j * nCells[0 ] + k * nCells[1 ] * nCells[0 ];
6053 }
6154
62- template <typename NCells, template <typename , std::size_t > typename Indexes, typename Index>
63- NO_DISCARD static DataType const & at (DataType const * data, NCells const & nCells,
64- Indexes<Index, dim> const & indexes)
55+ template <template <typename , std::size_t > typename Indexes, typename Index>
56+ NO_DISCARD static inline auto & at (auto * data, auto const & nCells,
57+ Indexes<Index, dim> const & indexes)
6558
6659 {
67- if constexpr (dim == 1 )
68- return at (data, nCells, indexes[0 ]);
69-
70- else if constexpr (dim == 2 )
71- return at (data, nCells, indexes[0 ], indexes[1 ]);
60+ auto const & i = idx (nCells, indexes);
61+ assert (i < product (nCells, std::uint32_t {1 }));
62+ return data[i];
63+ }
7264
73- else if constexpr (dim == 3 )
74- return at (data, nCells, indexes[0 ], indexes[1 ], indexes[2 ]);
65+ static inline auto & at (auto * data, auto const nCells, auto const ... indexes)
66+ {
67+ auto const & i = idx (nCells, indexes...);
68+ assert (i < product (nCells, std::uint32_t {1 }));
69+ return data[i];
7570 }
7671};
7772
7873
7974
75+
8076template <typename Array, typename Mask>
8177class MaskedView
8278{
@@ -102,7 +98,7 @@ class MaskedView
10298 template <typename ... Indexes>
10399 NO_DISCARD DataType const & operator ()(Indexes... indexes) const
104100 {
105- return NdArrayViewer<dimension, true , DataType >::at (array_.data (), shape_, indexes...);
101+ return NdArrayViewer<dimension, true >::at (array_.data (), shape_, indexes...);
106102 }
107103
108104 template <typename ... Indexes>
@@ -139,7 +135,7 @@ class NdArrayView
139135 static std::size_t const dimension = dim;
140136 using type = DataType;
141137 using pointer_type = DataType*;
142- using viewer = NdArrayViewer<dim, c_ordering, DataType >;
138+ using viewer = NdArrayViewer<dim, c_ordering>;
143139
144140 explicit NdArrayView (pointer_type ptr, std::array<std::uint32_t , dim> const & nCells)
145141 : ptr_{ptr}
@@ -272,7 +268,7 @@ class NdArrayVector
272268 template <typename ... Indexes>
273269 NO_DISCARD DataType const & operator ()(Indexes... indexes) const
274270 {
275- return NdArrayViewer<dim, c_ordering, DataType >::at (data_.data (), nCells_, indexes...);
271+ return NdArrayViewer<dim, c_ordering>::at (data_.data (), nCells_, indexes...);
276272 }
277273
278274 template <typename ... Indexes>
@@ -284,7 +280,7 @@ class NdArrayVector
284280 template <typename Index>
285281 NO_DISCARD DataType const & operator ()(std::array<Index, dim> const & indexes) const
286282 {
287- return NdArrayViewer<dim, c_ordering, DataType >::at (data_.data (), nCells_, indexes);
283+ return NdArrayViewer<dim, c_ordering>::at (data_.data (), nCells_, indexes);
288284 }
289285
290286 template <typename Index>
0 commit comments