X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=174c6f7b0414277b006b562f2736a83077db9fb7;hb=a16f3a90b540c9adf29050f8041baf0c0781c20a;hp=7f54109dc6c0103251cafff77bef93d92fa9f1e4;hpb=148a31960aeb2ba127ef4322a12ae3af28ef2988;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index 7f54109..174c6f7 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -20,6 +20,7 @@ #include "genealgorithms.h" #include "geneorder.h" +#include "componenttree.h" #include #include @@ -92,6 +93,12 @@ size_t countCycles(const GeneOrder& go){ return cycles; } +size_t inversionDistance(const GeneOrder& go){ + size_t cycles = countCycles(go); + + return go.size() - 1 - cycles; +} + int sign(Gene g){ if (g > 0) return 1; @@ -140,7 +147,7 @@ std::vector findComponents(const GeneOrder& go){ s = Sdir.top(); } if (go[i] > 0 && dir[i] == dir[s] && static_cast(i - s) == p[i] - p[s]) - components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s]))); + components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s]),s,i)); //Reverse if (p[i-1] < p[i]) @@ -156,7 +163,7 @@ std::vector findComponents(const GeneOrder& go){ s = Srev.top(); } if (go[i] < 0 && rev[i] == rev[s] && static_cast(i - s) == p[s] - p[i]) - components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s]))); + components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s]),s,i)); //Update stacks if (go[i] > 0) @@ -167,18 +174,40 @@ std::vector findComponents(const GeneOrder& go){ return components; } +int sign2(Gene g){ + if (g < 0) + return -1; + return 1; +} /** * */ std::vector findIntervals(const GeneOrder& go){ - vector intervals(go.size()-1,Interval(go.size(),go.size())); + const size_t max = go.size(); + vector intervals(go.size()-1,Interval(max,max,false)); size_t n = 0; - for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g, ++n){ + const GeneOrder::iterator end = go.end(); + for (GeneOrder::iterator g = go.begin(); g != end; ++g, ++n){ size_t i = abs(*g); - if (i < go.size() - 1) - intervals[i].first = n + (*g >= 0 ? 1 : 0); - if (i > 0) - intervals[i-1].second = n + (*g < 0 ? 1 : 0); + if (i < max - 1){ + Interval& curr = intervals[i]; + curr.first = n + (*g >= 0 ? 1 : 0); + + if (curr.second == max) + curr.oriented = *g < 0; + else + curr.oriented ^= *g < 0; + } + if (i > 0){ + Interval& prev = intervals[i-1]; + prev.second = n + (*g < 0 ? 1 : 0); + + if (prev.first == max) + prev.oriented = *g < 0; + else + prev.oriented ^= *g < 0; + } + } return intervals; } @@ -188,7 +217,7 @@ std::vector findIntervals(const GeneOrder& go){ */ std::vector findIntervalsAtPoints(const vector& intervals){ size_t max = intervals.size()+1; - vector points(max,Interval(max,max)); + vector points(max,Interval(max,max,false)); size_t n = 0; for (vector::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){ if (points[i->first].first == max){