* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
***************************************************************************/
-#include <geneorder.h>
-
#include <algorithm>
+#include <stdexcept>
#include <cstdlib>
-
using namespace std;
+#include "geneorder.h"
+
/**
* \file GeneOrder.cpp
* Implements the GeneOrder class
*/
-
GeneOrder::GeneOrder(const GeneOrder& go){
_geneorder = go._geneorder;
}
_geneorder.push_back(_geneorder.size());
}
-struct test{
+struct Abs{
Gene operator()(Gene x) const{
return abs(x);
}
void GeneOrder::verify(){
if(_geneorder[0] != 0)
- throw std::invalid_argument("Permutation doesn't start with 0");
+ throw invalid_argument("Permutation doesn't start with 0");
if ( _geneorder.back() != static_cast<Gene>(_geneorder.size() - 1))
- throw std::invalid_argument("Permutation doesn't end with n+1");
+ throw invalid_argument("Permutation doesn't end with n+1");
+
GeneList genes(_geneorder);
- transform(genes.begin(),genes.end(),genes.begin(),test());
+ transform(genes.begin(),genes.end(),genes.begin(),Abs());
sort(genes.begin(),genes.end());
if (unique(genes.begin(),genes.end()) != genes.end())
throw std::invalid_argument("Not all genes are present exactly 1 time");
*/
const Gene& GeneOrder::operator[](size_type i) const{
if (i < 0 || i >= _geneorder.size())
- throw out_of_range("Index is not in valid range");
+ throw out_of_range("Indexis not in valid range");
return _geneorder[i];
}
-int GeneOrder::size() const{
+GeneOrder::size_type GeneOrder::size() const{
return _geneorder.size();
}
const GeneOrder::GeneList& GeneOrder::list() const{
return _geneorder;
}
+
+
+GeneOrder::iterator GeneOrder::begin() const{
+ return _geneorder.begin();
+}
+
+GeneOrder::iterator GeneOrder::end() const{
+ return _geneorder.end();
+}
class GeneOrder{
public:
- typedef std::vector<long> GeneList;
+ typedef std::vector<Gene> GeneList;
typedef GeneList::size_type size_type;
+ typedef GeneList::const_iterator iterator;
/**
* Creates a copy of the given gene order
GeneOrder(const GeneOrder& go);
/**
- * Creates a gene order from a given list.
+ * Creates a gene order from a given list, using STL-type iterators.
*
- * \throws invalid_argument if the list is not a valid permutation.
+ * The given permutation needs to contain all genes 1 to n, if 0 is
+ * included it has to be the first item, if it is not present it will
+ * be added automatically. Similarily, if n is not the last item, then
+ * n+1 will be added to the end.
+ *
+ * \param begin iterator to the first element in the list, from begin()
+ * on stl collections and pointer to first item on plain arrays.
+ *
+ * \param end iterator to the element behind the last, from end() in
+ * stl collections or pointer to first element+size on plain arrays.
+ *
+ * \throws std::invalid_argument if the list is not a valid permutation.
*/
template<class T>
GeneOrder(T begin, T end);
/**
* Returns the gene at the given index.
*
- * \throws out_of_range if i is smaller than 0 or too big.
+ * \throws std::out_of_range if i is smaller than 0 or bigger than n.
*/
const Gene& operator[](size_type i) const;
/**
* Returns the size of the permutation.
*/
- int size() const;
+ size_type size() const;
/**
* Returns the vector containing the gene order permutation.
*/
const GeneList& list() const;
+ /**
+ * Returns the start iterator for the permutation.
+ * To be used with STL style functions.
+ *
+ * \see end
+ */
+ iterator begin() const;
+
+ /**
+ * Returns the end iterator for the permutation.
+ * To be used with STL style functions.
+ *
+ * \see begin
+ */
+ iterator end() const;
+
private:
GeneList _geneorder;
void testCopyCreate (){
GeneOrder go(_bigvalidPerm.begin(),_bigvalidPerm.end());
GeneOrder go2(go);
- CPPUNIT_ASSERT(equal(go.list().begin(),go.list().end(),go2.list().begin()));
+ CPPUNIT_ASSERT(equal(go.begin(),go.end(),go2.begin()));
GeneOrder go3(_validPerm.begin(),_validPerm.end());
- CPPUNIT_ASSERT(!equal(go3.list().begin(),go3.list().end(),go2.list().begin()));
+ CPPUNIT_ASSERT(!equal(go3.begin(),go3.end(),go2.begin()));
}
void testAssign (){
GeneOrder go(_bigvalidPerm.begin(),_bigvalidPerm.end());
GeneOrder go2(_validPerm.begin(),_validPerm.end());
go2 = go;
- CPPUNIT_ASSERT(equal(go.list().begin(),go.list().end(),go2.list().begin()));
+ CPPUNIT_ASSERT(equal(go.begin(),go.end(),go2.begin()));
GeneOrder go3(_validPerm.begin(),_validPerm.end());
- CPPUNIT_ASSERT(!equal(go3.list().begin(),go3.list().end(),go2.list().begin()));
+ CPPUNIT_ASSERT(!equal(go3.begin(),go3.end(),go2.begin()));
}
};