11#pragma once
22#include " macros.hpp"
3+ #include < initializer_list>
34
45
56/* *
@@ -26,34 +27,53 @@ struct CArrayWrapper;
2627template < typename T, int DIM0 >
2728struct CArrayWrapper < T, DIM0 >
2829{
30+
31+ constexpr CArrayWrapper () = default;
32+
33+ constexpr CArrayWrapper ( std::initializer_list< T > init )
34+ {
35+ int i = 0 ;
36+ for ( auto const & val : init )
37+ {
38+ data[i++] = val;
39+ }
40+ }
41+
42+ constexpr CArrayWrapper ( CArrayWrapper const & src )
43+ {
44+ for ( size_t i = 0 ; i < DIM0; i++ )
45+ {
46+ data[i] = src.data [i];
47+ }
48+ }
2949
3050 /* *
3151 * @brief Read/write access to an element by index.
3252 * @param dim The index (must be in range [0, DIM0)).
3353 * @return Reference to the element at the specified index.
3454 */
35- HPCREACT_HOST_DEVICE inline T & operator ()( int const dim ) { return data[dim]; }
55+ HPCREACT_HOST_DEVICE constexpr inline T & operator ()( int const dim ) { return data[dim]; }
3656
3757 /* *
3858 * @brief Read-only access to an element by index (const overload).
3959 * @param dim The index (must be in range [0, DIM0)).
4060 * @return Const reference to the element at the specified index.
4161 */
42- HPCREACT_HOST_DEVICE inline T const & operator ()( int const dim ) const { return data[dim]; }
62+ HPCREACT_HOST_DEVICE constexpr inline T const & operator ()( int const dim ) const { return data[dim]; }
4363
4464 /* *
4565 * @brief Subscript operator for read/write access.
4666 * @param dim The index (must be in range [0, DIM0)).
4767 * @return Reference to the element at the specified index.
4868 */
49- HPCREACT_HOST_DEVICE inline T & operator []( int const dim ) { return data[dim]; }
69+ HPCREACT_HOST_DEVICE constexpr inline T & operator []( int const dim ) { return data[dim]; }
5070
5171 /* *
5272 * @brief Subscript operator for read-only access (const overload).
5373 * @param dim The index (must be in range [0, DIM0)).
5474 * @return Const reference to the element at the specified index.
5575 */
56- HPCREACT_HOST_DEVICE inline T const & operator []( int const dim ) const { return data[dim]; }
76+ HPCREACT_HOST_DEVICE constexpr inline T const & operator []( int const dim ) const { return data[dim]; }
5777
5878 // / The underlying 1D C-style array.
5979 T data[DIM0];
@@ -72,13 +92,54 @@ struct CArrayWrapper< T, DIM0 >
7292template < typename T, int DIM0, int DIM1 >
7393struct CArrayWrapper < T, DIM0, DIM1 >
7494{
95+
96+ constexpr CArrayWrapper () = default;
97+
98+ constexpr CArrayWrapper ( CArrayWrapper const & src )
99+ {
100+ for ( size_t i = 0 ; i < DIM0; i++ )
101+ {
102+ for ( size_t j = 0 ; j < DIM1; j++)
103+ data[i][j] = src.data [i][j];
104+ }
105+ }
106+
107+ /* *
108+ * @brief Construct a 2D CArrayWrapper from nested initializer lists.
109+ *
110+ * Allows brace-initialization with a matrix-like structure:
111+ * @code
112+ * CArrayWrapper<double, 2, 3> mat = {
113+ * {1.0, 2.0, 3.0},
114+ * {4.0, 5.0, 6.0}
115+ * };
116+ * @endcode
117+ *
118+ * @param init A nested initializer list with exactly D0 rows and D1 elements per row.
119+ *
120+ * @note No runtime bounds checking is performed on the initializer dimensions.
121+ */
122+ constexpr CArrayWrapper ( std::initializer_list< std::initializer_list< T > > init )
123+ {
124+ int i = 0 ;
125+ for ( auto const & row : init )
126+ {
127+ int j = 0 ;
128+ for ( auto const & val : row )
129+ {
130+ data[i][j++] = val;
131+ }
132+ ++i;
133+ }
134+ }
135+
75136 /* *
76137 * @brief Read/write access to an element by 2D indices.
77138 * @param dim0 Index in the first dimension (range [0, DIM0)).
78139 * @param dim1 Index in the second dimension (range [0, DIM1)).
79140 * @return Reference to the element at the specified 2D location.
80141 */
81- HPCREACT_HOST_DEVICE inline T & operator ()( int const dim0, int const dim1 )
142+ HPCREACT_HOST_DEVICE constexpr inline T & operator ()( int const dim0, int const dim1 )
82143 {
83144 return data[dim0][dim1];
84145 }
@@ -89,7 +150,7 @@ struct CArrayWrapper< T, DIM0, DIM1 >
89150 * @param dim1 Index in the second dimension (range [0, DIM1)).
90151 * @return Const reference to the element at the specified 2D location.
91152 */
92- HPCREACT_HOST_DEVICE inline T const & operator ()( int const dim0, int const dim1 ) const
153+ HPCREACT_HOST_DEVICE constexpr inline T const & operator ()( int const dim0, int const dim1 ) const
93154 {
94155 return data[dim0][dim1];
95156 }
@@ -101,7 +162,7 @@ struct CArrayWrapper< T, DIM0, DIM1 >
101162 *
102163 * This allows usage like `obj[dim0][dim1]`.
103164 */
104- HPCREACT_HOST_DEVICE inline T ( & operator []( int const dim0 ))[DIM1]
165+ HPCREACT_HOST_DEVICE constexpr inline T ( & operator []( int const dim0 ))[DIM1]
105166 {
106167 return data[dim0];
107168 }
@@ -111,7 +172,7 @@ struct CArrayWrapper< T, DIM0, DIM1 >
111172 * @param dim0 The row index (range [0, DIM0)).
112173 * @return Const reference to an array of type T[DIM1].
113174 */
114- HPCREACT_HOST_DEVICE inline T const (&operator []( int const dim0 ) const )[DIM1]
175+ HPCREACT_HOST_DEVICE constexpr inline T const (&operator []( int const dim0 ) const )[DIM1]
115176 {
116177 return data[dim0];
117178 }
@@ -134,6 +195,51 @@ struct CArrayWrapper< T, DIM0, DIM1 >
134195template < typename T, int DIM0, int DIM1, int DIM2 >
135196struct CArrayWrapper < T, DIM0, DIM1, DIM2 >
136197{
198+
199+ constexpr CArrayWrapper () = default;
200+
201+ /* *
202+ * @brief Construct a 3D CArrayWrapper from nested initializer lists.
203+ *
204+ * Enables tensor-like initialization using triple-nested braces:
205+ * @code
206+ * CArrayWrapper<double, 2, 2, 2> cube = {
207+ * {
208+ * {1.0, 2.0},
209+ * {3.0, 4.0}
210+ * },
211+ * {
212+ * {5.0, 6.0},
213+ * {7.0, 8.0}
214+ * }
215+ * };
216+ * @endcode
217+ *
218+ * @param init A three-level nested initializer list with D0 planes, D1 rows per plane,
219+ * and D2 elements per row.
220+ *
221+ * @note This constructor does not perform size validation. Incorrect initializer sizes
222+ * may lead to undefined behavior.
223+ */
224+ constexpr CArrayWrapper ( std::initializer_list< std::initializer_list< std::initializer_list< T > > > init )
225+ {
226+ int i = 0 ;
227+ for ( auto const & plane : init )
228+ {
229+ int j = 0 ;
230+ for ( auto const & row : plane )
231+ {
232+ int k = 0 ;
233+ for ( auto const & val : row )
234+ {
235+ data[i][j][k++] = val;
236+ }
237+ ++j;
238+ }
239+ ++i;
240+ }
241+ }
242+
137243 /* *
138244 * @brief Read/write access to an element by 3D indices.
139245 * @param dim0 Index in the first dimension (range [0, DIM0)).
@@ -144,7 +250,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 >
144250 * @note Currently, this function incorrectly indexes data[dim0][dim1], missing dim2.
145251 * It should be `data[dim0][dim1][dim2]`. Please correct if intended.
146252 */
147- HPCREACT_HOST_DEVICE inline T & operator ()( int const dim0, int const dim1, int const dim2 )
253+ HPCREACT_HOST_DEVICE constexpr inline T & operator ()( int const dim0, int const dim1, int const dim2 )
148254 {
149255 // NOTE: This looks like a bug in your original code. Should be data[dim0][dim1][dim2].
150256 return data[dim0][dim1][dim2];
@@ -157,7 +263,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 >
157263 * @param dim2 Index in the third dimension (range [0, DIM2)).
158264 * @return Const reference to the element at the specified 3D location.
159265 */
160- HPCREACT_HOST_DEVICE inline T const & operator ()( int const dim0, int const dim1, int const dim2 ) const
266+ HPCREACT_HOST_DEVICE constexpr inline T const & operator ()( int const dim0, int const dim1, int const dim2 ) const
161267 {
162268 // NOTE: Same potential bug as above. Should be data[dim0][dim1][dim2].
163269 return data[dim0][dim1][dim2];
@@ -170,7 +276,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 >
170276 *
171277 * This allows usage like `obj[dim0][dim1][dim2]`.
172278 */
173- HPCREACT_HOST_DEVICE inline T ( & operator []( int const dim0 ))[DIM1][DIM2]
279+ HPCREACT_HOST_DEVICE constexpr inline T ( & operator []( int const dim0 ))[DIM1][DIM2]
174280 {
175281 return data[dim0];
176282 }
@@ -180,7 +286,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 >
180286 * @param dim0 The slice index (range [0, DIM0)).
181287 * @return Const reference to an array of type T[DIM1][DIM2].
182288 */
183- HPCREACT_HOST_DEVICE inline T const (&operator []( int const dim0 ) const )[DIM1][DIM2]
289+ HPCREACT_HOST_DEVICE constexpr inline T const (&operator []( int const dim0 ) const )[DIM1][DIM2]
184290 {
185291 return data[dim0];
186292 }
0 commit comments