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
+2
View File
@@ -32,6 +32,8 @@ int main(int argc, char* argv[])
} }
init_ncurses(); init_ncurses();
if( !tree.isEmpty() )
displayTree(tree); displayTree(tree);
return 0; return 0;
+26 -12
View File
@@ -1,8 +1,10 @@
#include"tree.hpp" #include <assert.h>
#include"utils.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <assert.h> #include <algorithm>
#include"tree.hpp"
#include"utils.hpp"
using std::vector; using std::vector;
using std::string; using std::string;
@@ -37,28 +39,40 @@ Tree& Tree::operator+=(const Tree& other)
people_.reserve(idCounter); people_.reserve(idCounter);
for (person_id i = 0; i < other.people_.size(); ++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] >= initNum) /* isnt already in I */ if (newId[i] >= initNum) /* isnt present 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.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()) for (Relation& rel: relations_.back())
rel.id = newId[rel.id]; rel.id = newId[rel.id];
} }
else // if present in both trees else // if present in both trees
{ {
person_id idIn1 = newId[i]; person_id idIn1 = newId[idIn2];
relations_[idIn1].reserve(relations_[idIn1].size() + other.relations_[i].size()); 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 */ if(newId[relIn2.id] >= initNum) /* relative not present in I */
relations_[idIn1].push_back( {newId[rel.id], rel.type} ); 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} );
}
} }
} }
+1 -1
View File
@@ -96,7 +96,7 @@ private:
// 0 is the first person in the tree // 0 is the first person in the tree
person_id focused_ = 0; /* the tree was draw based on this person */ person_id focused_ = 0; /* the tree was draw based on this person */
person_id selected_ = 0; person_id selected_;
U8 zoom_ = 0; U8 zoom_ = 0;
// todo: figure out what to do with these constants // todo: figure out what to do with these constants
-2
View File
@@ -16,8 +16,6 @@ namespace
void displayTree(Tree& t) void displayTree(Tree& t)
{ {
// checck there are people
person_id selected = 0; // the currently selected selected person_id selected = 0; // the currently selected selected
U8 zoom = 0; // how much info to show for each selected U8 zoom = 0; // how much info to show for each selected
+3 -2
View File
@@ -11,7 +11,7 @@ types - screenCoord...
Visualization: Visualization:
- zooming - zooming
- snap to selection/scroll with selection - snap to selection/scroll with selection
- horizontal navigation between cousins - finish - horizontal navigation between cousins
- color on different consoles - color on different consoles
- screen resize - screen resize
- empty tree - empty tree
@@ -33,7 +33,8 @@ Compilation
Refactoring Refactoring
- comment style /* - comment style /*
- check return codes - setlocale, - check return codes - setlocale,
- focused -> root ? - focused -> root
- tree::relations_ to std::set
test test
- no Adam - no Adam