sacgajcvs

Untitled

Sep 24th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. class File {
  7. public:
  8. string name, content;
  9. int time_created, recently_edited;
  10. File (string _name, string _content, int t1, int t2): name(_name), content(_content), time_created(t1), recently_edited(t2) {}
  11.  
  12. void editFile(string c, int t2) {
  13. content = c;
  14. recently_edited = t2;
  15. }
  16. };
  17.  
  18. class Branch {
  19. public:
  20. string name;
  21. int time_created;
  22. shared_ptr<Branch> parent;
  23. unordered_map<string, shared_ptr<File>> files;
  24. vector<shared_ptr<Branch>> child;
  25. Branch(string _name, int t1, shared_ptr<Branch> par) : name(_name), time_created(t1), parent(par) {}
  26.  
  27. void copyFiles(unordered_map<string, shared_ptr<File>>& f) {
  28. for(auto it : f) {
  29. files[it.first] = make_shared<File>(*it.second);
  30. }
  31. }
  32.  
  33. };
  34.  
  35. unordered_map<string, shared_ptr<Branch>> branches;
  36.  
  37.  
  38. void init() {
  39. branches.clear();
  40. auto root = make_shared<Branch> ("root", 0, nullptr);
  41. branches["root"] = root;
  42. }
  43.  
  44. void create(int mTime, char mBranch[], char mFile[], char mData[]) {
  45. auto branch = branches[mBranch];
  46. auto file = make_shared<File>(mFile, mData, mTime, mTime);
  47. branch -> files[mFile] = file;
  48. }
  49.  
  50. void edit(int mTime, char mBranch[], char mFile[], char mData[]) {
  51. auto branch = branches[mBranch];
  52. auto& file = branch -> files[mFile];
  53. file -> editFile(mData, mTime);
  54. }
  55.  
  56. void branch(int mTime, char mBranch[], char mChild[]) {
  57. auto par_branch = branches[mBranch];
  58. auto new_branch = make_shared<Branch>(mChild, mTime, par_branch);
  59. new_branch -> copyFiles(par_branch -> files);
  60. branches[mChild] = new_branch;
  61. par_branch -> child.push_back(new_branch);
  62. }
  63.  
  64. void mergeChild(shared_ptr<Branch> branch) {
  65. for(auto child : branch -> child) {
  66. mergeChild(child);
  67. branches.erase(child -> name);
  68. }
  69. branch -> child.clear();
  70. auto par = branch -> parent;
  71. for(auto it : branch -> files) {
  72. if(par -> files.find(it.first) == par -> files.end()) {
  73. par -> files[it.first] = it.second;
  74. } else {
  75. auto pf2 = par -> files[it.first];
  76. if(it.second -> recently_edited > pf2 -> recently_edited) {
  77. par -> files[it.first] = it.second;
  78. }
  79. }
  80. }
  81. branch -> files.clear();
  82. // mainMerge(branch -> parent);
  83. }
  84.  
  85. void mainMerge(shared_ptr<Branch> branch) {
  86. mergeChild(branch);
  87. branches.erase(branch -> name);
  88. int ps = -1;
  89. for(int i = 0; i < branch -> parent -> child.size(); i++) {
  90. if(branch -> parent -> child[i] == branch) {
  91. ps = i;
  92. break;
  93. }
  94. }
  95. branch -> parent -> child.erase(branch -> parent -> child.begin() + ps);
  96. }
  97.  
  98. void merge(int mTime, char mBranch[]) {
  99. mainMerge(branches[mBranch]);
  100. }
  101.  
  102. int readFile(int mTime, char mBranch[], char mFile[], char retstring[]) {
  103. auto branch = branches[mBranch];
  104. auto& file = branch -> files[mFile];
  105. const char *out = file -> content.c_str();
  106. strcpy(retstring, out);
  107. // cout << "here = " << out << " " << retstring << endl;
  108. // retstring = &(file -> content[0]);
  109. return file -> content.length();
  110. }
  111. // void print() {
  112. // queue<shared_ptr<Branch>> q;
  113. // q.push(branches["root"]);
  114. // while(!q.empty()) {
  115. // auto cur = q.front();
  116. // q.pop();
  117. // cout << cur -> name << " -> " << endl;
  118. // for(auto i : cur -> files) {
  119. // cout << i.first << " " << i.second -> content << endl;
  120. // }
  121. // cout << " --------- \n";
  122. // for(auto i : cur -> child) {
  123. // q.push(i);
  124. // }
  125. // }
  126. // cout << "---------\n";
  127. // }
  128.  
  129.  
  130. int main()
  131. {
  132. int t;
  133. cin >> t;
  134. char a[20], b[20], c[20], d[20];
  135. int num;
  136. init();
  137. for(int q = 1; 1 < t; q++) {
  138. int type;
  139. cin >> type;
  140. switch(type) {
  141. case 1:
  142. scanf("%s %s %s", a, b, c);
  143. create(q, a, b, c);
  144. break;
  145. case 2:
  146. scanf("%s %s %s", a, b, c);
  147. edit(q, a, b, c);
  148. break;
  149. case 3:
  150. scanf("%s %s", a, b);
  151. branch(q, a, b);
  152. break;
  153. case 4:
  154. scanf("%s", a);
  155. merge(q, a);
  156. break;
  157. case 5:
  158. scanf("%s %s %d %s", a, b, &num, d);
  159. int len = readFile(q, a, b, c);
  160. // scanf("%d %s", &num, d);
  161. if(num != len) {
  162. cout << q << " failed len\n" << c << " " << len << endl;
  163. return 0;
  164. } else if(strcmp(c, d) != 0) {
  165. cout << q << " failed vale\n" << c << " " << len << endl;
  166. return 0;
  167. }
  168. break;
  169. }
  170. cout << q << " ";
  171. print();
  172. }
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment