include include search search include include dprint dprint bool valid

  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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "../../include/search/search.h"
#include "../../include/dprint/dprint.h"
bool validatePattern(Unistring *pattern)
{
return (pattern->size() >= MIN_PATTERN_LENGTH);
}
Unistring *preProcessPattern(Unistring *pattern)
{
auto newPattern = new Unistring();
bool firstStar = false;
for(auto i:(*pattern))
{
if (i == unichar('*'))
{
if (!firstStar)
{
newPattern->append(i);
firstStar = true;
}
} else
{
newPattern->append(i);
firstStar = false;
}
}
return newPattern;
}
int findMaxCommonPrefix(Unistring data, Unistring pattern)
{
DBG("findMaxCommonPrefix for data and pattern");
data.print_as_int();
pattern.print_as_int();
DBG("MaxCommonPrefix is");
for(int i = 0; i < data.size(); i++)
{
if ((i == pattern.size()) || ((data)[i] != (pattern)[i]))
{
printf("ret %d\n",i);
DBG("");
return i;
}
}
printf("ret %ld\n",data.size());
DBG("");
return data.size();
}
void merge2Sets(std::set<int> *newSet, std::set<int> *appendSet)
{
DBG("merge2Sets");
DBG("first set:");
DBG_S(newSet);
DBG("append set:");
DBG_S(appendSet);
DBG("");
for(auto i:(*appendSet))
newSet->insert(i);
}
void TreeVertex::findDownNodes(std::set<int> *wordSet)
{
DBG("findDownNodes from Node");
DBG_N(this);
DBG("");
if (this->children.empty())
{
wordSet->insert(this->value);
} else
{
for(auto i:this->children)
i.second->findDownNodes(wordSet);
}
}
void TreeVertex::searchWordsFromNode(std::set<int> *wordSet, Unistring pattern)
{
DBG("searchWordsFromNode");
DBG_N(this);
DBG("pattern:");
pattern.print_as_int();
DBG("");
if (this->children.empty() && pattern.size() == 0)
{
DBG("found in leaf");
wordSet->insert(this->value);
return;
}
if (!(this->children.empty()) && ((pattern.size() == 0) || (pattern.size() == 1 && (pattern)[0] == '*')))
{
DBG("found not in leaf");
auto childNodesResultSet = new std::set<int>();
this->findDownNodes(childNodesResultSet);
merge2Sets(wordSet, childNodesResultSet);
return;
}
unichar curChar = (pattern)[0];
if (curChar == unichar('*'))
{
auto newSet = new std::set<int>;
this->searchWordsFromNode(newSet, pattern.substr(1));
merge2Sets(wordSet, newSet);
if (this->data.size() == 0 && !this->children.empty())
{
for(auto i:this->children)
{
auto newSet = new std::set<int>;
i.second->searchWordsFromNode(newSet, pattern);
merge2Sets(wordSet, newSet);
return;
}
} else
{
auto nextNode = new TreeVertex(this->data.substr(1), this->value, this->children);
auto newSet = new std::set<int>;
nextNode->searchWordsFromNode(newSet, pattern);
merge2Sets(wordSet, newSet);
return;
}
} else
{
int commonPrefixSize;
commonPrefixSize = findMaxCommonPrefix(this->data, pattern);
if (commonPrefixSize == this->data.size())
{
auto newSet = new std::set<int>;
if ((pattern.substr(commonPrefixSize).size() != 0) && (this->children.count((pattern.substr(commonPrefixSize))[0]) !=0))
{
TreeVertex *nextNode = this->children[(pattern.substr(commonPrefixSize))[0]];
nextNode->searchWordsFromNode(newSet, pattern.substr(commonPrefixSize));
} else
{
if (this->children.empty())
{
newSet->insert(this->value);
} else
{
//паттерн нулевой или из звездочки
if((pattern.substr(commonPrefixSize).size() == 0) || ((pattern.substr(commonPrefixSize).size() == 1 && (pattern)[0] == '*')))
{
auto childNodesResultSet = new std::set<int>();
this->findDownNodes(childNodesResultSet);
merge2Sets(newSet, childNodesResultSet);
} else
{
std::cout << "not found1" << std::endl;
}
}
}
merge2Sets(wordSet, newSet);
return;
} else
{
if (commonPrefixSize)
{
auto nextNode = new TreeVertex(this->data.substr(commonPrefixSize),this->value, this->children);
auto newSet = new std::set<int>;
nextNode->searchWordsFromNode(newSet, pattern.substr(commonPrefixSize));
merge2Sets(wordSet, newSet);
return;
} else
{
std::cout << "not found2" << std::endl;
}
}
}
return;
}
std::set<int> *SuffixTree::searchWordsFromSuffixTree(Unistring pattern)
{
auto finalSet = new std::set<int>;
if ((pattern)[0] == unichar('*'))
{
std::cout << "first symbol in pattern is star" << std::endl;
TreeVertex *callNode = root->children[(pattern)[1]];
if (callNode != nullptr)
{
callNode->searchWordsFromNode(finalSet, pattern.substr(1));
}
} else
{
std::cout << "first symbol in pattern is not star" << std::endl;
TreeVertex *callNode = root->children[(pattern)[0]];
if (callNode != nullptr)
{
callNode->searchWordsFromNode(finalSet, pattern);
}
}
return finalSet;
}