枚举价值的组合爆炸(729组合..)

Combination explosion of an enum value (729 combinations...)

本文关键字:729组 组合 枚举      更新时间:2023-10-16

我面临的问题,我必须生成大量代码,所有这些代码都相当相似,我想知道是否有任何启动模板。

可以说我有这种类型的结构

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);
}