#define __GENESORTER_H__
#include <vector>
-#include <tr1/memory>
class SortAction;
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
/**
* 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
#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
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));
}
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)));
}
};