fix a bug when merging unrelated trees

This commit is contained in:
vrd
2024-06-18 12:36:21 +03:00
parent 4d61963dae
commit c96bbdbecf
5 changed files with 36 additions and 21 deletions
+3 -1
View File
@@ -32,7 +32,9 @@ int main(int argc, char* argv[])
}
init_ncurses();
displayTree(tree);
if( !tree.isEmpty() )
displayTree(tree);
return 0;
}
+29 -15
View File
@@ -1,8 +1,10 @@
#include"tree.hpp"
#include"utils.hpp"
#include <assert.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <algorithm>
#include"tree.hpp"
#include"utils.hpp"
using std::vector;
using std::string;
@@ -37,28 +39,40 @@ Tree& Tree::operator+=(const Tree& other)
people_.reserve(idCounter);
for (person_id i = 0; i < other.people_.size(); ++i) /* add the personal data of II to I */
{
if (newId[i] >= initNum) /* isnt already in I */
if (newId[i] >= initNum) /* isnt present in I */
people_.push_back(other.people_[i]);
}
relations_.reserve(idCounter);
for (person_id i = 0; i < other.people_.size(); ++i) //merge the relative data
for (person_id idIn2 = 0; idIn2 < other.people_.size(); ++idIn2) // merge the relational data
{
if (newId[i] >= initNum) /* isnt already in I */
if (newId[idIn2] >= initNum) /* isnt present in I */
{
relations_.push_back(other.relations_[i]);
relations_.push_back(other.relations_[idIn2]);
for (Relation& rel: relations_.back())
rel.id = newId[rel.id];
}
else //if present in both trees
else // if present in both trees
{
person_id idIn1 = newId[i];
relations_[idIn1].reserve(relations_[idIn1].size() + other.relations_[i].size());
person_id idIn1 = newId[idIn2];
std::vector<Relation>& relsIn1 = relations_[idIn1];
relsIn1.reserve(relsIn1.size() + other.relations_[idIn2].size());
for (Relation rel: other.relations_[i])
for (Relation relIn2: other.relations_[idIn2])
{
if(newId[rel.id] >= initNum) /* not already in I */
relations_[idIn1].push_back( {newId[rel.id], rel.type} );
if(newId[relIn2.id] >= initNum) /* relative not present in I */
relsIn1.push_back( {newId[relIn2.id], relIn2.type} );
else
{
bool foundIn1 = false;
for(Relation relIn1: relsIn1)
if(relIn1.id == newId[relIn2.id])
foundIn1 = true;
if( !foundIn1 ) // relative is present in I, but relation only in II
relsIn1.push_back( {newId[relIn2.id], relIn2.type} );
}
}
}
@@ -529,13 +543,13 @@ I16 Tree::drawLineWithWives(const person_id id, I16 drawY, const I16 drawX) cons
}
const size_t numKids = children.size();
U16 person_w = person.draw(drawY, drawX,
id == selected_, spouse!=Nobody, true, numKids!=0, zoom_==1);
id == selected_, spouse!=Nobody, true, numKids!=0, zoom_==1);
U16 spouse_w = 0;
if (spouse != Nobody)
{
U16 spouse_box = people_[spouse].draw(drawY, drawX + person_w + spouse_dist_,
spouse == selected_, false, false, false, zoom_==1);
spouse == selected_, false, false, false, zoom_==1);
spouse_w = spouse_dist_ + spouse_box;
}
+1 -1
View File
@@ -96,7 +96,7 @@ private:
// 0 is the first person in the tree
person_id focused_ = 0; /* the tree was draw based on this person */
person_id selected_ = 0;
person_id selected_;
U8 zoom_ = 0;
// todo: figure out what to do with these constants
-2
View File
@@ -16,8 +16,6 @@ namespace
void displayTree(Tree& t)
{
// checck there are people
person_id selected = 0; // the currently selected selected
U8 zoom = 0; // how much info to show for each selected
+3 -2
View File
@@ -11,7 +11,7 @@ types - screenCoord...
Visualization:
- zooming
- snap to selection/scroll with selection
- horizontal navigation between cousins - finish
- horizontal navigation between cousins
- color on different consoles
- screen resize
- empty tree
@@ -33,7 +33,8 @@ Compilation
Refactoring
- comment style /*
- check return codes - setlocale,
- focused -> root ?
- focused -> root
- tree::relations_ to std::set
test
- no Adam