Working on a project where I have to return immediate children of queried line.A query can either be the first or second property. The input file is the output of pre-order traversal in an n-array tree. Each line can be thought of as a vertex in a n -array tree. How can this be improved performance-wise. Format of input file:
Level|Property1|Property2
1|AAAAAAAAAAAAAA|BBBBBBBBBBBBBBBBBBB
2|VVVVVVVVVVVVVV|fdgvjds sdbsks sdds
3|3784bbubsd@kjn|ejijo eurugvie
3|wiuefhiewbf844|weubwfbwe ewj
4|yyyyyyyyyyyyyy|wjfb wufwwfw
4|uuuuuuuuuuuuuu|wewb wrwrw
4|tttttttttttttt|webi wewewef
2|qqqqqqqqqqqqqq|yuweuw wsdjsods
If i query for AAAAAAAAAAAAAA, I get -> 2|VVVVVVVVVVVVVV|fdgvjds sdbsks sdds
If i query for fdgvjds sdbsks sdds, I get -> 3|3784bbubsd@kjn|ejijo eurugvie
3|wiuefhiewbf844|weubwfbwe ewj
If i query for wiuefhiewbf844, I get -> 4|yyyyyyyyyyyyyy|wjfb wufwwfw
4|uuuuuuuuuuuuuu|wewb wrwrw
4|tttttttttttttt|webi wewewef
If i query for qqqqqqqqqqqqqq, I get -> NIL
The property Level, is what determines immediate children. This is the code I used. I have only inserted the constructor of my class. Any variables not initialized in it are defined as private. I am using a vector output to store the result.
Reader(const char *Filename,string query)
{
ifstream in(Filename, ios::in | ios::binary);
string contents;
in.seekg(0, ios::end);
contents.resize(in.tellg());
in.seekg(0, ios::beg);
in.read(&contents[0], contents.size());
in.close();
istringstream ss(contents);
string line;
int flag = 0,searchLevel;
while(getline(ss,line,'\n'))
{
istringstream ss(line);
string token, Property1, Property2;
getline(ss, token, '|');
int level = convertToInteger(token);
int i = 1;
while (getline(ss, token, '|'))
{
if (i == 1)Property1 = token;
if (i == 2)
{
Property2 = token;
break;
}
i++;
}
if (!flag)
{
if (query == Property1 || query == Property2)
{
flag = 1;
searchLevel = level;
}
}
else if (level <= searchLevel)break;
else if (searchLevel+1 == level)
output.push_back(line);
}
}
How can I improve speed of my code?