source: doc/theses/jiada_liang_MMath/offsetAlgorithm.cc@ c2f6b79

Last change on this file since c2f6b79 was bd686f0, checked in by JiadaL <j82liang@…>, 13 months ago

CFAEnum to CFAEnum conversion algorithm (the previous was enumerator to CFAEnum which is not a cast suppose to do)

  • Property mode set to 100644
File size: 4.4 KB
Line 
1#include <iostream>
2#include <string>
3#include <variant>
4#include <vector>
5
6using namespace std;
7struct Enumerator {
8 string label;
9
10 Enumerator(string label) : label(label) {}
11};
12
13inline static bool operator==(const Enumerator& lhs, const Enumerator& rhs) {
14 return lhs.label == rhs.label;
15}
16
17struct 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
31 CFAEnum(string name) : name(name) {}
32};
33
34inline 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
61pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) {
62 int offset = 0;
63 // std::cout << dst.name << " : " << e.label << std::endl;
64 if (src == dst) return make_pair(true, 0);
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;
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 }
79 }
80 }
81 // std::cout << "End " << dst.name << " offset " << offset << std::endl;
82 return make_pair(false, offset);
83}
84
85std::ostream& operator<<(std::ostream& os, const CFAEnum& e) {
86 os << e.name;
87 return os;
88}
89
90void 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
101int main() {
102 /**
103 enum() E1 { A }; // A
104 enum() E2 { B, C }; // B C
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
107 *
108 */
109 struct Enumerator A("A"), B("B"), C("C"), D("D"), E("E"), F("F"), G("G");
110 struct CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4");
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);
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);
133}
134
135// Compile g++ -std=c++17 offsetAlgorithm.cc
Note: See TracBrowser for help on using the repository browser.