-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (84 loc) · 2.07 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
#include "bPlusTree.hpp"
#include "constant.hpp"
void executeCmd(vector<string> &cmdSplitVec, bPlusTree *tree) {
string cmd = cmdSplitVec[0];
if(cmd == initCmd) {
int degree = stoi(cmdSplitVec[1]);
tree->init(degree);
}
else if (cmd == searchCmd) {
if(cmdSplitVec.size() == searchParCount) {
int key = stoi(cmdSplitVec[1]);
tree->search(key);
}
else {
int start = stoi(cmdSplitVec[1]);
int end = stoi(cmdSplitVec[2]);
tree->searchRange(start, end);
}
}
else if(cmd == insertCmd) {
int key = stoi(cmdSplitVec[1]);
double value = stof(cmdSplitVec[2]);
tree->insertion(key, value);
}
else if(cmd == deleteCmd) {
int key = stoi(cmdSplitVec[1]);
tree->deletion(key);
}
else if(cmd == printTreeCmd){
tree->printTree(tree->getRoot());
}
else if(cmd == printLeadCmd){
tree->printLeafList();
}
}
vector<string> splitCmd(string line) {
vector<string> cmdSplitVec;
size_t prev = 0, pos;
while((pos = line.find_first_of("(,) ", prev)) != string::npos){
string sub = line.substr(prev, pos-prev);
if(sub.find_first_not_of(' ') != std::string::npos) {
cmdSplitVec.push_back(sub);
}
prev = pos+1;
}
return cmdSplitVec;
}
int main(int argc, char** argv) {
// check input name is in agrv
if(argc < 2) {
// go test case
bPlusTree t(3);
for(int i=0; i<10000; i++) {
t.insertion(i,i);
}
for(int i=0; i<10000; i++) {
t.deletion(i);
}
t.printTree(t.getRoot());
return -1;
}
string inputFileName = argv[1];
string line;
ifstream inputFile(inputFileName);
/**
* @brief redirect cout to output_file.txt!
*/
std::ofstream out("output_file.txt");
std::cout.rdbuf(out.rdbuf());
static vector<string> splitResult;
bPlusTree *tree = new bPlusTree();
if(inputFile.is_open()) {
while(getline(inputFile, line)) {
splitResult = splitCmd(line);
executeCmd(splitResult, tree);
}
inputFile.close();
}
return 0;
}