From 3fff88637ec7e806f19e613051f31ca5178d2cf5 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 24 Jun 2007 12:08:34 +0000 Subject: [PATCH] better if SortAction takes care of the shared_ptr, makes other code easier --- src/genesorter.cpp | 2 +- src/genesorter.h | 4 +--- src/reverseaction.h | 3 ++- src/sortaction.h | 18 +++++++++++++++--- src/test/genesortertest.cpp | 10 +++++----- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/genesorter.cpp b/src/genesorter.cpp index 1e19984..40e62e5 100644 --- a/src/genesorter.cpp +++ b/src/genesorter.cpp @@ -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; } diff --git a/src/genesorter.h b/src/genesorter.h index 402464b..3842611 100644 --- a/src/genesorter.h +++ b/src/genesorter.h @@ -22,7 +22,6 @@ #define __GENESORTER_H__ #include -#include class SortAction; class GeneOrder; @@ -34,8 +33,7 @@ class GeneOrder; */ class GeneSorter{ public: - typedef std::tr1::shared_ptr ActionPointer; - typedef std::vector ActionList; + typedef std::vector ActionList; /** * Takes a GeneOrder, finds the actions to transform it into a sorted diff --git a/src/reverseaction.h b/src/reverseaction.h index 0d8ac91..0330597 100644 --- a/src/reverseaction.h +++ b/src/reverseaction.h @@ -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 diff --git a/src/sortaction.h b/src/sortaction.h index a71a533..d1a3f7b 100644 --- a/src/sortaction.h +++ b/src/sortaction.h @@ -21,30 +21,42 @@ #ifndef __SORTACTION_H__ #define __SORTACTION_H__ +#include 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 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 diff --git a/src/test/genesortertest.cpp b/src/test/genesortertest.cpp index b061918..479db46 100644 --- a/src/test/genesortertest.cpp +++ b/src/test/genesortertest.cpp @@ -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))); } }; -- 2.39.2