Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
File System
/* File System Write a file system class, which has two functions: create, and get create("/a",1) get("/a") //get 1 create("/a/b",2) get("/a/b") //get 2 create("/c/d",1) //Error, because "/c" is not existed get("/c") //Error, because "/c" is not existed */ #include <iostream> #include <map> #include <string> #include <assert.h> using namespace std; class Runable{ }; class FileSystem{ map<string,int> m; // path, val map<string, Runable> callbackMap; // path, runable public: bool create(string path, int val){ //remove the last '/' if(path.back() == '/') path.pop_back(); //itself should not already exist if(m.count(path)) return false; //parent should exist int pos = path.find_last_of('/'); if(pos){ string parent = path.substr(0, pos); if(m.count(parent) == 0) return false; } m[path] = val; return true; } int get(string path){ if(m.count(path) == 0) return -1; return m[path]; } bool set(string path, int val){ if(m.count(path) == 0) return false; m[path] = val; return true; } bool watch(string path, Runable running){ if(m.count(path) == 0) return false; callbackMap[path] = running; return true; } }; int main() { FileSystem fs; bool res1 = fs.create("/a",1); bool res2 = fs.create("/a/b", 2); bool res3 = fs.create("/c/d",1); assert(res1 == true); assert(res2 == true); assert(res3 == false); int v1 = fs.get("/a"); //get 1 int v2 = fs.get("/a/b"); //get 2 int v3 = fs.get("/c"); assert(v1 == 1); assert(v2 == 2); assert(v3 == -1); Runable x; // watching bool rx = fs.watch("/a", x); assert(rx == true); cout << endl << "All pass !" << endl << endl; return 0; }
run
|
edit
|
history
|
help
0
Изволов#7
ProPriceTemp
project
Teatime Snack
CV
Projekt misker
Longest Palindrom in String
VirtualResto
doubly
Merge problem