枚举价值的组合爆炸(729组合..)
Combination explosion of an enum value (729 combinations...)
我面临的问题,我必须生成大量代码,所有这些代码都相当相似,我想知道是否有任何启动模板。
可以说我有这种类型的结构
template <typename ...NodeKindTs>
struct Element{
std::tuple<NodeKindTs...> nodes;
}
i具有INT的向量,该向量将节点与另一个节点相关,而枚举的向量则说明哪种节点是。这种可以是a,b或c。
enum class Kind {A,B,C};
std::vector<int> relationShip;
std::vector<Kind> kind;
例如,如果我有
relationShip = {1,2,-1};
kind = {A,B,A}
意味着第一个节点是友善的a,并且与第二个节点有关,该节点是善良的。
现在,我必须创建元素并将它们插入向量,这取决于每个节点和关系船的nodekind。该元素由多达6个Nodekinds模板。为了解决这个问题,如果那样检查每个节点的种类,然后调用元素ctor。
对于2个Nodekinds案例,这意味着要做
之类的事情if (node1.type == A && node2.type == A) {
auto &simNode1 = containerOfAs(node1.id);
auto &smiNode2 = containerOfAs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
if (node1.type == A && node2.type == C)
{
auto &simNode1 = containerOfAs(node1.id);
auto &smiNode2 = containerOfCs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
if (node1.type == B && node2.type == C)
{
auto &simNode1 = containerOfBs(node1.id);
auto &smiNode2 = containerOfCs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
...
inserelement是我创建的元功能,如果适合容器列表,则将元素插入容器中。
对于此两种情况,最多需要9个IF。对于3个案例,它需要27个IF,对于6个情况,它需要729个IF。我真的不想编码它们。
我如何解决这个问题?
谢谢!
使用std::variant
,您可能有类似的东西:
std::variant<std::reference_wrapper<NodeA>,
std::reference_wrapper<NodeB>,
std::reference_wrapper<NodeC>> getNodeAsVariant(Kind kind, int id)
{
switch (kind) {
case Kind::A: return containerOfAs(id);
case Kind::B: return containerOfBs(id);
case Kind::C: return containerOfCs(id);
}
throw std::runtime_error("Invalid kind");
}
,然后
auto v1 = getNodeAsVariant(node1.type, node1.id);
auto v2 = getNodeAsVariant(node2.type, node2.id);
auto v3 = getNodeAsVariant(node3.type, node3.id);
auto v4 = getNodeAsVariant(node4.type, node4.id);
auto v5 = getNodeAsVariant(node5.type, node5.id);
auto v6 = getNodeAsVariant(node6.type, node6.id);
// r1, .., r6 would be the correct reference_wrapper<T>
std::visit([](auto r1, auto r2, auto r3, auto r4, auto r5, auto r6) {
insertElement(elementFactory(r1/*.get()*/, r2, r3, r4, r5, r6));
}, v1, v2, v3, v4, v5, v6);
因此std::visit
将为您生成729个过载。
也许可以将类似的东西用作起点(我可能已经误解了这个问题,并且可能有一些方法可以使其短):
#include <iostream>
enum class Kind { A, B, C };
std::ostream& operator<<(std::ostream& os, Kind k) {
switch (k) {
case Kind::A: return os << "A";
case Kind::B: return os << "B";
case Kind::C: return os << "C";
}
return os << "Unknown";
}
template<typename F>
void dispatch(Kind k, F f) {
switch (k) {
case Kind::A: f.template call<Kind::A>(); return;
case Kind::B: f.template call<Kind::B>(); return;
case Kind::C: f.template call<Kind::C>(); return;
}
abort();
}
template<Kind k1>
struct handle2 {
template<Kind k2>
void call() {
std::cout << "k1=" << k1 << " k2=" << k2 << "n";
// Do your thing with k1 and k2 here
}
};
struct handle1 {
Kind k2;
template<Kind k1>
void call() {
dispatch(k2, handle2<k1>{});
}
};
void handle(Kind k1, Kind k2) {
dispatch(k1, handle1{k2});
}
int main() {
handle(Kind::C, Kind::B);
}
相关文章:
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- 有没有办法将不连续的捕获组组合到单个正则表达式中?
- 如何通过组合多个字符数组来创建字符数组?
- 如何计算数组整数的总可能组合
- 如何将数组的每个n个元素组合成一个元素
- 检查一组类型是否是具有组合的子集
- 在编译时将两个或多个不同大小的数组合并为一个数组
- 返回 sum 的数组中元素的组合
- 将两个数组组合在一起,形成每个项目的一对
- C 将数组值插入另一个数组中,但将相似值组合到相同的元素中
- 如何将多个可变参数模板元组类组合成一个类?
- 获取C++中多维数组元素的所有组合
- 在循环中组合元组
- 将类成员数组组合到单个数组时性能下降
- 枚举价值的组合爆炸(729组合..)
- 组合两个数组元素或将一个数组元素分发到另一个数组元素
- 在数组C++中创建所有可能的非唯一整数组合
- "无法封送'返回值':无效的托管/非托管类型组合",同时使用字符串数组封送委托
- 找到两个向量数组的组合,该数组的总和是 c++ 中所有其他组合的最大值
- 将glUniformMatrix与数组组合使用时出现分割错误