diff --git a/.gitignore b/.gitignore index 9348895..9bb52f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .vscode +bin main .attach_* *.o +*.dat +*.log diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f25764c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/bin/main", + "args": ["1"], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "preLaunchTask": "make", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ef5ef7e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,40 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "chrono": "cpp", + "cstdint": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..1803238 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,12 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "make", + "type": "shell", + "command": "make" + } + ] +} \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..977e08d --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +./bin/main 1 +./bin/main 1_1 +./bin/main 2 +./bin/main 2_1 +./bin/main 3 diff --git a/src/main.cpp b/src/main.cpp index 52df895..7852cf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,126 +1,140 @@ -#include -#include -#include -#include -#include "vbtree.h" #include "sbt.h" - +#include "vbtree.h" +#include +#include +#include +#include +#include +#include using namespace std; using chrono::high_resolution_clock; +using std::string; -int IntCompare(int v1, int v2) -{ - if (v1 > v2) - { +int IntCompare(int v1, int v2) { + if (v1 > v2) { return 1; - } - else if (v1 < v2) - { + } else if (v1 < v2) { return -1; - } - else - { + } else { return 0; } } const ULONG N = 5000000; -void Case1() -{ +vector vec; +map funcmap ; + +void init() { + const char *fpath = "./vec.dat"; + std::ifstream inf(fpath); + if (!inf.is_open()) { + std::ofstream openfile(fpath, ios::binary | ios::trunc); + default_random_engine e; + std::uniform_int_distribution<> dist{0, 1000000000}; + for (ULONG i = 0; i < N; i++) { + auto v = dist(e); + openfile << v << " "; + + } + openfile.close(); + inf.open(fpath); + } + for(;!inf.eof();) { + int v; + inf >> v; + vec.push_back(v); + } +} + +void Case1() { std::map m; default_random_engine e; std::uniform_int_distribution<> dist{0, 1000000000}; - high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t1 = + high_resolution_clock::now(); //返回时间戳 - for (ULONG i = 0; i < N; i++) - { - auto v = dist(e); + for (ULONG i = 0; i < N; i++) { + auto v = vec[i]; m[v] = v; } - high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t2 = + high_resolution_clock::now(); //返回时间戳 std::cout << (t2 - t1).count() / N << std::endl; - std::cout << "end RBTree Case Benchmark" << std::endl; + std::cout << "end RBTree Case Benchmark" << std::endl; + + + } -void Case1_1() -{ +void Case1_1() { std::map m; default_random_engine e; std::uniform_int_distribution<> dist{0, 1000000000}; - vector vec; - - for (ULONG i = 0; i < N; i++) - { - auto v = dist(e); + for (ULONG i = 0; i < N; i++) { + auto v = vec[i]; m[v] = v; - vec.push_back(v); } - high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t1 = + high_resolution_clock::now(); //返回时间戳 - for(auto iter = vec.begin(); iter != vec.end() ; iter++) { - m[*iter]; + for (auto iter = vec.begin(); iter != vec.end(); iter++) { + m[*iter]; } - high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t2 = + high_resolution_clock::now(); //返回时间戳 std::cout << (t2 - t1).count() / N << std::endl; - std::cout << "end RBTree Case Benchmark" << std::endl; + std::cout << "end RBTree Case Benchmark" << std::endl; } -void Case2() -{ +void Case2() { + VBTree m(IntCompare); + + high_resolution_clock::time_point t1 = + high_resolution_clock::now(); //返回时间戳 + for (ULONG i = 0; i < N; i++) { + auto v = vec[i]; + m.put(v, v); + } + high_resolution_clock::time_point t2 = + high_resolution_clock::now(); //返回时间戳 + + std::cout << (t2 - t1).count() / N << std::endl; + std::cout << "end VBTree Case Benchmark" << std::endl; +} + +void Case2_1() { VBTree m(IntCompare); default_random_engine e; std::uniform_int_distribution<> dist{0, 1000000000}; - high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 - - for (ULONG i = 0; i < N; i++) - { - auto v = dist(e); + for (ULONG i = 0; i < N; i++) { + auto v = vec[i]; m.put(v, v); } - high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 - std::cout << (t2 - t1).count() / N << std::endl; - std::cout << "end VBTree Case Benchmark" << std::endl; -} + high_resolution_clock::time_point t1 = + high_resolution_clock::now(); //返回时间戳 -void Case2_1() -{ - VBTree m(IntCompare); - - default_random_engine e; - std::uniform_int_distribution<> dist{0, 1000000000}; - - vector vec; - - for (ULONG i = 0; i < N; i++) - { - auto v = dist(e); - m.put(v, v); - vec.push_back(v); + for (auto iter = vec.begin(); iter != vec.end(); iter++) { + m.get(*iter); } - high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 - - for(auto iter = vec.begin(); iter != vec.end() ; iter++) { - m.get(*iter); - } - - high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t2 = + high_resolution_clock::now(); //返回时间戳 std::cout << (t2 - t1).count() / N << std::endl; - std::cout << "end VBTree Case Benchmark" << std::endl; + std::cout << "end VBTree Case Benchmark" << std::endl; } void Case3() { @@ -129,21 +143,32 @@ void Case3() { default_random_engine e; std::uniform_int_distribution<> dist{0, 1000000000}; - high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t1 = + high_resolution_clock::now(); //返回时间戳 - for (ULONG i = 0; i < N; i++) - { - auto v = dist(e); + for (ULONG i = 0; i < N; i++) { + auto v = vec[i]; tree.insert(v); } - high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 + high_resolution_clock::time_point t2 = + high_resolution_clock::now(); //返回时间戳 std::cout << (t2 - t1).count() / N << std::endl; - std::cout << "end SBT Case Benchmark" << std::endl; + std::cout << "end SBT Case Benchmark" << std::endl; } -int main(int argc, char *argv[]) -{ - Case2_1(); - // Case1(); +int main(int argc, char *argv[]) { + init(); + + funcmap["1"] = Case1; + funcmap["1_1"] = Case1_1; + + funcmap["2"] = Case2; + funcmap["2_1"] = Case2_1; + + funcmap["3"] = Case3; + + cout << argv[1] << endl; + string fname = argv[1]; + funcmap[fname](); } \ No newline at end of file diff --git a/src/vbtree.h b/src/vbtree.h index 8eba988..0100844 100644 --- a/src/vbtree.h +++ b/src/vbtree.h @@ -401,7 +401,6 @@ public: return; } - std::vector vsize; for (TreeNode *cur = this->root;;) { @@ -416,8 +415,7 @@ public: } } - // cur->size++; - vsize.push_back(cur); + cur->size++; int c = this->compare(key, cur->key); if (c < 0) @@ -428,10 +426,6 @@ public: node->key = key; node->value = value; - for (auto iter = vsize.begin(); iter != vsize.end();iter++) { - (*iter)->size ++ ; - } - cur->children[0] = node; node->parent = cur; @@ -450,7 +444,7 @@ public: } cur = cur->children[0]; } - else if (c > 0) + else { if (cur->children[1] == NULL) { @@ -458,10 +452,6 @@ public: node->key = key; node->value = value; - for (auto iter = vsize.begin(); iter != vsize.end();iter++) { - (*iter)->size ++ ; - } - cur->children[1] = node; node->parent = cur; @@ -480,13 +470,10 @@ public: } cur = cur->children[1]; } - else - { - cur->key = key; - cur->value = value; - return; - } } + + cout << "错误" << endl; + return ; }; /* data */