| 1 |
#ifndef KEYWORDS_CLASS_H |
|---|
| 2 |
#define KEYWORDS_CLASS_H |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
class KeywordsClass |
|---|
| 6 |
{ |
|---|
| 7 |
|
|---|
| 8 |
public: |
|---|
| 9 |
vector<string> keywords; |
|---|
| 10 |
KeywordsClass(); |
|---|
| 11 |
|
|---|
| 12 |
void rootSearch(const string& root, vector<string>& matches ) |
|---|
| 13 |
{ |
|---|
| 14 |
|
|---|
| 15 |
if ( keywords.size() ) |
|---|
| 16 |
{ |
|---|
| 17 |
rootSearchR(0,keywords.size() - 1,root, matches); |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
std::sort(matches.begin(),matches.end()); |
|---|
| 22 |
|
|---|
| 23 |
} |
|---|
| 24 |
private: |
|---|
| 25 |
|
|---|
| 26 |
void rootSearchR (int l, int r, const string& root, vector<string>& matches) { // return a vector of matches |
|---|
| 27 |
|
|---|
| 28 |
if ( l > r ) { |
|---|
| 29 |
return; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
int m = (l +r ) / 2; |
|---|
| 33 |
|
|---|
| 34 |
if (keywords[m].length() >= root.length() ) { |
|---|
| 35 |
|
|---|
| 36 |
if ( keywords[m].substr(0,root.length() ) == root ) { |
|---|
| 37 |
|
|---|
| 38 |
matches.push_back(keywords[m]); |
|---|
| 39 |
int temp = m - 1; |
|---|
| 40 |
|
|---|
| 41 |
if (temp >= 0 ) { |
|---|
| 42 |
bool isMatched = true; |
|---|
| 43 |
while (isMatched) { |
|---|
| 44 |
isMatched = false; |
|---|
| 45 |
|
|---|
| 46 |
if ( keywords[temp].length() >= root.length() ) { |
|---|
| 47 |
|
|---|
| 48 |
if ( keywords[temp].substr(0,root.length() ) == root) { |
|---|
| 49 |
matches.push_back(keywords[temp]); |
|---|
| 50 |
isMatched = true; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
temp--; |
|---|
| 56 |
if ( temp < 0 ) break; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
} |
|---|
| 60 |
temp = m + 1; |
|---|
| 61 |
if ( temp < keywords.size() ) { |
|---|
| 62 |
|
|---|
| 63 |
bool isMatched = true; |
|---|
| 64 |
while (isMatched) { |
|---|
| 65 |
|
|---|
| 66 |
isMatched = false; |
|---|
| 67 |
|
|---|
| 68 |
if ( keywords[temp].length() >= root.length() ) { |
|---|
| 69 |
|
|---|
| 70 |
if ( keywords[temp].substr(0,root.length() ) == root) { |
|---|
| 71 |
// printf("\nMatch for %s \n",keywords[temp].substr(0,root.length() ) .c_str() ); |
|---|
| 72 |
matches.push_back(keywords[temp]); |
|---|
| 73 |
isMatched = true; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
temp++; |
|---|
| 79 |
if ( temp >= keywords.size() ) break; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
return; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
if (root < keywords[m]) |
|---|
| 91 |
rootSearchR(l,m-1,root,matches); |
|---|
| 92 |
else |
|---|
| 93 |
rootSearchR(m+1,r,root,matches); |
|---|
| 94 |
|
|---|
| 95 |
return; |
|---|
| 96 |
|
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
}; |
|---|
| 107 |
|
|---|
| 108 |
#endif |
|---|