Reputation: 161
I would like to typedef
a multi-dimensional array using the boost library. The matrix is rectangular and the dimension and lengths in each dimension should be fixed within the typedef
. In case of a vector this was exactly what is solved by the boost::array
template class:
typedef boost::array<int, 3> vec3_t;
For multi-dimensional arrays I could repeat this construction
typedef boost::array<boost::array<int, 2> ,3> mat2x3_t
and it allows me to also initialize my matrix quite nicely (although I'm not sure if this behavior is guaranteed...):
Mat2x3_t m = { 11, 12, 13,
21, 22, 23};
However, from a performance standpoint I am unsure whether this is to way to go. My understanding of memory management is very limited but the way I imagine this in the computer memory is, that the inner-most array (boost::array<int, 2>
in the above example) is guaranteed to allocate a contiguous block of memory, whereas, for the "higher levels" of arrays I'n not sure.
I also looked at the boost::multi_array
class, where, to my understanding, the whole matrix will take up a contiguous block of memory and is quite ideal for my "rectangular" matrices that I'm interested in. It further supplies some neat member functions (which are not crucial for my implementation but are a nice addition nonetheless). The deal breaker for this class seems to be, however, that I cannot typedef
the length in each dimension but only the number of dimensions.
So my question is: Does the the construction in terms of multiple arrays result in performance issues when I have to iterate through it a lot (~10^7 times)? In my example I used a 2x3 int matrix but my use-case would be a higher-dimensional 64x4x2 matrix with complex numbers. And, is the initialization in terms of the curly braces guaranteed by the implementation? If there are issues with the arrays, is there a way to typedef a fixed size multi_array? Or are the other alternatives I am missing?
Upvotes: 1
Views: 853
Reputation: 47498
boost::array<T, N>
(just like its modern cousin, std::array<T, N>
) is defined to have just one data member:
// public data members
T elems[N];
which is a contiguous sequence of Ts. If each T is an array in turn, the whole thing is contiguous (technically, there could have been gaps due to padding before or after elems
, but leading padding is not allowed in this case, and trailing padding is unrealistic.
Brace initialization and brace elision are guaranteed, although some compilers like to emit warnings if they think you don't have enough braces.
Upvotes: 2