]> ruin.nu Git - germs.git/blob - src/GeneOrder.h
link with fann
[germs.git] / src / GeneOrder.h
1 /***************************************************************************
2  *   Copyright (C) 2006 by Michael Andreen                                 *
3  *   andreen@student.chalmers.se                                           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
19  ***************************************************************************/
20
21 #ifndef __GENEORDER_H__
22 #define __GENEORDER_H__
23
24 #include <vector>
25 #include <stdexcept>
26 #include <cstdlib>
27
28 #include "misc.h"
29
30 /**
31  * Stores a gene order permutation and ensures that all genes are present and not duplicated.
32  * \author Michael Andreen
33 */
34 class GeneOrder{
35         public:
36
37                 typedef std::vector<Gene> GeneList;
38                 typedef GeneList::size_type size_type;
39
40                 /**
41                  * Creates a copy of the given gene order
42                  */
43                 GeneOrder(const GeneOrder& go);
44
45                 /**
46                  * Creates a gene order from a given list.
47                  *
48                  * \throws invalid_argument if the list is not a valid permutation.
49                  */
50                 template<class T>
51                 GeneOrder(T begin, T end);
52
53                 /**
54                  * Destroyes the gene order.
55                  */
56                 ~GeneOrder();
57
58                 /**
59                  * Copies the given gene order.
60                  */
61                 const GeneOrder& operator=(const GeneOrder& go);
62
63                 /**
64                  * Returns the gene at the given index.
65                  *
66                  * \throws out_of_range if i is smaller than 0 or too big.
67                  */
68                 const Gene& operator[](size_type i) const;
69
70                 /**
71                  * Returns the size of the permutation.
72                  */
73                 int size() const;
74
75                 /**
76                  * Returns the vector containing the gene order permutation.
77                  */
78                 const GeneList& list() const;
79
80                 /**
81                  * Reverses an interval and returns the new permutation
82                  */
83                 GeneOrder reverse(size_type i, size_type j) const;
84
85                 /**
86                  * Moves the gene on position i to position j, returning a new permutation
87                  */
88                 GeneOrder transpos(size_type i, size_type j) const;
89
90         private:
91                 GeneList _geneorder;
92 };
93
94
95 /**
96  * Put all the genes in the list, check that all genes are included, pad with 0 and n+1 if needed.
97  * TODO: This is far from done atm
98  */
99 template<typename T>
100 GeneOrder::GeneOrder(T begin, T end): _geneorder(begin,end){
101
102         /*TODO: Pad code, just not sure if I need it all the time.
103         if (_geneorder[0] != 0)
104                 _geneorder.insert(_geneorder.begin(),0);
105         if(_geneorder[_geneorder.size()-1] != _geneorder.size() - 1)
106                 _geneorder.push_back(_geneorder.size());
107         */
108
109         GeneList genes(_geneorder.size());
110         for (GeneList::iterator gene = _geneorder.begin(); gene != _geneorder.end(); ++gene){
111                 ++genes[std::abs(*gene)];
112         }
113         for (GeneList::iterator gene = genes.begin(); gene != genes.end(); ++gene){
114                 if (*gene != 1)
115                         throw std::invalid_argument("Not all genes are present only 1 time");
116         }
117 }
118
119 #endif
120