- Timestamp:
- Aug 6, 2024, 4:13:13 PM (4 months ago)
- Branches:
- master
- Children:
- 8cb2ff6
- Parents:
- 11f8ff7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/offsetAlgorithm.cc
r11f8ff7 rbd686f0 1 #include <iostream> 1 2 #include <string> 2 3 #include <variant> 3 4 #include <vector> 4 #include <iostream>5 5 6 6 using namespace std; … … 29 29 } 30 30 31 CFAEnum( string name ): name(name) {}31 CFAEnum(string name) : name(name) {} 32 32 }; 33 33 34 pair<bool, int> calculateEnumOffset(CFAEnum dst, Enumerator e) { 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) { 35 62 int offset = 0; 36 63 // std::cout << dst.name << " : " << e.label << std::endl; 64 if (src == dst) return make_pair(true, 0); 37 65 for (auto v : dst.members) { 38 66 // std::cout << " offset: " << offset << std::endl; 39 67 if (holds_alternative<Enumerator>(v)) { 40 auto m = get<Enumerator>(v);41 // std::cout << " Enumerator: " << ":" << m.label << std::endl;42 if (m == e) return make_pair(true, offset);43 68 offset++; 44 45 69 } else { 46 70 auto m = get<CFAEnum>(v); 47 71 // std::cout << " CFAEnum: " << ":" << m.name << std::endl; 48 auto p = calculateEnumOffset(m, e); 49 if (p.first) return make_pair(true, offset + p.second); 50 offset += p.second; 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 } 51 79 } 52 80 } … … 55 83 } 56 84 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 57 101 int main() { 58 102 /** 59 *enum() E1 { A }; // A103 enum() E1 { A }; // A 60 104 enum() E2 { B, C }; // B C 61 enum() E3 { D, inline E1, inline E2, E }; // D A B CE62 enum() E4 { F, inline E3, G }; // F D A B C EG105 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 63 107 * 64 108 */ 65 109 struct Enumerator A("A"), B("B"), C("C"), D("D"), E("E"), F("F"), G("G"); 66 CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4");110 struct CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4"); 67 111 E1.addMember(A); 68 112 E2.addMember(B).addMember(C); 69 113 E3.addMember(D).addMember(E1).addMember(E2).addMember(E); 70 114 E4.addMember(F).addMember(E3).addMember(G); 71 std::cout << calculateEnumOffset(E3, B).first << " " << calculateEnumOffset(E3, B).second << std::endl; 72 std::cout << calculateEnumOffset(E4, B).first << " " << calculateEnumOffset(E4, B).second << std::endl; 73 std::cout << calculateEnumOffset(E3, E).first << " " << calculateEnumOffset(E3, E).second << std::endl; 74 std::cout << calculateEnumOffset(E4, E).first << " " << calculateEnumOffset(E4, E).second << std::endl; 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); 75 133 } 76 134
Note: See TracChangeset
for help on using the changeset viewer.