]> ruin.nu Git - germs.git/blobdiff - src/genealgorithms.cpp
findIntervals implemented and passing test
[germs.git] / src / genealgorithms.cpp
index 7dc832f252c208900b92c234879c8f09b5a93c24..eecdf3fea92bd8048c6c47fd1ef466d5e4698c67 100644 (file)
@@ -24,6 +24,7 @@
 #include <algorithm>
 #include <set>
 #include <cstdlib>
+#include <iostream>
 using namespace std;
 
 std::pair<int,int> longestSequences(const GeneOrder& go){
@@ -69,6 +70,33 @@ std::vector<Component> findComponents(const GeneOrder& go){
        return vector<Component>();
 }
 
+//TODO: Think of a better than O(n^2) implementation
 std::vector<Interval> findIntervals(const GeneOrder& go){
-       return vector<Interval>();
+       vector<Interval> 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<size_t>(abs(*g)) == i){
+                               f = n;
+                               if (*g >= 0)
+                                       ++f;
+                               if (found)
+                                       break;
+                               found = true;
+                       }
+                       if(static_cast<size_t>(abs(*g)) == i+1){
+                               s = n;
+                               if (*g < 0)
+                                       ++s;
+                               if (found)
+                                       break;
+                               found = true;
+                       }
+               }
+               intervals.push_back(Interval(f,s));
+       }
+       return intervals;
 }