[bd686f0] | 1 | #include <iostream> |
---|
[d6c5faa] | 2 | #include <string> |
---|
| 3 | #include <variant> |
---|
| 4 | #include <vector> |
---|
| 5 | |
---|
| 6 | using namespace std; |
---|
| 7 | struct Enumerator { |
---|
| 8 | string label; |
---|
| 9 | |
---|
| 10 | Enumerator(string label) : label(label) {} |
---|
| 11 | }; |
---|
| 12 | |
---|
| 13 | inline static bool operator==(const Enumerator& lhs, const Enumerator& rhs) { |
---|
| 14 | return lhs.label == rhs.label; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | struct CFAEnum { |
---|
| 18 | vector<variant<CFAEnum, Enumerator>> members; |
---|
| 19 | string name; |
---|
| 20 | |
---|
| 21 | CFAEnum& addMember(CFAEnum inlineMember) { |
---|
| 22 | members.push_back(inlineMember); |
---|
| 23 | return *this; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | CFAEnum& addMember(Enumerator enumerator) { |
---|
| 27 | members.push_back(enumerator); |
---|
| 28 | return *this; |
---|
| 29 | } |
---|
| 30 | |
---|
[bd686f0] | 31 | CFAEnum(string name) : name(name) {} |
---|
[d6c5faa] | 32 | }; |
---|
| 33 | |
---|
[bd686f0] | 34 | inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { |
---|
| 35 | return lhs.name == rhs.name; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // pair<bool, int> calculateEnumOffset(CFAEnum dst, Enumerator e) { |
---|
| 39 | // int offset = 0; |
---|
| 40 | // // std::cout << dst.name << " : " << e.label << std::endl; |
---|
| 41 | // for (auto v : dst.members) { |
---|
| 42 | // // std::cout << " offset: " << offset << std::endl; |
---|
| 43 | // if (holds_alternative<Enumerator>(v)) { |
---|
| 44 | // auto m = get<Enumerator>(v); |
---|
| 45 | // // std::cout << " Enumerator: " << ":" << m.label << std::endl; |
---|
| 46 | // if (m == e) return make_pair(true, offset); |
---|
| 47 | // offset++; |
---|
| 48 | |
---|
| 49 | // } else { |
---|
| 50 | // auto m = get<CFAEnum>(v); |
---|
| 51 | // // std::cout << " CFAEnum: " << ":" << m.name << std::endl; |
---|
| 52 | // auto p = calculateEnumOffset(m, e); |
---|
| 53 | // if (p.first) return make_pair(true, offset + p.second); |
---|
| 54 | // offset += p.second; |
---|
| 55 | // } |
---|
| 56 | // } |
---|
| 57 | // // std::cout << "End " << dst.name << " offset " << offset << std::endl; |
---|
| 58 | // return make_pair(false, offset); |
---|
| 59 | // } |
---|
| 60 | |
---|
| 61 | pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) { |
---|
[d6c5faa] | 62 | int offset = 0; |
---|
| 63 | // std::cout << dst.name << " : " << e.label << std::endl; |
---|
[bd686f0] | 64 | if (src == dst) return make_pair(true, 0); |
---|
[d6c5faa] | 65 | for (auto v : dst.members) { |
---|
| 66 | // std::cout << " offset: " << offset << std::endl; |
---|
| 67 | if (holds_alternative<Enumerator>(v)) { |
---|
| 68 | offset++; |
---|
| 69 | } else { |
---|
| 70 | auto m = get<CFAEnum>(v); |
---|
| 71 | // std::cout << " CFAEnum: " << ":" << m.name << std::endl; |
---|
[bd686f0] | 72 | if (m == src) return make_pair(true, offset); |
---|
| 73 | auto dist = calculateEnumOffset(src, m); |
---|
| 74 | if (dist.first) { |
---|
| 75 | return make_pair(true, offset + dist.second); |
---|
| 76 | } else { |
---|
| 77 | offset += dist.second; |
---|
| 78 | } |
---|
[d6c5faa] | 79 | } |
---|
| 80 | } |
---|
| 81 | // std::cout << "End " << dst.name << " offset " << offset << std::endl; |
---|
| 82 | return make_pair(false, offset); |
---|
| 83 | } |
---|
| 84 | |
---|
[bd686f0] | 85 | std::ostream& operator<<(std::ostream& os, const CFAEnum& e) { |
---|
| 86 | os << e.name; |
---|
| 87 | return os; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void printEnumOffset(CFAEnum src, CFAEnum dst) { |
---|
| 91 | auto offset = calculateEnumOffset(src, dst); |
---|
| 92 | if (offset.first) { |
---|
| 93 | std::cout << src << " To " << dst << ":" |
---|
| 94 | << " " << calculateEnumOffset(src, dst).second << std::endl; |
---|
| 95 | } else { |
---|
| 96 | std::cout << src << " Cannot convert to " << dst << std::endl; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | |
---|
[d6c5faa] | 101 | int main() { |
---|
| 102 | /** |
---|
[bd686f0] | 103 | enum() E1 { A }; // A |
---|
[d6c5faa] | 104 | enum() E2 { B, C }; // B C |
---|
[bd686f0] | 105 | enum() E3 { D, inline E1, inline E2, E }; // D {A}_{E1} {B C}_{E2} E |
---|
| 106 | enum() E4 { F, inline E3, G }; // F { D {A}_{E1} {B C}_{E2} E }_{E3} G |
---|
[d6c5faa] | 107 | * |
---|
| 108 | */ |
---|
| 109 | struct Enumerator A("A"), B("B"), C("C"), D("D"), E("E"), F("F"), G("G"); |
---|
[bd686f0] | 110 | struct CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4"); |
---|
[d6c5faa] | 111 | E1.addMember(A); |
---|
| 112 | E2.addMember(B).addMember(C); |
---|
| 113 | E3.addMember(D).addMember(E1).addMember(E2).addMember(E); |
---|
| 114 | E4.addMember(F).addMember(E3).addMember(G); |
---|
[bd686f0] | 115 | // std::cout << calculateEnumOffset(E3, B).first << " " |
---|
| 116 | // << calculateEnumOffset(E3, B).second << std::endl; |
---|
| 117 | // std::cout << calculateEnumOffset(E4, B).first << " " |
---|
| 118 | // << calculateEnumOffset(E4, B).second << std::endl; |
---|
| 119 | // std::cout << calculateEnumOffset(E3, E).first << " " |
---|
| 120 | // << calculateEnumOffset(E3, E).second << std::endl; |
---|
| 121 | // std::cout << calculateEnumOffset(E4, E).first << " " |
---|
| 122 | // << calculateEnumOffset(E4, E).second << std::endl; |
---|
| 123 | // std::cout << calculateEnumOffset(E3, G).first << " " |
---|
| 124 | // << calculateEnumOffset(E3, G).second << std::endl; |
---|
| 125 | |
---|
| 126 | printEnumOffset(E1, E3); |
---|
| 127 | printEnumOffset(E1, E4); |
---|
| 128 | printEnumOffset(E2, E3); |
---|
| 129 | printEnumOffset(E2, E4); |
---|
| 130 | printEnumOffset(E3, E4); |
---|
| 131 | printEnumOffset(E4, E4); |
---|
| 132 | printEnumOffset(E4, E1); |
---|
[d6c5faa] | 133 | } |
---|
| 134 | |
---|
| 135 | // Compile g++ -std=c++17 offsetAlgorithm.cc |
---|