X-Git-Url: https://ruin.nu/git/index.pl?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=e96c9be63e9d378187a0d58b9413905bf6517e20;hb=bd6d14a70222d80caf00cee73b3014d392dbc69f;hp=04875daca3031c0803ed527b9a36742d8965289a;hpb=172455c7649ea714eb13f9ac1fd96652f0617ca0;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index 04875da..e96c9be 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -65,40 +65,6 @@ struct FindP{ }; -std::vector findIntervalsAtPoints(const vector& intervals){ - vector points; - points.push_back(Interval(intervals.size(),intervals.size())); //Dummy interval to match point and index - for (size_t p = 1; p <= intervals.size(); ++p){ - size_t f = 0; - size_t s = 0; - bool found = false; - size_t n = 0; - for (vector::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){ - if (i->first == p){ - if (!found){ - f = n; - found = true; - }else{ - s = n; - break; - } - } - if (i->second == p){ - if (!found){ - f = n; - found = true; - }else{ - s = n; - break; - } - } - } - points.push_back(Interval(f,s)); - } - return points; - -} - int countCycles(const GeneOrder& go){ int cycles = 0; set marked; @@ -130,36 +96,39 @@ std::vector findComponents(const GeneOrder& go){ } /** - * TODO: Think of a better than O(n^2) implementation - * Possibly move it to GeneOrder too + * */ std::vector findIntervals(const GeneOrder& go){ - vector intervals; - for (size_t i = 0; i < go.size() - 1; ++i){ - size_t f = 0; - size_t s = 0; - bool found = false; - size_t n = 0; - for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g, ++n){ - if (static_cast(abs(*g)) == i){ - f = n; - if (*g >= 0) - ++f; - if (found) - break; - found = true; - } - if(static_cast(abs(*g)) == i+1){ - s = n; - if (*g < 0) - ++s; - if (found) - break; - found = true; - } - } - intervals.push_back(Interval(f,s)); + vector intervals(go.size()-1,Interval(go.size(),go.size())); + size_t n = 0; + for (GeneOrder::iterator g = go.begin(); g != go.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); } return intervals; } +/** + * + */ +std::vector findIntervalsAtPoints(const vector& intervals){ + size_t max = intervals.size()+1; + vector points(max,Interval(max,max)); + size_t n = 0; + for (vector::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){ + if (points[i->first].first == max){ + points[i->first].first = n; + }else + points[i->first].second = n; + + if (points[i->second].first == max){ + points[i->second].first = n; + }else + points[i->second].second = n; + } + return points; + +}