From: Michael Andreen Date: Wed, 20 Jun 2007 09:08:42 +0000 (+0000) Subject: findComponents implemented and passes test X-Git-Tag: v0.1~57 X-Git-Url: https://ruin.nu/git/?p=germs.git;a=commitdiff_plain;h=052ccbc26078638efee3614cd7cc2ea485313cad findComponents implemented and passes test --- diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index e96c9be..a32968e 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include using namespace std; @@ -91,8 +92,80 @@ 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 findComponents(const GeneOrder& go){ - return vector(); + vector components; + vector 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 Mdir; + Mdir.push(go.size()-1); + stack Mrev; + Mrev.push(0); + stack Sdir; + Sdir.push(0); + stack Srev; + Srev.push(0); + vector dir; + dir.push_back(go.size()-1); + vector rev; + rev.push_back(0); + size_t s; + vector 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] && 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] && i - s == p[s] - p[i]) + components.push_back(Component(-p[s],-p[i],os[s])); + + //Update stacks + if (go[i] > 0) + Sdir.push(i); + else + Srev.push(i); + } + return components; } /** diff --git a/src/genealgorithms.h b/src/genealgorithms.h index b0e75b9..0158c35 100644 --- a/src/genealgorithms.h +++ b/src/genealgorithms.h @@ -27,6 +27,9 @@ class GeneOrder; struct Component{ Component(int b,int e,int s):begin(b),end(e),sign(s){} + bool operator==(const Component& c){ + return begin == c.begin && end == c.end && sign == c.sign; + } int begin; int end; int sign; diff --git a/src/test/genealgorithmstest.cpp b/src/test/genealgorithmstest.cpp index 1d7f2d0..f2bd118 100644 --- a/src/test/genealgorithmstest.cpp +++ b/src/test/genealgorithmstest.cpp @@ -25,6 +25,7 @@ class TESTNAME : public CPPUNIT_NS::TestFixture CPPUNIT_TEST( testFindIntervals ); CPPUNIT_TEST( testFindIntervalsAtPoints ); CPPUNIT_TEST( testCountCycles ); + CPPUNIT_TEST( testFindComponents ); CPPUNIT_TEST_SUITE_END(); protected: @@ -128,6 +129,36 @@ protected: CPPUNIT_ASSERT_EQUAL(6,c); } + void testFindComponents (){ + GeneOrder go(_validPerm.begin(),_validPerm.end()); + vector v = findComponents(go); + CPPUNIT_ASSERT_EQUAL(4ul,v.size()); + Component go10(0,1,0); + Component go11(1,2,0); + Component go12(2,3,0); + Component go13(3,4,0); + CPPUNIT_ASSERT(go10 == v[0]); + CPPUNIT_ASSERT(go11 == v[1]); + CPPUNIT_ASSERT(go12 == v[2]); + CPPUNIT_ASSERT(go13 == v[3]); + + GeneOrder go2(_validPerm3.begin(),_validPerm3.end()); + v = findComponents(go2); + CPPUNIT_ASSERT_EQUAL(6ul,v.size()); + Component go20(1,2,0); + Component go21(0,4,0); + Component go22(4,7,1); + Component go23(-15,-12,-1); + Component go24(-12,-9,-1); + Component go25(7,16,0); + CPPUNIT_ASSERT(go20 == v[0]); + CPPUNIT_ASSERT(go21 == v[1]); + CPPUNIT_ASSERT(go22 == v[2]); + CPPUNIT_ASSERT(go23 == v[3]); + CPPUNIT_ASSERT(go24 == v[4]); + CPPUNIT_ASSERT(go25 == v[5]); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION( TESTNAME );