sacgajcvs

Untitled

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