fix save and load
This commit is contained in:
@@ -1,69 +1,18 @@
|
||||
#include"tree.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <assert.h>
|
||||
#include"tree.h"
|
||||
#include"person.h"
|
||||
#include"help.h"
|
||||
|
||||
using std::cout;
|
||||
using std::ofstream;
|
||||
using std::ifstream;
|
||||
using std::vector;
|
||||
|
||||
Tree Tree::operator+ (const Tree& other) const
|
||||
{
|
||||
/* Tree result(treeName + other.treeName); //The tree resulting from the addition */
|
||||
/* vector<person_id> newId; //The indexes members of the II tree will have in result */
|
||||
/* newId.reserve(other.numPeople); */
|
||||
|
||||
/* U32 idCounter = numPeople; */
|
||||
/* for (U32 i = 0, j; i < other.numPeople; ++i) //Searches for people present in both trees and gives them the same index in result */
|
||||
/* { */
|
||||
/* for (j = 0; j < numPeople; ++j) */
|
||||
/* { */
|
||||
/* if (other.people[i] == people[j]) */
|
||||
/* { */
|
||||
/* newId[i] = j; */
|
||||
/* break; */
|
||||
/* } */
|
||||
/* } */
|
||||
/* if (j >= numPeople) */
|
||||
/* newId[i] = ++idCounter; */
|
||||
/* } */
|
||||
/* result.numPeople = idCounter; //The number of members the resulting tree has */
|
||||
|
||||
/* result.people.reserve(result.numPeople); */
|
||||
/* result.people = people; //Adds the members of the I tree to the resulting tree */
|
||||
/* for (U32 i=0; i < other.numPeople; ++i) //Adds the members of the II tree to the resulting tree */
|
||||
/* { */
|
||||
/* if (newId[i] >= numPeople) */
|
||||
/* result.people.push_back(other.people[i]); */
|
||||
/* } */
|
||||
|
||||
/* result.relations.reserve(result.numPeople); */
|
||||
/* result.relations = relations; */
|
||||
|
||||
/* for (U32 i=0; i < other.numPeople; ++i) //Adds the relatives from the II tree */
|
||||
/* { */
|
||||
/* if (newId[i] >= numPeople) //If the member is found in the II tree but not in the I */
|
||||
/* { */
|
||||
/* result.relations.push_back(other.relations[i]); */
|
||||
/* } */
|
||||
/* else //If he's found in both */
|
||||
/* { */
|
||||
/* for (person_id j = 0; j < other.relations[i].size(); ++j) //Adds the number of relatives from the II to the number in the I */
|
||||
/* { */
|
||||
/* if (newId[other.relations[i][j].id] > numPeople) */
|
||||
/* ++result.people[newId[i]].numRel_; */
|
||||
/* } */
|
||||
|
||||
/* result.relations[newId[i]].reserve(result.people[newId[i]].numRel_); */
|
||||
|
||||
/* result.relations[newId[i]] = relations[newId[i]]; */
|
||||
|
||||
/* result.relations[newId[i]] += other.relations[i]; //The relatives from the II tree */
|
||||
/* } */
|
||||
/* } */
|
||||
Tree result(*this);
|
||||
|
||||
return result += other;
|
||||
}
|
||||
|
||||
@@ -102,12 +51,12 @@ Tree& Tree::operator+=(const Tree& other)
|
||||
relations.push_back(other.relations[i]);
|
||||
for (Relation& rel: relations.back())
|
||||
rel.id = newId[rel.id];
|
||||
/* for (person_id j = 0; j < other.people[i].numRel_; ++j) */
|
||||
/* for (person_id j = 0; j < other.relations[i].size(); ++j) */
|
||||
/* relations[newId[i]][j].id = newId[ relations[newId[i]][j].id ]; */
|
||||
}
|
||||
else //if present in both trees
|
||||
{
|
||||
/* for (person_id j = 0; j < other.people[i].numRel_; ++j) //combine relations */
|
||||
/* for (person_id j = 0; j < other.relations[i].size(); ++j) //combine relations */
|
||||
/* { */
|
||||
/* if (newId[other.relations[i][j].id] >= numPeople) */
|
||||
/* ++people[newId[i]].numRel_; */
|
||||
@@ -124,11 +73,6 @@ Tree& Tree::operator+=(const Tree& other)
|
||||
}
|
||||
}
|
||||
|
||||
/* for (person_id j = 0; j < other.people[i].numRel_; ++j) */
|
||||
/* { */
|
||||
/* relations[newId[i]][j + people[newId[i]].numRel_ - other.people[i].numRel_].id = newId[other.relations[i][j].id]; */
|
||||
/* relations[newId[i]][j + people[newId[i]].numRel_ - other.people[i].numRel_].type = other.relations[i][j].type; */
|
||||
/* } */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,30 +98,28 @@ Tree& Tree::operator-= (const Tree& other)
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Tree::rename(const char* newName)
|
||||
{
|
||||
treeName = newName;
|
||||
}
|
||||
|
||||
person_id Tree::findId(const char* name, const short year, const unsigned char month, const unsigned char day) const
|
||||
person_id Tree::findId(const char* name, bool m) const
|
||||
{
|
||||
//TODO
|
||||
//Person sought(name, day, month, year,)
|
||||
Person sought(name, {}, m);
|
||||
for (person_id i = 0; i < numPeople; ++i)
|
||||
{
|
||||
if (name == people[i].name_) //&& (year == people[i].year || !year || !people[i].year) && (month == people[i].month || !month || !people[i].month) && (day == people[i].day || !day || people[i].day))
|
||||
if (sought == people[i])
|
||||
return i;
|
||||
}
|
||||
return Nobody;
|
||||
}
|
||||
|
||||
person_id Tree::findOldestAncestor(unsigned id, bool isMale)const
|
||||
person_id Tree::findOldestAncestor(person_id id, bool isMale) const
|
||||
{
|
||||
for (unsigned i = 0; i < people[id].numRel_; ++i)
|
||||
for (const Relation& rel: relations[id])
|
||||
{
|
||||
if (relations[id][i].type == Child && people[ relations[id][i].id ].isMale_ == isMale)
|
||||
return findOldestAncestor(relations[id][i].id, isMale);
|
||||
if (rel.type == Child && people[ rel.id ].isMale() == isMale)
|
||||
return findOldestAncestor(rel.id, isMale);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
@@ -193,9 +135,9 @@ person_id Tree::findCommonAncestor(const unsigned firstId, const unsigned second
|
||||
U16 oldestBday = SHRT_MAX;
|
||||
for (person_id i = 0; i < numPeople; ++i)
|
||||
{
|
||||
if (visited[i] == 2 && people[i].birth_.year < oldestBday)
|
||||
if (visited[i] == 2 && people[i].birth().year < oldestBday)
|
||||
{
|
||||
oldestBday = people[i].birth_.year;
|
||||
oldestBday = people[i].birth().year;
|
||||
oldest = i;
|
||||
}
|
||||
}
|
||||
@@ -207,7 +149,7 @@ person_id Tree::findParent(person_id child, bool isParentMale) const
|
||||
person_id parent = Nobody;
|
||||
for(const Relation& rel: relations[child])
|
||||
{
|
||||
if (rel.type == Child && people[rel.id].isMale())
|
||||
if (rel.type == Child && people[rel.id].isMale() == isParentMale)
|
||||
parent = rel.id;
|
||||
}
|
||||
return parent;
|
||||
@@ -250,52 +192,42 @@ RelType Tree::findRelation(person_id first, person_id second) const
|
||||
return None;
|
||||
}
|
||||
|
||||
void Tree::saveTree() const
|
||||
void Tree::saveToFile() const
|
||||
{
|
||||
std::ofstream peopleFile(treeName+ ".people", std::ios::binary);
|
||||
ofstream file(treeName + ".famt", std::ios::binary);
|
||||
|
||||
peopleFile.write(reinterpret_cast<const char*>(&numPeople), sizeof(numPeople));
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
people[i].save(peopleFile);
|
||||
peopleFile.close();
|
||||
file.write((const char*)(&numPeople), sizeof(numPeople));
|
||||
for (const Person& p: people)
|
||||
p.write(file);
|
||||
|
||||
std::ofstream relationFile(treeName + ".relations", std::ios::binary);
|
||||
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
for (auto& rels: relations)
|
||||
{
|
||||
//relationFile.write((char*)&people[i].numRel_, sizeof(people[i].numRel_));
|
||||
for (unsigned j = 0; j < people[i].numRel_; ++j)
|
||||
relationFile.write(reinterpret_cast<const char*>(&relations[i][j]), sizeof(relations[i][j]));
|
||||
U32 numRels = rels.size();
|
||||
file.write((const char*)(& numRels), sizeof(numRels));
|
||||
file.write((const char*)(rels.data()), sizeof(Relation) * numRels);
|
||||
}
|
||||
relationFile.close();
|
||||
}
|
||||
|
||||
bool Tree::loadTree()
|
||||
bool Tree::loadFromFile()
|
||||
{
|
||||
std::ifstream peopleFile(treeName + ".people", std::ios::binary);
|
||||
if (peopleFile.peek() == std::char_traits<char>::eof())
|
||||
{
|
||||
peopleFile.close();
|
||||
return 0;
|
||||
}
|
||||
ifstream file(treeName + ".famt", std::ios::binary);
|
||||
|
||||
file.read((char*)(&numPeople), sizeof(numPeople));
|
||||
|
||||
peopleFile.read(reinterpret_cast<char*>(&numPeople), sizeof(numPeople));
|
||||
people.resize(numPeople);
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
people[i].load(peopleFile);
|
||||
peopleFile.close();
|
||||
for (Person& p: people)
|
||||
p.read(file);
|
||||
|
||||
|
||||
std::ifstream relationFile(treeName + ".relations", std::ios::binary);
|
||||
relations.resize(numPeople);
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
for (auto& rels: relations)
|
||||
{
|
||||
relations[i].resize(people[i].numRel_);
|
||||
for (unsigned j = 0; j < people[i].numRel_; ++j)
|
||||
relationFile.read(reinterpret_cast<char*>(&relations[i][j]), sizeof(relations[i][j]));
|
||||
U32 numRels;
|
||||
file.read((char*)(& numRels), sizeof(numRels));
|
||||
|
||||
rels.resize(numRels);
|
||||
file.read((char*)(rels.data()), sizeof(Relation) * numRels);
|
||||
}
|
||||
relationFile.close();
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Tree::addPerson(const char* name, bool isMale, unsigned father, unsigned mother, EventTime birth, EventTime death)
|
||||
@@ -320,10 +252,10 @@ bool Tree::addRelation(const char* firstName, const RelType type, const char* se
|
||||
bool Tree::addRelation(const unsigned firstId, const RelType type, const unsigned secondId, bool opposite)//
|
||||
{
|
||||
if (firstId == Nobody || firstId >= numPeople || secondId == Nobody || secondId >= numPeople || type == None)
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
unsigned i = 0;
|
||||
for (; i < people[firstId].numRel_; ++i) //If they are already relatives
|
||||
for (; i < relations[firstId].size(); ++i) //If they are already relatives
|
||||
{
|
||||
if (relations[firstId][i].id == secondId)
|
||||
{
|
||||
@@ -331,10 +263,9 @@ bool Tree::addRelation(const unsigned firstId, const RelType type, const unsigne
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= people[firstId].numRel_)
|
||||
if (i >= relations[firstId].size())
|
||||
{
|
||||
relations[firstId].push_back({secondId, type});
|
||||
++people[firstId].numRel_;
|
||||
}
|
||||
|
||||
if (!opposite)
|
||||
@@ -356,7 +287,7 @@ bool Tree::addRelation(const unsigned firstId, const RelType type, const unsigne
|
||||
default: assert(!"Unexpected case"); break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Tree::removeRelation(const char* firstName, const char* secondName)
|
||||
@@ -379,10 +310,10 @@ void Tree::removePerson(const person_id id)
|
||||
}
|
||||
}
|
||||
}
|
||||
for (unsigned i = 0; i < people[numPeople - 2].numRel_; ++i) //Move the last in his place and update IDs
|
||||
for (unsigned i = 0; i < relations[numPeople - 2].size(); ++i) //Move the last in his place and update IDs
|
||||
{
|
||||
relId = relations[numPeople - 1][i].id;
|
||||
for (unsigned j = 0; j < people[relId].numRel_; ++j)
|
||||
for (unsigned j = 0; j < relations[relId].size(); ++j)
|
||||
{
|
||||
if (relations[relId][j].id == numPeople - 1)
|
||||
{
|
||||
@@ -396,10 +327,10 @@ void Tree::removePerson(const person_id id)
|
||||
--numPeople;
|
||||
}
|
||||
|
||||
void Tree::removePerson(const char* personName, short year, unsigned char month, unsigned char day)
|
||||
{
|
||||
removePerson(findId(personName, year, month, day));
|
||||
}
|
||||
/* void Tree::removePerson(const char* personName, short year, unsigned char month, unsigned char day) */
|
||||
/* { */
|
||||
/* removePerson(findId(personName, year, month, day)); */
|
||||
/* } */
|
||||
|
||||
void Tree::removeRelation(const unsigned firstId, const unsigned secondId)
|
||||
{
|
||||
@@ -492,8 +423,8 @@ void Tree::print() const
|
||||
member.displayInfo();
|
||||
}
|
||||
|
||||
void Tree::printRel(const unsigned id)const
|
||||
{
|
||||
/* void Tree::printRel(const unsigned id)const */
|
||||
/* { */
|
||||
/* cout << people[id].name_; */
|
||||
/* if (people[id].numRel_ == 0) */
|
||||
/* cout << " has no known relatives"; */
|
||||
@@ -541,7 +472,7 @@ void Tree::printRel(const unsigned id)const
|
||||
/* } */
|
||||
/* cout << '\n'; */
|
||||
/* } */
|
||||
}
|
||||
/* } */
|
||||
|
||||
void Tree::printMember(const unsigned id)const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user