drop array
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
#include"tree.h"
|
||||
#include"person.h"
|
||||
#include"help.h"
|
||||
|
||||
using std::cout;
|
||||
|
||||
using std::vector;
|
||||
|
||||
Tree Tree::operator+ (const Tree& other)//
|
||||
{
|
||||
Tree result(treeName + other.treeName); //The tree resulting from the addition
|
||||
unsigned *newId = new unsigned[other.numPeople]; //The indexes members of the II tree will have in result
|
||||
vector<person_id> newId; //The indexes members of the II tree will have in result
|
||||
newId.reserve(other.numPeople);
|
||||
|
||||
unsigned idCounter = numPeople;
|
||||
for (unsigned i = 0, j; i < other.numPeople; ++i) //Searches for people present in both trees and gives them the same index in result
|
||||
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)
|
||||
{
|
||||
@@ -30,22 +30,22 @@ Tree Tree::operator+ (const Tree& other)//
|
||||
}
|
||||
result.numPeople = idCounter; //The number of members the resulting tree has
|
||||
|
||||
result.people.resize(result.numPeople);
|
||||
result.people.reserve(result.numPeople);
|
||||
result.people = people; //Adds the members of the I tree to the resulting tree
|
||||
for (unsigned i=0; i < other.numPeople; ++i) //Adds the members of the II 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(other.people[i]);
|
||||
result.people.push_back(other.people[i]);
|
||||
}
|
||||
|
||||
result.relations.resize(result.numPeople);
|
||||
result.relations.reserve(result.numPeople);
|
||||
result.relations = relations;
|
||||
|
||||
for (unsigned i=0; i < other.numPeople; ++i) //Adds the relatives from the II tree
|
||||
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(other.relations[i]);
|
||||
result.relations.push_back(other.relations[i]);
|
||||
}
|
||||
else //If he's found in both
|
||||
{
|
||||
@@ -55,7 +55,7 @@ Tree Tree::operator+ (const Tree& other)//
|
||||
++result.people[newId[i]].numRel_;
|
||||
}
|
||||
|
||||
result.relations[newId[i]].resize(result.people[newId[i]].numRel_);
|
||||
result.relations[newId[i]].reserve(result.people[newId[i]].numRel_);
|
||||
|
||||
result.relations[newId[i]] = relations[newId[i]];
|
||||
|
||||
@@ -63,16 +63,16 @@ Tree Tree::operator+ (const Tree& other)//
|
||||
}
|
||||
}
|
||||
|
||||
delete[]newId;
|
||||
return result;
|
||||
}
|
||||
|
||||
Tree& Tree::operator+=(const Tree& other)
|
||||
{
|
||||
unsigned *newId = new unsigned[other.numPeople]; //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);
|
||||
|
||||
unsigned idCounter = numPeople;
|
||||
for (unsigned i = 0, j; i < other.numPeople; ++i) //Searches for people present in both trees and gives them the same index in I
|
||||
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 I
|
||||
{
|
||||
for (j = 0; j < numPeople; ++j)
|
||||
{
|
||||
@@ -86,32 +86,32 @@ Tree& Tree::operator+=(const Tree& other)
|
||||
newId[i] = idCounter++;
|
||||
}
|
||||
|
||||
people.resize(idCounter);
|
||||
for (unsigned i = 0; i < other.numPeople; ++i) //Adds the member personal data of II tree to I tree
|
||||
people.reserve(idCounter);
|
||||
for (U32 i = 0; i < other.numPeople; ++i) //Adds the member personal data of II tree to I tree
|
||||
{
|
||||
if (newId[i] >= numPeople)
|
||||
people.push(other.people[i]);
|
||||
people.push_back(other.people[i]);
|
||||
}
|
||||
|
||||
relations.resize(idCounter);
|
||||
for (unsigned i = 0; i < other.numPeople; ++i) //Merges the relative data
|
||||
relations.reserve(idCounter);
|
||||
for (U32 i = 0; i < other.numPeople; ++i) //Merges the relative data
|
||||
{
|
||||
if (newId[i] >= numPeople)
|
||||
{
|
||||
relations.push(other.relations[i]);
|
||||
for (unsigned j = 0; j < other.people[i].numRel_; ++j)
|
||||
relations.push_back(other.relations[i]);
|
||||
for (U32 j = 0; j < other.people[i].numRel_; ++j)
|
||||
relations[newId[i]][j].id = newId[ relations[newId[i]][j].id ];
|
||||
}
|
||||
else //If the member is present in both trees
|
||||
{
|
||||
for (unsigned j = 0; j < other.people[i].numRel_; ++j) //Adds the number of relatives from the II to the number in the I
|
||||
for (U32 j = 0; j < other.people[i].numRel_; ++j) //Adds the number of relatives from the II to the number in the I
|
||||
{
|
||||
if (newId[other.relations[i][j].id] >= numPeople)
|
||||
++people[newId[i]].numRel_;
|
||||
}
|
||||
|
||||
relations[newId[i]].resize(people[newId[i]].numRel_);
|
||||
for (unsigned j = 0; j < other.people[i].numRel_; ++j)
|
||||
relations[newId[i]].reserve(people[newId[i]].numRel_);
|
||||
for (U32 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;
|
||||
@@ -119,18 +119,17 @@ Tree& Tree::operator+=(const Tree& other)
|
||||
}
|
||||
}
|
||||
|
||||
delete[]newId;
|
||||
numPeople = idCounter; //Number of members in I tree
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tree& Tree::operator-= (const Tree& other)
|
||||
{
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
for (U32 i = 0; i < numPeople; ++i)
|
||||
{
|
||||
for (unsigned j = 0; j < other.numPeople; ++j)
|
||||
for (const Person& othPers: other.people)
|
||||
{
|
||||
if (people[i] == other.people[j])
|
||||
if (people[i] == othPers)
|
||||
{
|
||||
removePerson(i);
|
||||
--i;
|
||||
@@ -152,7 +151,7 @@ unsigned Tree::findId(const char* name, const short year, const unsigned char mo
|
||||
{
|
||||
//TODO
|
||||
//Person sought(name, day, month, year,)
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
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))
|
||||
return i;
|
||||
@@ -172,7 +171,7 @@ unsigned Tree::findOldestAncestor(unsigned id, bool isMale)const
|
||||
|
||||
unsigned Tree::findCommonAncestor(const unsigned firstId, const unsigned secondId)const
|
||||
{
|
||||
Array<char> visited(numPeople); //if a member is visited twice he's a common ancestor
|
||||
vector<char> visited(numPeople); //if a member is visited twice he's a common ancestor
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
visited[i] = 0;
|
||||
commonAncestorRec(firstId, visited);
|
||||
@@ -194,7 +193,7 @@ unsigned Tree::findCommonAncestor(const unsigned firstId, const unsigned secondI
|
||||
/* person_id Tree::findRelation(person_id first, person_id second) const */
|
||||
/* { */
|
||||
/* const auto& curRels = relations[first]; */
|
||||
/* for(auto i =0; i<curRels.gNum(); ++i) */
|
||||
/* for(auto i =0; i<curRels.size(); ++i) */
|
||||
/* { */
|
||||
/* if(curRels[i].id == second) */
|
||||
/* { */
|
||||
@@ -217,7 +216,7 @@ void Tree::saveTree() const
|
||||
{
|
||||
//relationFile.write((char*)&people[i].numRel_, sizeof(people[i].numRel_));
|
||||
for (unsigned j = 0; j < people[i].numRel_; ++j)
|
||||
relationFile.write(reinterpret_cast<char*>(&relations[i][j]), sizeof(relations[i][j]));
|
||||
relationFile.write(reinterpret_cast<const char*>(&relations[i][j]), sizeof(relations[i][j]));
|
||||
}
|
||||
relationFile.close();
|
||||
}
|
||||
@@ -235,7 +234,6 @@ bool Tree::loadTree()
|
||||
people.resize(numPeople);
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
people[i].load(peopleFile);
|
||||
people.sNum(numPeople);
|
||||
peopleFile.close();
|
||||
|
||||
|
||||
@@ -246,9 +244,7 @@ bool Tree::loadTree()
|
||||
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]));
|
||||
relations[i].sNum(people[i].numRel_);
|
||||
}
|
||||
relations.sNum(numPeople);
|
||||
relationFile.close();
|
||||
return 1;
|
||||
}
|
||||
@@ -256,10 +252,10 @@ bool Tree::loadTree()
|
||||
void Tree::addPerson(const char* name, bool isMale, unsigned father, unsigned mother, EventTime birth, EventTime death)
|
||||
{
|
||||
Person newPerson(name, birth, isMale, death);
|
||||
people.push(newPerson);
|
||||
people.push_back(newPerson);
|
||||
|
||||
Array<Relation> newRels;
|
||||
relations.push(newRels);
|
||||
vector<Relation> newRels;
|
||||
relations.push_back(newRels);
|
||||
++numPeople;
|
||||
|
||||
addRelation(father, Parent, numPeople - 1);
|
||||
@@ -288,7 +284,7 @@ bool Tree::addRelation(const unsigned firstId, const RelType type, const unsigne
|
||||
}
|
||||
if (i >= people[firstId].numRel_)
|
||||
{
|
||||
relations[firstId].push({secondId, type});
|
||||
relations[firstId].push_back({secondId, type});
|
||||
++people[firstId].numRel_;
|
||||
}
|
||||
|
||||
@@ -318,23 +314,22 @@ void Tree::removeRelation(const char* firstName, const char* secondName)
|
||||
removeRelation(findId(firstName), findId(secondName));
|
||||
}
|
||||
|
||||
void Tree::removePerson(const unsigned id)
|
||||
void Tree::removePerson(const person_id id)
|
||||
{
|
||||
unsigned relId; //Íoìåð íà òåêóùèÿ ðîäíèíàòà
|
||||
for (unsigned i = 0; i < people[id].numRel_; ++i) //Ïðåìàõâàò ñå âðúçêèòå íà ÷ëåíà çà ïðåìàõâàíå
|
||||
person_id relId; //Íoìåð íà òåêóùèÿ ðîäíèíàòà
|
||||
for (Relation rel: relations[id]) //Remove all of his relations
|
||||
{
|
||||
relId = relations[id][i].id;
|
||||
for (unsigned j = 0; j < people[relId].numRel_; ++j)
|
||||
relId = rel.id;
|
||||
for (U32 j = 0; j < relations[relId].size(); ++j)
|
||||
{
|
||||
if (relations[relId][j].id == id)
|
||||
{
|
||||
relations[relId].remove(j);
|
||||
remove(relations[relId], j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
--people[relId].numRel_;
|
||||
}
|
||||
for (unsigned i = 0; i < people[numPeople - 2].numRel_; ++i) //Ïîñëåäíèÿò ÷ëåí âçåìà íîìåðà íà ïðåìàõíàòèÿ
|
||||
for (unsigned i = 0; i < people[numPeople - 2].numRel_; ++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)
|
||||
@@ -346,8 +341,8 @@ void Tree::removePerson(const unsigned id)
|
||||
}
|
||||
}
|
||||
}
|
||||
relations.remove(id);
|
||||
people.remove(id);
|
||||
remove(relations, id);
|
||||
remove(people, id);
|
||||
--numPeople;
|
||||
}
|
||||
|
||||
@@ -358,21 +353,19 @@ void Tree::removePerson(const char* personName, short year, unsigned char month,
|
||||
|
||||
void Tree::removeRelation(const unsigned firstId, const unsigned secondId)
|
||||
{
|
||||
for (unsigned i = 0; i < people[firstId].numRel_; ++i)
|
||||
for (unsigned i = 0; i < relations[firstId].size(); ++i)
|
||||
{
|
||||
if (relations[firstId][i].id == secondId)
|
||||
{
|
||||
relations[firstId].remove(i);
|
||||
--people[firstId].numRel_;
|
||||
remove(relations[firstId], i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (unsigned i = 0; i < people[secondId].numRel_; ++i)
|
||||
for (unsigned i = 0; i < relations[secondId].size(); ++i)
|
||||
{
|
||||
if (relations[secondId][i].id == firstId)
|
||||
{
|
||||
relations[secondId].remove(i);
|
||||
--people[secondId].numRel_;
|
||||
remove(relations[secondId], i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -399,6 +392,16 @@ void Tree::display()
|
||||
case KEY_UP:
|
||||
selectUp();
|
||||
break;
|
||||
case 'j':
|
||||
selectDown();
|
||||
break;
|
||||
case 'k':
|
||||
selectUp();
|
||||
break;
|
||||
case 'i':
|
||||
people[selected_].print();
|
||||
draw();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -419,10 +422,8 @@ void Tree::draw() const
|
||||
|
||||
void Tree::print() const
|
||||
{
|
||||
//for(const auto& member: people)
|
||||
// member.print();
|
||||
for(int i=0; i<numPeople;++i)
|
||||
people[i].print();
|
||||
for(auto& member: people)
|
||||
member.print();
|
||||
}
|
||||
|
||||
void Tree::printRel(const unsigned id)const
|
||||
@@ -489,15 +490,15 @@ void Tree::printOldestAncestors(const unsigned id)const
|
||||
|
||||
|
||||
|
||||
void Tree::commonAncestorRec(person_id id, const Array<char> &visited)const
|
||||
void Tree::commonAncestorRec(person_id id, vector<char> &visited)const
|
||||
{
|
||||
auto& curRels = relations[id];
|
||||
for (unsigned i = 0; i < curRels.gNum(); ++i)
|
||||
for (auto& rel: curRels)
|
||||
{
|
||||
if (curRels[i].type == Child)
|
||||
if (rel.type == Child)
|
||||
{
|
||||
++visited[curRels[i].id];
|
||||
commonAncestorRec(curRels[i].id, visited);
|
||||
++visited[rel.id];
|
||||
commonAncestorRec(rel.id, visited);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -547,7 +548,7 @@ static U8 linkToChild(I16 startY, I16 startX, I16 endX, bool moreSiblings)
|
||||
/* people[id].draw(drawY, drawX, true, false, true); */
|
||||
|
||||
/* std::vector<person_id> children; */
|
||||
/* for(U32 i=0; i<relations[id].gNum(); ++i) */
|
||||
/* for(U32 i=0; i<relations[id].size(); ++i) */
|
||||
/* { */
|
||||
/* if(relations[id][i].type == RelType::Parent) */
|
||||
/* children.push_back(relations[id][i].id); */
|
||||
@@ -580,13 +581,13 @@ I16 Tree::drawLineWithWives(const person_id id, I16 drawY, const I16 drawX) cons
|
||||
|
||||
person_id spouse = Nobody;
|
||||
std::vector<person_id> children;
|
||||
for(person_id i = 0; i < rels.gNum(); ++i)
|
||||
for(const auto& rel: rels)
|
||||
{
|
||||
if(rels[i].type == RelType::Parent)
|
||||
children.push_back(rels[i].id);
|
||||
if(rel.type == RelType::Parent)
|
||||
children.push_back(rel.id);
|
||||
|
||||
else if(rels[i].type == RelType::Spouse && spouse == Nobody)
|
||||
spouse = rels[i].id;
|
||||
else if(rel.type == RelType::Spouse && spouse == Nobody)
|
||||
spouse = rel.id;
|
||||
}
|
||||
const size_t numKids = children.size();
|
||||
person.draw(drawY, drawX,
|
||||
@@ -625,7 +626,7 @@ void Tree::selectLeft() /* todo */
|
||||
{
|
||||
const auto& curRel = relations[selected_];
|
||||
|
||||
for(U32 i = 0; i < curRel.gNum(); ++i)
|
||||
for(U32 i = 0; i < curRel.size(); ++i)
|
||||
{
|
||||
if(curRel[i].type == Sibling)
|
||||
{
|
||||
@@ -638,13 +639,13 @@ void Tree::selectLeft() /* todo */
|
||||
|
||||
void Tree::selectDown()
|
||||
{
|
||||
const auto& curRel = relations[selected_];
|
||||
const auto& curRels = relations[selected_];
|
||||
|
||||
for(U32 i = 0; i < curRel.gNum(); ++i)
|
||||
for(const auto& rel: curRels)
|
||||
{
|
||||
if(curRel[i].type == Parent)
|
||||
if(rel.type == Parent)
|
||||
{
|
||||
selected_ = curRel[i].id;
|
||||
selected_ = rel.id;
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
@@ -653,13 +654,13 @@ void Tree::selectDown()
|
||||
|
||||
void Tree::selectUp()
|
||||
{
|
||||
const auto& curRel = relations[selected_];
|
||||
const auto& curRels = relations[selected_];
|
||||
|
||||
for(U32 i = 0; i < curRel.gNum(); ++i)
|
||||
for(const auto& rel: curRels)
|
||||
{
|
||||
if(curRel[i].type == Child && (people[curRel[i].id].isMale() || curRel[i].id == focused_))
|
||||
if(rel.type == Child && (people[rel.id].isMale() || rel.id == focused_))
|
||||
{
|
||||
selected_ = curRel[i].id;
|
||||
selected_ = rel.id;
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user