dsharlet / array

C++ multidimensional arrays in the spirit of the STL
Apache License 2.0
198 stars 15 forks source link

Alias for a fixed size array allocated on the stack #65

Closed fbleibel-g closed 2 years ago

fbleibel-g commented 2 years ago

I've recently had use for a fixed size array allocated on the stack, i.e. the n-dimensional counterpart to std::array or the equivalent of Eigen::Matrix<T, Rows, Cols>: this is an array with.

template <typename T, size_t... Extents>
using FixedSizeArray =
    nda::array<T, typename CompactShapeHelper<Extents...>::Shape,
               nda::auto_allocator<T, (... * Extents)>>;

Where Shape defines each dim with completely static values: min = 0, extent = Extent, Stride = (the product of all subsequent extents), that is row-major for a 2D matrix. I guess Stride doesn't have to make the array compact for all use cases, but this can help with naive indexing (offset = h w i + w * j + k).

Do you think it would be interesting to have such a type defined in array.h?

jiawen commented 2 years ago

It's already defined in matrix.h as small_matrix.

fbleibel-g commented 2 years ago

Thanks, that looks like the right header. I'm looking for the N-dimensional generalization of this - something I can use to define 3D and > arrays.

jiawen commented 2 years ago

Gah, I clearly should have read the request more carefully. Now I'm really curious what use you have for a fixed size high dimensional array!

dsharlet commented 2 years ago

I was thinking about this a bit. It's probably a good idea to add a fixed_dense_shape type (see #74). This is tricky enough to implement that we should provide a wrapper.

I think adding an alias for array is more questionable though, and not that hard for user code to do with fixed_dense_shape, since it's a one liner with that (replaces your CompactShapeHelper), and doesn't require any big leaps of template programming. It also still requires some decisions from the user (e.g. initialized vs. not).

fbleibel-g commented 2 years ago

Thanks, the CompactShapeHelper replacement was most of the code, and I can remove our own implementation of that.

Subsidiary question: do you think matrix_shape and vector_shape from image.h should now also be defined in terms of fixed_dense_shape? This can provide a 'guide' for users that are looking to implement the same.

Thanks again!

dsharlet commented 2 years ago

matrix_shape has the dense dimension as the second dimension, so it wouldn't be the same thing...