C ++中的变维动态数组

时间:2017-11-19 01:31:05

标签: c++ multidimensional-array tensor

我想在C ++中实现一个类似STL的张量模板,它需要重载operator []来访问它的元素,这可能是这样的:

Tensor<double,2> tensor2
{
    {1,2,3},
    {4,5,6}
};
std::cout<< tensor2[1][2]<<'\n';

对于低维度,可能很容易使用像

这样的东西
std::vector<std::vector<int>>

但这对于更高的尺寸来说并不容易。 另外,我想在张量之间实现产品,并在一个张量中实现尺寸之间的收缩。

1 个答案:

答案 0 :(得分:0)

我用一些非常基本的操作实现了多维数组。由于Boost.MultiArray提供了更好的解决方案,我的实现仅用于演示。

namespace DataStructures
{
    struct ExtentList   //Discribe extents of dimensions for MultiArray
    {
        std::vector<size_t> dimensions;
        ExtentList() = default;
        template<typename Iterator, typename SFINAE = std::enable_if_t<std::_Is_iterator_v<Iterator>>>
        ExtentList(Iterator begin, Iterator end) : dimensions(begin, end) {}
        //operator[] used to initialize the extents
        ExtentList& operator[](size_t n) { dimensions.push_back(n); return *this; }
        ExtentList after_front() const { return ExtentList(++dimensions.cbegin(), dimensions.cend()); }
        //at() used to access extents
        size_t at(size_t n) const { return dimensions.at(n); }
    };

    static ExtentList Extents;

    template<
        typename ElemType,                                                  //Underlying Type
        size_t Dimension,                                                   //Dimension of MultiArray
        typename ElementAllocator = std::allocator<ElemType>,               //Allocator for elements
        template<typename, typename> typename ContainerType = std::vector,  //Underlying container type
        template<typename> typename ContainerAllocator = std::allocator>    //Allocator for container
    class MultiArray
    {
        //Necessary for contructor with ExtentList
        friend class MultiArray<ElemType, Dimension + 1U, ElementAllocator, ContainerType, ContainerAllocator>;

        using value_type = typename
            std::conditional_t<
            Dimension == 1U, 
            ElemType,
            MultiArray<ElemType, Dimension - 1U, ElementAllocator, ContainerType, ContainerAllocator>>;
        using allocator_type = typename
            std::conditional_t<
            Dimension == 1U,
            ElementAllocator,
            ContainerAllocator<value_type>>;
        ContainerType<value_type, allocator_type> data;


    public:
        MultiArray() = default;
        MultiArray(size_t n, const value_type& val) : data(n, val) {}

        template<typename SFINAE = std::enable_if_t<(Dimension == 1U)>>
            MultiArray(ExtentList extents, const ElemType& elem) : data(extents.at(0), elem) {}
        template<typename SFINAE = std::enable_if_t<(Dimension >= 2U)>, typename SFINAE2 = SFINAE>
            MultiArray(ExtentList extents, const ElemType& elem) : data(extents.at(0), value_type(extents.after_front(), elem)) {}

        MultiArray(std::initializer_list<value_type> ilist) : data(ilist) {}


        template<typename ... SizeType>
        MultiArray(size_t N, SizeType... args) : data(N, value_type(args...)) {}

        value_type& operator[](size_t n) { return data[n]; }
        void push_back(const value_type& elem) { data.push_back(elem); }
    };
}

这个想法是用递归模板实现多维数组,因此允许订阅和列表初始化操作。

namespace DS = DataStructures;

DS::MultiArray<int, 2> matrix_2d
{
    { 1,2,3 },
    { 4,5,6 },
    { 7,8,9 }
};
for (size_t i = 0; i != 3; ++i)
    for (size_t j = 0; j != 3; ++j)
        std::cout << matrix_2d[i][j] << ' ';

DS::MultiArray<int, 3> matrix_3d(DS::Extents[10][10][10], 0);
size_t sum = 0;
for (size_t i = 0; i != DS::Extents.at(0); ++i)
    for (size_t j = 0; j != DS::Extents.at(1); ++j)
        for (size_t k = 0; k != DS::Extents.at(2); ++k)
            sum += (matrix_3d[i][j][k] = i * 100 + j * 10 + k);
std::cout << sum << '\n' << matrix_3d[9][9][9] << '\n';

ExtentList的想法来自Boost。它比变量函数参数列表或变量模板更好。