]> ruin.nu Git - germs.git/blobdiff - src/genealgorithms.cpp
a little cleanup and minor fix
[germs.git] / src / genealgorithms.cpp
index d85cd8d16caba9f60a2cefb104be7c6962729ea1..78b0f0572dc283b1e7e8f7278303954ff77cddac 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <algorithm>
 #include <set>
+#include <stack>
 #include <cstdlib>
 #include <iostream>
 using namespace std;
@@ -91,8 +92,79 @@ int countCycles(const GeneOrder& go){
        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;
 }
 
 /**
@@ -112,8 +184,7 @@ std::vector<Interval> findIntervals(const GeneOrder& go){
 }
 
 /**
- * TODO: Think of a better than O(n^2) implementation.
- * Possibly move to cache result
+ * 
  */
 std::vector<Interval> findIntervalsAtPoints(const vector<Interval>& intervals){
        size_t max = intervals.size()+1;