创建一个用元组0,1,2,3初始化的多维数组,

时间:2016-04-03 17:45:35

标签: c++ templates multidimensional-array c++14 variadic-templates

函数createMultiArray<M,N>()创建

`std::array<std::array<std::tuple<std::size_t, std::size_t>, M>, N>`,

其元素是:

(0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3)

以下是我的简单实现:

#include <iostream>
#include <tuple>
#include <utility>

template <std::size_t M, std::size_t N>
struct InitializeMultiArray {
    using Array = std::array<std::array<std::tuple<std::size_t, std::size_t>, M>, N>;
    template <std::size_t... Is>
    static Array execute (std::index_sequence<Is...>) {
        Array array;
        const int a[] = {(initialize<Is>(array, std::make_index_sequence<M>{}), 0)...};
        static_cast<void>(a);
        return array;
    }
private:
    template <std::size_t I, std::size_t... Is>
    static void initialize (Array& array, std::index_sequence<Is...>) {
        const int a[] = {(array[I][Is] = std::make_tuple(I, Is), 0)...};
        static_cast<void>(a);
    }
};

template <std::size_t M, std::size_t N>
std::array<std::array<std::tuple<std::size_t, std::size_t>, M>, N> createMultiArray() {
    return InitializeMultiArray<M,N>::execute(std::make_index_sequence<N>{});
}


int main() {
    constexpr std::size_t M = 4, N = 3;
    const std::array<std::array<std::tuple<std::size_t, std::size_t>, M>, N> array = createMultiArray<M,N>();
    for (std::size_t i = 0; i < N; i++) {
        for (std::size_t j = 0; j < M; j++)
            std::cout << "(" << std::get<0>(array[i][j]) << ", " << std::get<1>(array[i][j]) << ") ";
            std::cout << '\n'; 
    }
}

现在我需要将createMultiArray<M,N>()扩展到createMultiArray<Dimensions...>()到任意数量的维度,例如array[i][j][k]...[last] = std::make_tuple(i,j,k,...,last)。我坚持如何进行这种概括。任何人都可以帮忙吗?

这是存储在多维数组中的精确元组类型:

template <std::size_t N>
using Type = std::size_t;

template <typename> struct TupleOfIntsHelper;

template <std::size_t... Is>
struct TupleOfIntsHelper<std::index_sequence<Is...>> {
    using type = std::tuple<Type<Is>...>;
};

template <std::size_t N>
using TupleOfInts = typename TupleOfIntsHelper<std::make_index_sequence<N>>::type;

// ...

static_assert (std::is_same<TupleOfInts<3>, std::tuple<std::size_t, std::size_t, std::size_t>>::value, "");

然后返回类型createMultiArray<Dimensions...>()

    typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type

,其中

template <typename, std::size_t...> struct NArray;

template <typename T, std::size_t N>
struct NArray<T,N> {
    using type = std::array<T,N>;
};

template <typename T, std::size_t First, std::size_t... Rest>
struct NArray<T, First, Rest...> {
    using type = std::array<typename NArray<T, Rest...>::type, First>;
};

所以唯一困难的任务是如上所述初始化它:

template <std::size_t... Dimensions>
typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type createMultiArray() {
    typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type array;
    // ???
    return array;
}

更新:以下是我的想法:

template <typename... IndexSequences>
struct AllCombinations {
    using type = std::tuple<std::index_sequence<0,0,0>, std::index_sequence<0,0,1>>;  // etc...
    // Generate these based on IndexSequences...
};

template <typename Combinations, typename Array>
void initialize (Array& array) {
// Use each type in Combinations to initialize 'array' via a function like
// void initialize_impl(Array& array, std::index_sequence<Is...>) {
//      get_array_element(array, {Is...}) = std::make_tuple(Is...);
// }
}

template <std::size_t... Dimensions>
typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type createMultiArray() {
    typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type array;
    using Combinations = typename AllCombinations<std::make_index_sequence<Dimensions>...>::type;
    initialize<Combinations>(array);
    return array;
}

这里是我上面提到的initialize_impl函数:

template <std::size_t I>
struct MultiArrayGet {
    template <typename Array, std::size_t N>
    static auto& get (Array& a, const std::array<std::size_t, N>& index) {
        return MultiArrayGet<I - 1>::get(a[index[N - I]], index);  // Here I is just a counter so that we know when to stop.
    }
};

template <>
struct MultiArrayGet<0> {
    template <typename T, std::size_t N>
    static auto& get (T& t, const std::array<std::size_t, N>&) { return t; }
};

template <std::size_t N, typename Array>
auto& get_array_element (Array& a, const std::array<std::size_t, N>& index) {
    return MultiArrayGet<N>::get(a, index);
}

template <typename Array, std::size_t... Is>
void initialize_impl (Array& array, std::index_sequence<Is...>) {
    get_array_element<sizeof...(Is)>(array, {Is...}) = std::make_tuple(Is...);
}

3 个答案:

答案 0 :(得分:3)

让我们不要让事情变得不必要地复杂化。从概念上讲,您要编写的初始化只是一堆嵌套的for循环:

for(std::size_t i = 0; i < Dim0; ++i) 
    for(std::size_t j = 0; j < Dim1; ++j) 
        for(std::size_t k = 0; k < Dim2; ++k)
             // ...
                 for(std::size_t last = 0; last < DimN; ++last)
                     array[i][j][k]...[last] = std::make_tuple(i,j,k,...,last);

让我们这样做。这是一个简单的递归。

namespace details {
    template<class... Ts, class... Args>
    void init_array(std::tuple<Ts...>& tup, Args... args) {
        static_assert(sizeof...(Ts) == sizeof...(args), "Oops");
        tup = std::make_tuple(args...);
    }

    template<class Array, class... Args>
    void init_array(Array& arr, Args... args) {
        for(std::size_t i = 0; i < arr.size(); ++i){
            init_array(arr[i], args..., i);
        }
    }
}

template <std::size_t... Dimensions>
typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type createMultiArray() {
    typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type array;
    details::init_array(array);
    return array;
}

这可以是C ++ 17中的constexpr

对于那些真正想要C ++ 14 constexpr的人来说,这并不难。我们无法在创建数组后将其编入索引,因此需要在初始化时完成。

namespace details {
    // create an std::array out of the provided elements
    template<class... Ts>
    constexpr std::array<std::common_type_t<Ts...>, sizeof...(Ts)> make_array(Ts&&... ts) {
        return { { std::forward<Ts>(ts)... } };
    }

    // terminating case just creates a tuple.
    template<std::size_t... Dimensions, class... Ts>
    constexpr auto createMultiArrayHelper(std::index_sequence<Dimensions...>, 
                                          std::index_sequence<>, Ts... vals){
        static_assert(sizeof...(Dimensions) == sizeof...(vals), "Oops");
        return std::make_tuple(vals...);
    }
    template<std::size_t... Dimensions, std::size_t... Is, class... Ts>
    constexpr auto createMultiArrayHelper(std::index_sequence<Dimensions...>,
                                          std::index_sequence<Is...>, Ts... vals){
        constexpr std::size_t dims[] = {Dimensions..., 0}; // 0 for the terminating case
        constexpr auto next_dim = dims[sizeof...(vals) + 1];
        return make_array(createMultiArrayHelper(std::index_sequence<Dimensions...>(), 
                              std::make_index_sequence<next_dim>(), vals..., Is)...);
    }
}

template<std::size_t... Dimensions>
constexpr auto createMultiArray(){
    constexpr std::size_t dims[] = {Dimensions...};
    return details::createMultiArrayHelper(std::index_sequence<Dimensions...>(),
                                           std::make_index_sequence<dims[0]>());
}

制作这个C ++ 11 constexpr留给读者练习。

答案 1 :(得分:2)

我相信T.C.的C ++ 14 constexpr解决方案尽可能缩短,并尽可能地进行优化(因为传递的参数较少):

#include <iostream>
#include <tuple>
#include <utility>

template <typename... Ts>
constexpr std::array<std::common_type_t<Ts...>, sizeof...(Ts)> makeArray (Ts&&... ts) {
    return { {std::forward<Ts>(ts)...} };
}

template <typename... Ts>
constexpr auto createMultiArrayHelper (std::index_sequence<>, Ts... vals) {
    return std::make_tuple(vals...);
}

template <std::size_t First, std::size_t... Rest, std::size_t... Is, typename... Ts>
constexpr auto createMultiArrayHelper (std::index_sequence<Is...>, Ts... vals) {
    return makeArray (createMultiArrayHelper<Rest...>(std::make_index_sequence<First>{}, vals..., Is)...);
}

template <std::size_t First, std::size_t... Rest>
constexpr auto createMultiArray() {
    return createMultiArrayHelper<Rest..., 0>(std::make_index_sequence<First>{});
}

// Testing
int main() {
    constexpr std::size_t M = 3, N = 2, P = 4;
    constexpr auto array = createMultiArray<M,N,P>();
    for (std::size_t i = 0; i < M; i++) {
        for (std::size_t j = 0; j < N; j++) {
            for (std::size_t k = 0; k < P; k++)
                std::cout << "(" << std::get<0>(array[i][j][k]) << ", " << std::get<1>(array[i][j][k]) << ", " << std::get<2>(array[i][j][k]) << ") ";
                std::cout << '\n';
        }
    }
}

输出:

(0, 0, 0) (0, 0, 1) (0, 0, 2) (0, 0, 3)
(0, 1, 0) (0, 1, 1) (0, 1, 2) (0, 1, 3)
(1, 0, 0) (1, 0, 1) (1, 0, 2) (1, 0, 3)
(1, 1, 0) (1, 1, 1) (1, 1, 2) (1, 1, 3)
(2, 0, 0) (2, 0, 1) (2, 0, 2) (2, 0, 3)
(2, 1, 0) (2, 1, 1) (2, 1, 2) (2, 1, 3)

由于此处的每个constexpr函数都已包含单个返回行,因此它实际上已经是一个C ++ 11解决方案(只需要定义std :: index_sequence并添加尾随的decltype返回类型)。

答案 2 :(得分:0)

好的,我认为我有一个有效的解决方案。我明天会尝试更好地改进它。使用std::index_sequence实际上导致了我烦人的错误,因为它只是std::integer_sequence<std::size_t, Is...>的别名,不适用于我的ExpandPacks类(我会尝试将它们放回以后),所以我从第一原则定义sequencemake_sequence并改为使用它们。

#include <iostream>
#include <tuple>
#include <utility>

template <std::size_t...> struct sequence {};

template <std::size_t N, std::size_t... Is>
struct make_sequence_helper : make_sequence_helper<N-1, N-1, Is...> {};

template <std::size_t... Is>
struct make_sequence_helper<0, Is...> {
    using type = sequence<Is...>;
};

template <std::size_t N>
using make_sequence = typename make_sequence_helper<N>::type;

template <typename, std::size_t... Dimensions> struct NArray;

template <typename T, std::size_t N>
struct NArray<T,N> {
    using type = std::array<T,N>;
};

template <typename T, std::size_t First, std::size_t... Rest>
struct NArray<T, First, Rest...> {
    using type = std::array<typename NArray<T, Rest...>::type, First>;
};

template <std::size_t N>
using Type = std::size_t;

template <typename> struct TupleOfIntsHelper;

template <std::size_t... Is>
struct TupleOfIntsHelper<std::index_sequence<Is...>> {
    using type = std::tuple<Type<Is>...>;
};

template <std::size_t N>
using TupleOfInts = typename TupleOfIntsHelper<std::make_index_sequence<N>>::type;

template <std::size_t I, typename Pack> struct Prepend;
template <typename...> struct Merge;

template <std::size_t I, template <std::size_t...> class P, std::size_t... Is>
struct Prepend<I, P<Is...>> {
    using type = P<I, Is...>;
};

template <typename Pack>
struct Merge<Pack> {
    using type = Pack;
};

template <template <typename...> class P, typename... Ts, typename... Us>
struct Merge<P<Ts...>, P<Us...>> {
    using type = P<Ts..., Us...>;
};

template <typename First, typename... Rest>
struct Merge<First, Rest...> : Merge<First, typename Merge<Rest...>::type> {};

template <typename... Packs> struct ExpandPacks;
template <std::size_t I, typename Pack> struct PairEach;
template <std::size_t I, typename PackOfPacks> struct PrependEach;
template <typename Pack, typename PackOfPacks> struct ExpandPacksHelper;

template <template <std::size_t...> class P, std::size_t I, std::size_t... Is>
struct PairEach<I, P<Is...>> {
    using type = std::tuple<P<I, Is>...>;
};

template <std::size_t I, typename... Packs>
struct PrependEach<I, std::tuple<Packs...>> {
    using type = std::tuple<typename Prepend<I, Packs>::type...>;
};

template <template <std::size_t...> class P, std::size_t... Is, typename... Packs>
struct ExpandPacksHelper<P<Is...>, std::tuple<Packs...>> : Merge<typename PrependEach<Is, std::tuple<Packs...>>::type...> {};

template <template <std::size_t...> class P, std::size_t... Is, typename Pack>
struct ExpandPacks<P<Is...>, Pack> : Merge<typename PairEach<Is, Pack>::type...> {};

template <typename First, typename... Rest>
struct ExpandPacks<First, Rest...> : ExpandPacksHelper<First, typename ExpandPacks<Rest...>::type> {};

template <std::size_t I>
struct MultiArrayGet {
    template <typename Array, std::size_t N>
    static auto& get (Array& a, const std::array<std::size_t, N>& index) {
        return MultiArrayGet<I - 1>::get(a[index[N - I]], index);  // Here I is just a counter so that we know when to stop.
    }
};

template <>
struct MultiArrayGet<0> {
    template <typename T, std::size_t N>
    static auto& get (T& t, const std::array<std::size_t, N>&) { return t; }
};

template <std::size_t N, typename Array>
auto& get_array_element (Array& a, const std::array<std::size_t, N>& index) {
    return MultiArrayGet<N>::get(a, index);
}

template <typename Array, std::size_t... Is>
void initialize_impl (Array& array, sequence<Is...>) {
    get_array_element<sizeof...(Is)>(array, {Is...}) = std::make_tuple(Is...);
}

template <typename Combinations> struct Initialize;

template <template <typename...> class P>
struct Initialize<P<>> {
    template <typename Array>
    static void execute (Array&) {}  // End of recursion.
};

template <template <typename...> class P, typename First, typename... Rest>
struct Initialize<P<First, Rest...>> {
    template <typename Array>
    static void execute (Array& array) {
        initialize_impl (array, First{});
        Initialize<P<Rest...>>::execute(array);
    }
};

template <std::size_t... Dimensions>
typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type createMultiArray() {
    typename NArray<TupleOfInts<sizeof...(Dimensions)>, Dimensions...>::type array;
    using Combinations = typename ExpandPacks<make_sequence<Dimensions>...>::type;
    Initialize<Combinations>::execute(array);
    return array;
}

int main() {    
    constexpr std::size_t M = 3, N = 2, P = 4;
    const auto array = createMultiArray<M,N,P>();  // 'auto' is std::array<std::array<std::array<std::tuple<std::size_t, std::size_t, std::size_t>, P>, N>, M>.
    for (std::size_t i = 0; i < M; i++) {
        for (std::size_t j = 0; j < N; j++) {
            for (std::size_t k = 0; k < P; k++)
                std::cout << "(" << std::get<0>(array[i][j][k]) << ", " << std::get<1>(array[i][j][k]) << ", " << std::get<2>(array[i][j][k]) << ") ";
                std::cout << '\n';
        }
    }
}

输出:

(0, 0, 0) (0, 0, 1) (0, 0, 2) (0, 0, 3)
(0, 1, 0) (0, 1, 1) (0, 1, 2) (0, 1, 3)
(1, 0, 0) (1, 0, 1) (1, 0, 2) (1, 0, 3)
(1, 1, 0) (1, 1, 1) (1, 1, 2) (1, 1, 3)
(2, 0, 0) (2, 0, 1) (2, 0, 2) (2, 0, 3)
(2, 1, 0) (2, 1, 1) (2, 1, 2) (2, 1, 3)

虽然可能有一个更短的解决方案。我会尝试改进这个丑陋的解决方案,但至少我让它运作起来。欢迎更好的解决方案。