array icon indicating copy to clipboard operation
array copied to clipboard

Alias for a fixed size array allocated on the stack

Open fbleibel-g opened this issue 2 years ago • 3 comments

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?

fbleibel-g avatar Jun 08 '22 16:06 fbleibel-g

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

jiawen avatar Jun 08 '22 16:06 jiawen

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.

fbleibel-g avatar Jun 08 '22 16:06 fbleibel-g

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!

jiawen avatar Jun 08 '22 16:06 jiawen

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).

dsharlet avatar Sep 03 '22 06:09 dsharlet

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!

fbleibel-g avatar Sep 06 '22 16:09 fbleibel-g

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

dsharlet avatar Sep 06 '22 19:09 dsharlet