Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class File {
- public:
- string name, content;
- int time_created, recently_edited;
- File (string _name, string _content, int t1, int t2): name(_name), content(_content), time_created(t1), recently_edited(t2) {}
- void editFile(string c, int t2) {
- content = c;
- recently_edited = t2;
- }
- };
- class Branch {
- public:
- string name;
- int time_created;
- shared_ptr<Branch> parent;
- unordered_map<string, shared_ptr<File>> files;
- vector<shared_ptr<Branch>> child;
- Branch(string _name, int t1, shared_ptr<Branch> par) : name(_name), time_created(t1), parent(par) {}
- void copyFiles(unordered_map<string, shared_ptr<File>>& f) {
- for(auto it : f) {
- files[it.first] = make_shared<File>(*it.second);
- }
- }
- };
- unordered_map<string, shared_ptr<Branch>> branches;
- void init() {
- auto root = make_shared<Branch> ("root", 0, nullptr);
- branches["root"] = root;
- }
- void create(int mTime, char mBranch[], char mFile[], char mData[]) {
- auto branch = branches[mBranch];
- auto file = make_shared<File>(mFile, mData, mTime, mTime);
- branch -> files[mFile] = file;
- }
- void edit(int mTime, char mBranch[], char mFile[], char mData[]) {
- auto branch = branches[mBranch];
- auto& file = branch -> files[mFile];
- file -> editFile(mData, mTime);
- }
- void branch(int mTime, char mBranch[], char mChild[]) {
- auto par_branch = branches[mBranch];
- auto new_branch = make_shared<Branch>(mChild, mTime, par_branch);
- new_branch -> copyFiles(par_branch -> files);
- branches[mChild] = new_branch;
- par_branch -> child.push_back(new_branch);
- }
- void mergeChild(shared_ptr<Branch> branch) {
- for(auto child : branch -> child) {
- mergeChild(child);
- branches.erase(child -> name);
- }
- branch -> child.clear();
- auto par = branch -> parent;
- for(auto it : branch -> files) {
- if(par -> files.find(it.first) == par -> files.end()) {
- par -> files[it.first] = it.second;
- } else {
- auto pf2 = par -> files[it.first];
- if(it.second -> recently_edited > pf2 -> recently_edited) {
- par -> files[it.first] = it.second;
- }
- }
- }
- branch -> files.clear();
- // mainMerge(branch -> parent);
- }
- void mainMerge(shared_ptr<Branch> branch) {
- mergeChild(branch);
- branches.erase(branch -> name);
- int ps = -1;
- for(int i = 0; i < branch -> parent -> child.size(); i++) {
- if(branch -> parent -> child[i] == branch) {
- ps = i;
- break;
- }
- }
- branch -> parent -> child.erase(branch -> parent -> child.begin() + ps);
- }
- void merge(int mTime, char mBranch[]) {
- mainMerge(branches[mBranch]);
- }
- int readFile(int mTime, char mBranch[], char mFile[], char retstring[]) {
- auto branch = branches[mBranch];
- auto& file = branch -> files[mFile];
- const char *out = file -> content.c_str();
- strcpy(retstring, out);
- // cout << "here = " << out << " " << retstring << endl;
- // retstring = &(file -> content[0]);
- return file -> content.length();
- }
- // void print() {
- // queue<shared_ptr<Branch>> q;
- // q.push(branches["root"]);
- // while(!q.empty()) {
- // auto cur = q.front();
- // q.pop();
- // cout << cur -> name << " -> " << endl;
- // for(auto i : cur -> files) {
- // cout << i.first << " " << i.second -> content << endl;
- // }
- // cout << " --------- \n";
- // for(auto i : cur -> child) {
- // q.push(i);
- // }
- // }
- // cout << "---------\n";
- // }
- int main()
- {
- int t;
- cin >> t;
- char a[20], b[20], c[20], d[20];
- int num;
- init();
- for(int q = 1; 1 < t; q++) {
- int type;
- cin >> type;
- switch(type) {
- case 1:
- scanf("%s %s %s", a, b, c);
- create(q, a, b, c);
- break;
- case 2:
- scanf("%s %s %s", a, b, c);
- edit(q, a, b, c);
- break;
- case 3:
- scanf("%s %s", a, b);
- branch(q, a, b);
- break;
- case 4:
- scanf("%s", a);
- merge(q, a);
- break;
- case 5:
- scanf("%s %s %d %s", a, b, &num, d);
- int len = readFile(q, a, b, c);
- // scanf("%d %s", &num, d);
- if(num != len) {
- cout << q << " failed len\n" << c << " " << len << endl;
- return 0;
- } else if(strcmp(c, d) != 0) {
- cout << q << " failed vale\n" << c << " " << len << endl;
- return 0;
- }
- break;
- }
- cout << q << " ";
- print();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment