#include <algorithm>
#include <set>
+#include <stack>
#include <cstdlib>
#include <iostream>
using namespace std;
return cycles;
}
+int sign(Gene g){
+ if (g > 0)
+ return 1;
+ if (g < 0)
+ return -1;
+ return 0;
+}
+
+struct Abs{
+ Gene operator()(Gene x) const{
+ return abs(x);
+ }
+};
std::vector<Component> findComponents(const GeneOrder& go){
- return vector<Component>();
+ vector<Component> components;
+ vector<int> os(go.size()-1);
+ for (size_t i = 0; i < os.size(); ++i)
+ os[i] = (go[i]*go[i+1] > 0 ? sign(go[i]) : 0);
+ stack<Gene> Mdir;
+ Mdir.push(go.size()-1);
+ stack<Gene> Mrev;
+ Mrev.push(0);
+ stack<size_t> Sdir;
+ Sdir.push(0);
+ stack<size_t> Srev;
+ Srev.push(0);
+ vector<Gene> dir;
+ dir.push_back(go.size()-1);
+ vector<Gene> rev;
+ rev.push_back(0);
+ size_t s;
+ vector<Gene> p(go.list());
+ transform(p.begin(),p.end(),p.begin(),Abs());
+ for (size_t i = 1; i < go.size(); ++i){
+ //Directed
+ if (p[i-1] > p[i])
+ Mdir.push(p[i-1]);
+ else while (Mdir.top() < p[i])
+ Mdir.pop();
+ dir.push_back(Mdir.top());
+
+ s = Sdir.top();
+ while(p[Sdir.top()] > p[i] || dir[Sdir.top()] < p[i]){
+ Sdir.pop();
+ os[Sdir.top()] = (os[Sdir.top()] == os[s] ? os[s] : 0);
+ s = Sdir.top();
+ }
+ if (go[i] > 0 && dir[i] == dir[s] && static_cast<Gene>(i - s) == p[i] - p[s])
+ components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s])));
+
+ //Reverse
+ if (p[i-1] < p[i])
+ Mrev.push(p[i-1]);
+ else while (Mrev.top() > p[i])
+ Mrev.pop();
+ rev.push_back(Mrev.top());
+
+ s = Srev.top();
+ while((p[s] < p[i] || rev[s] > p[i]) && s > 0){
+ Srev.pop();
+ os[Srev.top()] *= (os[Srev.top()] == os[s] ? 1 : 0);
+ s = Srev.top();
+ }
+ if (go[i] < 0 && rev[i] == rev[s] && static_cast<Gene>(i - s) == p[s] - p[i])
+ components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s])));
+
+ //Update stacks
+ if (go[i] > 0)
+ Sdir.push(i);
+ else
+ Srev.push(i);
+ }
+ return components;
}
/**
}
/**
- * TODO: Think of a better than O(n^2) implementation.
- * Possibly move to cache result
+ *
*/
std::vector<Interval> findIntervalsAtPoints(const vector<Interval>& intervals){
- vector<Interval> 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<Interval>::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));
+ size_t max = intervals.size()+1;
+ vector<Interval> points(max,Interval(max,max));
+ size_t n = 0;
+ for (vector<Interval>::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;