fix parset issues

This commit is contained in:
vrd
2023-02-25 11:57:15 +02:00
parent e626470f71
commit 176ca1c85a
6 changed files with 65 additions and 50 deletions
+20 -13
View File
@@ -99,7 +99,7 @@ namespace /* internal */
sign = -1; sign = -1;
break; break;
case '0' ... '9': case '0' ... '9':
val = val*10 + *it_data; val = val*10 + (*it_data - '0');
break; break;
case '.': case '.':
if(val == 0) if(val == 0)
@@ -177,8 +177,13 @@ namespace /* internal */
switch(*it_data) switch(*it_data)
{ {
case ',': case ',':
res.push_back( cur_tree->findId(name.data()) ); if(!name.empty())
name.clear(); {
res.push_back( cur_tree->findId(name.data()) );
name.clear();
}
else
unexpected_char(',');
break; break;
case '\n': case '\n':
++line; ++line;
@@ -188,7 +193,9 @@ namespace /* internal */
name.push_back(*it_data); name.push_back(*it_data);
} }
} }
/* return res;todo */ if(!name.empty())
res.push_back( cur_tree->findId(name.data()) );
return res;
} }
void state_neutral() void state_neutral()
@@ -216,7 +223,7 @@ namespace /* internal */
{ {
char* name_start = it_data; char* name_start = it_data;
pair<person_id, person_id> parents(Nobody, Nobody); pair<person_id, person_id> parents(Nobody, Nobody);
person_id spouse; vector<person_id> spouses;
EventTime birth{}; EventTime birth{};
EventTime death{}; EventTime death{};
@@ -232,7 +239,8 @@ namespace /* internal */
{ {
case '\n': case '\n':
++line; ++line;
++it_data; break;
default:
if(it_data[1] == ':') /* todo */ if(it_data[1] == ':') /* todo */
{ {
switch(*it_data) switch(*it_data)
@@ -251,7 +259,7 @@ namespace /* internal */
break; break;
case 's': case 's':
it_data += 2; it_data += 2;
parseSpouses(); spouses = parseSpouses();
break; break;
default: default:
unexpected_char(it_data[-1]); unexpected_char(it_data[-1]);
@@ -261,17 +269,17 @@ namespace /* internal */
else else
{ {
cur_tree->addPerson(name.c_str(), sex, parents.first, parents.second, birth, death); cur_tree->addPerson(name.c_str(), sex, parents.first, parents.second, birth, death);
for(person_id spouse: spouses)
cur_tree->addRelation(spouse, Spouse, cur_tree->last());
--it_data; /* rewind 1 */
return; return;
} }
break; break;
/* case '#': */
/* state = state_comment; */
/* return; */
/* case 0...9: */ /* case 0...9: */
/* case ':': */ /* case ':': */
/* unexpected_char(*it_data); */ /* unexpected_char(*it_data); */
default: /* default: */
break; /* break; */
} }
} }
} }
@@ -308,7 +316,6 @@ namespace /* internal */
Tree parseTftFile(MappedFile file) Tree parseTftFile(MappedFile file)
{ {
Tree result("name"); Tree result("name");
cur_tree = &result; cur_tree = &result;
+1 -1
View File
@@ -63,7 +63,7 @@ void EventTime::print() const
Person::Person(const char* aName, EventTime birth, Sex aSex, EventTime death) Person::Person(const char* aName, EventTime birth, Sex aSex, EventTime death)
: name(aName) : name(aName)
, birth_(birth) , birth_(birth)
, sex(sex) , sex(aSex)
, death_(death) , death_(death)
{ {
} }
+6 -6
View File
@@ -15,14 +15,14 @@ int main(int argc, char* argv[])
parseTftFile(f).display(); parseTftFile(f).display();
Tree test1("Dechkovi"); Tree test1("Dechkovi");
test1.loadFromFile(); /* test1.loadFromFile(); */
/* test1.addPerson("Rumen", M, Nobody, Nobody, {1968, 8, 9}); */ test1.addPerson("Rumen", M, Nobody, Nobody, {1968, 8, 9});
/* test1.addPerson("Nadejda", F, Nobody, Nobody, {1972, 12, 15}); */ test1.addPerson("Nadejda", F, Nobody, Nobody, {1972, 12, 15});
/* test1.addRelation(0, Spouse, 1); */ test1.addRelation(0, Spouse, 1);
/* test1.addPerson("Venelin", M, 0, 1, {1998, 6, 9}); */ test1.addPerson("Venelin", M, 0, 1, {1998, 6, 9});
/* test1.addPerson("Vasil", M, 0, 1, {2001, 1, 16}); */ test1.addPerson("Vasil", M, 0, 1, {2001, 1, 16});
/* test1.saveToFile(); */ /* test1.saveToFile(); */
Tree test2("Karamanovi"); Tree test2("Karamanovi");
+33 -28
View File
@@ -18,12 +18,13 @@ Tree Tree::operator+ (const Tree& other) const
Tree& Tree::operator+=(const Tree& other) Tree& Tree::operator+=(const Tree& other)
{ {
vector<person_id> newId; //The indexes people from II tree will have in I tree vector<person_id> newId; //The indexes people from II tree will have in I tree
newId.reserve(other.numPeople); newId.reserve(other.people.size());
U32 idCounter = numPeople; U32 initNum = people.size(); /* number of people at the start */
for (person_id i = 0, j; i < other.numPeople; ++i) //find the new ids for people in II, people present in both trees get the ids from I U32 idCounter = initNum;
for (person_id i = 0, j; i < other.people.size(); ++i) //find the new ids for people in II, people present in both trees get the ids from I
{ {
for (j = 0; j < numPeople; ++j) for (j = 0; j < initNum; ++j)
{ {
if (other.people[i] == people[j]) if (other.people[i] == people[j])
{ {
@@ -31,21 +32,21 @@ Tree& Tree::operator+=(const Tree& other)
break; break;
} }
} }
if (j >= numPeople) /* not present in I */ if (j >= initNum) /* not present in I */
newId[i] = idCounter++; newId[i] = idCounter++;
} }
people.reserve(idCounter); people.reserve(idCounter);
for (person_id i = 0; i < other.numPeople; ++i) /* add the personal data of II to I */ for (person_id i = 0; i < other.people.size(); ++i) /* add the personal data of II to I */
{ {
if (newId[i] >= numPeople) /* isnt already in I */ if (newId[i] >= initNum) /* isnt already in I */
people.push_back(other.people[i]); people.push_back(other.people[i]);
} }
relations.reserve(idCounter); relations.reserve(idCounter);
for (person_id i = 0; i < other.numPeople; ++i) //merge the relative data for (person_id i = 0; i < other.people.size(); ++i) //merge the relative data
{ {
if (newId[i] >= numPeople) /* isnt already in I */ if (newId[i] >= initNum) /* isnt already in I */
{ {
relations.push_back(other.relations[i]); relations.push_back(other.relations[i]);
for (Relation& rel: relations.back()) for (Relation& rel: relations.back())
@@ -57,7 +58,7 @@ Tree& Tree::operator+=(const Tree& other)
{ {
/* for (person_id j = 0; j < other.relations[i].size(); ++j) //combine relations */ /* for (person_id j = 0; j < other.relations[i].size(); ++j) //combine relations */
/* { */ /* { */
/* if (newId[other.relations[i][j].id] >= numPeople) */ /* if (newId[other.relations[i][j].id] >= initNum) */
/* ++people[newId[i]].numRel_; */ /* ++people[newId[i]].numRel_; */
/* } */ /* } */
person_id idIn1 = newId[i]; person_id idIn1 = newId[i];
@@ -65,7 +66,7 @@ Tree& Tree::operator+=(const Tree& other)
for (Relation rel: other.relations[i]) for (Relation rel: other.relations[i])
{ {
if(newId[rel.id] >= numPeople) /* not already in I */ if(newId[rel.id] >= initNum) /* not already in I */
relations[idIn1].push_back( {newId[rel.id], rel.type} ); relations[idIn1].push_back( {newId[rel.id], rel.type} );
else else
{ {
@@ -75,13 +76,12 @@ Tree& Tree::operator+=(const Tree& other)
} }
} }
numPeople = idCounter; //Number of members in I tree
return *this; return *this;
} }
Tree& Tree::operator-= (const Tree& other) Tree& Tree::operator-= (const Tree& other)
{ {
for (U32 i = 0; i < numPeople; ++i) for (U32 i = 0; i < people.size(); ++i)
{ {
for (const Person& othPers: other.people) for (const Person& othPers: other.people)
{ {
@@ -102,9 +102,14 @@ void Tree::rename(const char* newName)
treeName = newName; treeName = newName;
} }
person_id Tree::last()
{
return people.size() - 1;
}
person_id Tree::findId(const char* name) const person_id Tree::findId(const char* name) const
{ {
for (person_id i = 0; i < numPeople; ++i) for (person_id i = 0; i < people.size(); ++i)
{ {
if (name == people[i].name) if (name == people[i].name)
return i; return i;
@@ -115,7 +120,7 @@ person_id Tree::findId(const char* name) const
person_id Tree::findId(const char* name, Sex s) const person_id Tree::findId(const char* name, Sex s) const
{ {
Person sought(name, {}, s); Person sought(name, {}, s);
for (person_id i = 0; i < numPeople; ++i) for (person_id i = 0; i < people.size(); ++i)
{ {
if (sought == people[i]) if (sought == people[i])
return i; return i;
@@ -135,14 +140,14 @@ person_id Tree::findOldestAncestor(person_id id, Sex sex) const
person_id Tree::findCommonAncestor(const unsigned first, const unsigned second) const person_id Tree::findCommonAncestor(const unsigned first, const unsigned second) const
{ {
vector<U8> visited(numPeople); //if a member is visited twice he's a common ancestor vector<U8> visited(people.size()); //if a member is visited twice he's a common ancestor
commonAncestorRec(first, visited); commonAncestorRec(first, visited);
commonAncestorRec(second, visited); commonAncestorRec(second, visited);
person_id oldest = Nobody; person_id oldest = Nobody;
U16 oldestBday = SHRT_MAX; U16 oldestBday = SHRT_MAX;
for (person_id i = 0; i < numPeople; ++i) for (person_id i = 0; i < people.size(); ++i)
{ {
if (visited[i] == 2 && people[i].birth().year < oldestBday) if (visited[i] == 2 && people[i].birth().year < oldestBday)
{ {
@@ -225,7 +230,8 @@ void Tree::saveToFile() const
ofstream file(tftDir + '/' + treeName + ".tftb", std::ios::binary); ofstream file(tftDir + '/' + treeName + ".tftb", std::ios::binary);
file.write((const char*)(&numPeople), sizeof(numPeople)); U32 numPeople = people.size();
file.write((const char*)(&numPeople), sizeof(people.size()));
for (const Person& p: people) for (const Person& p: people)
p.write(file); p.write(file);
@@ -245,13 +251,14 @@ bool Tree::loadFromFile()
ifstream file(tftDir + '/' + treeName + ".tftb", std::ios::binary); ifstream file(tftDir + '/' + treeName + ".tftb", std::ios::binary);
file.read((char*)(&numPeople), sizeof(numPeople)); U32 numPeople;
file.read((char*)(&numPeople), sizeof(people.size()));
people.resize(numPeople); people.resize(people.size());
for (Person& p: people) for (Person& p: people)
p.read(file); p.read(file);
relations.resize(numPeople); relations.resize(people.size());
for (auto& rels: relations) for (auto& rels: relations)
{ {
U32 numRels; U32 numRels;
@@ -268,9 +275,8 @@ void Tree::addPerson(const char* name, Sex sex, unsigned father, unsigned mother
people.emplace_back(name, birth, sex, death); people.emplace_back(name, birth, sex, death);
relations.emplace_back(); /* relations of the new person */ relations.emplace_back(); /* relations of the new person */
++numPeople;
person_id new_person_id = numPeople - 1; person_id new_person_id = people.size() - 1;
addRelation(father, Parent, new_person_id); addRelation(father, Parent, new_person_id);
addRelation(mother, Parent, new_person_id); addRelation(mother, Parent, new_person_id);
@@ -285,7 +291,7 @@ bool Tree::addRelation(const char* firstName, const RelType type, const char* se
bool Tree::addRelation(const unsigned first, const RelType type, const unsigned second, bool update) bool Tree::addRelation(const unsigned first, const RelType type, const unsigned second, bool update)
{ {
if (first == Nobody || first >= numPeople || second == Nobody || second >= numPeople || type == None) if (first == Nobody || first >= people.size() || second == Nobody || second >= people.size() || type == None)
return false; return false;
switch (type) switch (type)
@@ -353,12 +359,12 @@ void Tree::removePerson(const person_id id)
} }
} }
} }
for (unsigned i = 0; i < relations[numPeople - 2].size(); ++i) //Move the last in his place and update IDs for (unsigned i = 0; i < relations[people.size() - 2].size(); ++i) //Move the last in his place and update IDs
{ {
relId = relations[numPeople - 1][i].id; relId = relations[people.size() - 1][i].id;
for (unsigned j = 0; j < relations[relId].size(); ++j) for (unsigned j = 0; j < relations[relId].size(); ++j)
{ {
if (relations[relId][j].id == numPeople - 1) if (relations[relId][j].id == people.size() - 1)
{ {
relations[relId][j].id = id; relations[relId][j].id = id;
break; break;
@@ -367,7 +373,6 @@ void Tree::removePerson(const person_id id)
} }
remove(relations, id); remove(relations, id);
remove(people, id); remove(people, id);
--numPeople;
} }
/* void Tree::removePerson(const char* personName, short year, unsigned char month, unsigned char day) */ /* void Tree::removePerson(const char* personName, short year, unsigned char month, unsigned char day) */
+3 -2
View File
@@ -44,11 +44,13 @@ public:
void rename(const char* newName); void rename(const char* newName);
person_id last();
person_id findId(const char* name) const; person_id findId(const char* name) const;
person_id findId(const char* name, Sex) const; person_id findId(const char* name, Sex) const;
person_id findOldestAncestor(person_id, Sex) const;// person_id findOldestAncestor(person_id, Sex) const;//
//oldest common ancestor ID, a person is considerd an ancestor of himself //oldest common ancestor ID, a person is considerd an ancestor of himself
person_id findCommonAncestor(person_id first, person_id second) const; person_id findCommonAncestor(person_id first, person_id second) const; /* needs to be fixed, never return nobody */
RelType findRelation(person_id first, person_id second) const; RelType findRelation(person_id first, person_id second) const;
person_id findParent(person_id child, Sex parentSex) const; person_id findParent(person_id child, Sex parentSex) const;
person_id findSpouse(person_id person) const; person_id findSpouse(person_id person) const;
@@ -97,7 +99,6 @@ private:
void selectRightSib(person_id); void selectRightSib(person_id);
std::string treeName; std::string treeName;
unsigned numPeople = 0;
std::vector<Person> people; //The data for each member std::vector<Person> people; //The data for each member
std::vector<std::vector<Relation>> relations; //The relatives of each member std::vector<std::vector<Relation>> relations; //The relatives of each member
+2
View File
@@ -1,9 +1,11 @@
open tree open tree
Parser: Parser:
- clean wh - clean wh
--help --help
help - lowercase
findRelative findRelative