-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.hpp
92 lines (85 loc) · 2.37 KB
/
completion.hpp
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
#ifndef COMPLETION_HPP_
#define COMPLETION_HPP_
#include <string>
#include <vector>
using namespace std;
class Completion {
public:
Completion(string cmd) {root=new CompletionNode(cmd);}
~Completion() {if(root) delete root;}
void insert(vector<string> data) {
CompletionNode *node = root;
// printf("\r\nInserting\n");
for(unsigned int i=0;i<data.size();i++) {
// printf("\r\nToken %s\n",data[i].c_str());
// printf("\r\nSearching Children\n");
bool found = false;
for(unsigned int j=0;j<node->children.size();j++) {
// printf("\r%s == %s\n",node->children[j]->data.c_str(),data[i].c_str());
if(!node->children[j]->data.compare(data[i])) {
found = true;
// printf("\r\nChild found\n");
node = node->children[j];
break;
}
}
if(!found) {
// printf("\r\nChild not found\n");
CompletionNode *temp_node = new CompletionNode(data[i]);
node->children.push_back(temp_node);
node = temp_node;
}
// }
}
}
void insert(string data) {
vector<string> input;
input.push_back(data);
insert(input);
}
vector<string> all () {
vector<string> output;
for(unsigned int i=0;i<root->children.size();i++)
output.push_back(root->children[i]->data);
return output;
}
vector<string> search(vector<string> &data) {
// printf("\r\nCompletion Searching\n");
vector<string> output;
CompletionNode *node = root;
bool error = false;
for(unsigned int i=0;i<data.size();i++) {
// printf("\r\nToken %s\n",data[i].c_str());
bool found = false;
for(unsigned int j=0;j<node->children.size();j++) {
// printf("\r%s == %s\n",node->children[j]->data.c_str(),data[i].c_str());
if(!node->children[j]->data.compare(data[i])) {
// if(!node->children[j]->children.size()) break;
found = true;
node = node->children[j];
// printf("\r\nChild found\n");
break;
}
}
if(!found) {
error=true;
// printf("\r\nChild not found\n");
break;
}
}
if(!error)
for(unsigned int i=0;i<node->children.size();i++)
output.push_back(node->children[i]->data);
return output;
}
private:
class CompletionNode {
public:
CompletionNode(string new_data) {data=new_data;}
~CompletionNode() {for(unsigned int i=0;i<children.size();i++) delete children[i];}
string data;
vector<CompletionNode*> children;
};
CompletionNode *root;
};
#endif