Combination explosion of an enum value (729 combin

2019-07-25 17:41发布

问题:

im facing a problem in which I have to generate a huge amount of code, all of it fairly similar, and I want to know if there is any way to templatized.

Lets say I have a structure of this type

template <typename ...NodeKindTs>
struct Element{
std::tuple<NodeKindTs...> nodes;
}

I have a vector of ints which related a Node with another, and a vector of an enum which says which kind of Node is each. The kind can be A, B or C.

enum class Kind {A,B,C};
std::vector<int> relationShip;
std::vector<Kind> kind;

For example, if I have

relationShip = {1,2,-1};
kind = {A,B,A}

would mean that the first node is of kind A and is related to the second node, which is of kind B. You get it.

Now, I have to create Elements and insert them into a vector depending on the NodeKind that each node and the relation ship. This Elements are templatized by up to 6 NodeKinds. For solving this, I need a huge if that check the Kind of each node and then call the Element ctor.

For the 2 NodeKinds case, this means doing something like

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 is a meta-function I have create which insert the element into the container if fits in from a list of containers.

For this 2 case, this requires up to 9 ifs. For the 3 case it requires 27 ifs and for the 6 case it requires 729 ifs. I really dont want to code them.

Any idea on how I could solve this?

Thanks!

回答1:

With std::variant, you might have something like:

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

And then

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

So std::visit would generate the 729 overloads for you.



回答2:

Maybe something like this can be used as a starting point (I may have misunderstood the question and there are probably ways to make it shorter):

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