gpt4 book ai didi

c++ - 在编译时推导整个 vector 空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:35 27 4
gpt4 key购买 nike

受到这个问题的启发:c++ generate (xyz) points in range

我开始怀疑是否有一种形式的模板代码可以,从这个声明:

using X = axis_limits<-10, +10>;
using Y = axis_limits<-10, +10>;
using Z = axis_limits<-10, +10>;

auto space = std::vector<point>{ generate_point_space<X, Y, Z> };

在编译时构造一个名为 space 的 vector ,其中每个 x、y、z 包含一个点,其中 begin(X) <= x < end(X)... y 和 z 等。

顺序不重要。

generate_point_space<> 的返回类型应该是 std::initializer_list<int>或类似的编译时构造的序列。我不想生成对 push_back() 的调用序列.那太容易了:)

struct point将具有以下形式的构造函数:

point::point(int x, int y, int z)

整数的单一维度很简单(下面的代码)。这个问题的多维方面今天超出了我的范围

#include <utility>
#include <iostream>
#include <vector>

template<int Begin, int End>
struct axis_limits
{
static constexpr int first = Begin;
static constexpr int last = End;
};

namespace details
{
template<typename Int, typename, Int Begin, bool Increasing>
struct integer_range_impl;

template<typename Int, Int... N, Int Begin>
struct integer_range_impl<Int, std::integer_sequence<Int, N...>, Begin, true> {
using type = std::integer_sequence<Int, N+Begin...>;
};

template<typename Int, Int... N, Int Begin>
struct integer_range_impl<Int, std::integer_sequence<Int, N...>, Begin, false> {
using type = std::integer_sequence<Int, Begin-N...>;
};
}

template<typename Int, Int Begin, Int End>
using integer_range = typename details::integer_range_impl<
Int,
std::make_integer_sequence<Int, (Begin<End) ? End-Begin : Begin-End>,
Begin,
(Begin<End) >::type;

template<int...Is>
std::vector<int> make_vector(std::integer_sequence<int, Is...>)
{
return std::vector<int> { Is... };
}

template<int Begin, int End>
struct axis_range
{
using sequence_type = integer_range<int, Begin, End>;
static constexpr int size = sequence_type::size();
static std::vector<int> as_vector()
{
return make_vector(sequence_type {});
}
};

template< int Begin, int End >
std::vector<int> make_axis(const axis_limits<Begin, End> &)
{
return axis_range<Begin, End>::as_vector();
}

template<class T>
void dump_vector(std::ostream& os, const std::vector<T>& v) {
const char* sep = "{ ";
for(const auto& i : v) {
os << sep << i;
sep = ", ";
}
os << " }";
}

template<class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
dump_vector(os, vec);
return os;
}

using namespace std;

int main()
{

using X = axis_limits<-5, +5>;
auto space = std::vector<int>(make_axis(X{}));
cout << space << endl;
return 0;
}

当前输出:

{ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 }

我在找什么:

{ { -10, -10, -10 }, { -10, -10, -9 } .... { 9, 9, 8 }, { 9, 9, 9 } }

最佳答案

您可以执行以下操作:

template<int Begin, int End>
struct axis_limits
{
static constexpr int first = Begin;
static constexpr int last = End;
static constexpr int range = End - Begin + 1;
};

struct point
{
explicit point(int x, int y, int z) : x(x), y(y), z(z) {}
int x; int y; int z;
};

namespace detail
{

template <typename X, typename Y, typename Z, std::size_t... Is>
std::vector<point> generate_point_space_impl(std::index_sequence<Is...>)
{
return {point(
static_cast<int>(Is / (Z::range * Y::range)) % X::range + X::first,
static_cast<int>(Is / Z::range) % Y::range + Y::first,
static_cast<int>(Is) % Z::range + Z::first)...
};
}

}


template <typename X, typename Y, typename Z>
std::vector<point> generate_point_space()
{
return detail::generate_point_space_impl<X, Y, Z>(std::make_index_sequence<X::range * Y::range * Z::range>());
}

Live demo

关于c++ - 在编译时推导整个 vector 空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30171531/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com