]> ruin.nu Git - germs.git/commitdiff
better if SortAction takes care of the shared_ptr, makes other code easier
authorMichael Andreen <harv@ruin.nu>
Sun, 24 Jun 2007 12:08:34 +0000 (12:08 +0000)
committerMichael Andreen <harv@ruin.nu>
Sun, 24 Jun 2007 12:08:34 +0000 (12:08 +0000)
src/genesorter.cpp
src/genesorter.h
src/reverseaction.h
src/sortaction.h
src/test/genesortertest.cpp

index 1e199843a1a5ecedcbf692fc6da5117dafa4ce8e..40e62e5b8bab62ae58e55fdc85f0c2f7a486380e 100644 (file)
@@ -36,6 +36,6 @@ GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){
        if (countCycles(go) == go.size() - 1)
                return ActionList();
        ActionList al;
-       al.push_back(ActionPointer(new ReverseAction(2,3)));
+       al.push_back(SortAction(new ReverseAction(2,3)));
        return al;
 }
index 402464bcc4b8555d3d0c6541dc2ab21aa4fc18f3..384261196060dfaa19522a7deadc68e28784e6d1 100644 (file)
@@ -22,7 +22,6 @@
 #define __GENESORTER_H__
 
 #include <vector>
-#include <tr1/memory>
 
 class SortAction;
 class GeneOrder;
@@ -34,8 +33,7 @@ class GeneOrder;
  */
 class GeneSorter{
        public:
-               typedef std::tr1::shared_ptr<SortAction> ActionPointer;
-               typedef std::vector<ActionPointer> ActionList;
+               typedef std::vector<SortAction> ActionList;
 
                /**
                 * Takes a GeneOrder, finds the actions to transform it into a sorted
index 0d8ac91999e02d3f5bf2b3d1058854e3dcb76fb9..0330597a90b775c7da1ed89fb118ee59482e1241 100644 (file)
@@ -33,7 +33,8 @@ class ReverseAction : public SortAction{
                /**
                 * Creates a new reverse action for the interval [i,j]
                 */
-               ReverseAction(size_t i, size_t j):_i(i),_j(j){}
+               ReverseAction(size_t i, size_t j): SortAction(0),_i(i),_j(j){
+               }
 
                /**
                 * Applies the sort action on the gene order
index a71a533cf70ec981f10d32527053e58cdf0a609b..d1a3f7bca68e3fe5a11c94631f117123c10295fe 100644 (file)
 #ifndef __SORTACTION_H__
 #define __SORTACTION_H__
 
+#include <tr1/memory>
 class GeneOrder;
 /**
- * Abstraction of a sort action
+ * Abstraction of a sort action, all child actions has to be immutable.
  *
  * \author Michael Andreen
  */
 class SortAction{
        public:
+               typedef std::tr1::shared_ptr<SortAction> ActionPointer;
 
+               /**
+                * Creates a new sort action, given a specified action.
+                * SortAction promises to remove the given action.
+                */
+               SortAction(SortAction* sa): _action(sa){
+               }
+               
                virtual ~SortAction(){};
 
                /**
                 * Applies the action on the GeneOrder and returning it.
                 */
                virtual GeneOrder& operator()(GeneOrder& go) const{
-                       return go;
+                       return (*_action)(go);
                }
 
                /**
                 * Compares sort actions.
                 */
                virtual bool operator==(const SortAction& sa) const{
-                       return false;
+                       return (*_action) == (sa._action.get() == 0 ? sa : *sa._action);
                }
+       private:
+               ActionPointer _action;
+
 };
 
 #endif
index b06191827c58376cd50e31346094dea7e05d7280..479db468e25d97d048d5c7cb9acdc54bdd54cb3c 100644 (file)
@@ -62,16 +62,16 @@ protected:
                GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
                al = so.sort(go2);
                CPPUNIT_ASSERT_EQUAL(1ul,al.size());
-               CPPUNIT_ASSERT((*al[0]) == ReverseAction(2,3));
+               CPPUNIT_ASSERT(al[0] == ReverseAction(2,3));
 
-               (*al[0])(go2);
+               al[0](go2);
                CPPUNIT_ASSERT(equal(go.begin(),go.end(),go2.begin()));
 
                GeneOrder go3(_validPerm3.begin(),_validPerm3.end());
                al = so.sort(go3);
                CPPUNIT_ASSERT_EQUAL(5ul,al.size());
                for (size_t i = 0; i < al.size(); ++i)
-                       (*al[i])(go3);
+                       al[i](go3);
                int perm[] = {0,1,2,3,4,5,6,7,8,9};
                CPPUNIT_ASSERT(equal(go3.begin(),go3.end(),perm));
        }
@@ -84,8 +84,8 @@ protected:
                GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
                al = so.safeActions(go2);
                CPPUNIT_ASSERT_EQUAL(1ul,al.size());
-               CPPUNIT_ASSERT((*al[0]) == ReverseAction(2,3));
-               CPPUNIT_ASSERT(!((*al[0]) == ReverseAction(2,5)));
+               CPPUNIT_ASSERT(al[0] == ReverseAction(2,3));
+               CPPUNIT_ASSERT(!(al[0] == ReverseAction(2,5)));
        }
 
 };