]> ruin.nu Git - germs.git/blob - src/geneorder.h
More help output
[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
27 #include "misc.h"
28
29 /**
30  * Stores a gene order permutation and ensures that all genes are present 
31  * and not duplicated.
32  *
33  * It has limited support for acting as an STL container, but still maintaining
34  * the invariant of a permutation starting with 0 and ending with n, with all
35  * genes present.
36  *
37  * \author Michael Andreen
38 */
39 class GeneOrder{
40         public:
41
42                 typedef std::vector<Gene> GeneList;
43                 typedef GeneList::size_type size_type;
44                 typedef GeneList::const_iterator iterator;
45
46                 /**
47                  * Creates a copy of the given gene order
48                  */
49                 GeneOrder(const GeneOrder& go);
50
51                 /**
52                  * Creates a gene order from a given list, using STL-type iterators.
53                  *
54                  * The given permutation needs to contain all genes 1 to n, if 0 is
55                  * included it has to be the first item, if it is not present it will
56                  * be added automatically. Similarily, if n is not the last item, then
57                  * n+1 will be added to the end.
58                  *
59                  * \param begin iterator to the first element in the list, from begin()
60                  * on stl collections and pointer to first item on plain arrays.
61                  *
62                  * \param end iterator to the element behind the last, from end() in
63                  * stl collections or pointer to first element+size on plain arrays.
64                  *
65                  * \throws std::invalid_argument if the list is not a valid permutation.
66                  */
67                 template<class T>
68                 GeneOrder(T begin, T end);
69
70                 /**
71                  * Destroyes the gene order.
72                  */
73                 ~GeneOrder();
74
75                 /**
76                  * Copies the given gene order.
77                  */
78                 const GeneOrder& operator=(const GeneOrder& go);
79
80                 /**
81                  * Returns the gene at the given index.
82                  *
83                  * \throws std::out_of_range if i is smaller than 0 or bigger than n.
84                  */
85                 const Gene& operator[](size_type i) const;
86
87                 /**
88                  * Returns the size of the permutation.
89                  */
90                 size_type size() const;
91
92                 /**
93                  * Returns the vector containing the gene order permutation.
94                  */
95                 const GeneList& list() const;
96
97                 /**
98                  * Returns the start iterator for the permutation.
99                  * To be used with STL style functions.
100                  *
101                  * \see end
102                  */
103                 iterator begin() const;
104
105                 /**
106                  * Returns the end iterator for the permutation.
107                  * To be used with STL style functions.
108                  *
109                  * \see begin
110                  */
111                 iterator end() const;
112
113                 /**
114                  * Reverserses the interval [i,j], changing the sign on all elements
115                  * affected.
116                  *
117                  * \throws std::out_of_range if i is smaller than 0 or bigger than n.
118                  */
119                 void reverse(size_type i, size_type j);
120
121
122         private:
123                 GeneList _geneorder;
124                 /**
125                  * pads the permutation with 0 in front and n+1 at the end if needed
126                  */
127                 void pad();
128
129                 /**
130                  * Verifies that the permutation starts with 0, ends with n+1 and that all genes i, 0 < i < n+1
131                  * are present, without duplication.
132                  */
133                 void verify();
134 };
135
136
137 /**
138  * Put all the genes in the list, check that all genes are included, pad with 0 and n+1 if needed.
139  * TODO: This is far from done atm
140  */
141 template<typename T>
142 GeneOrder::GeneOrder(T begin, T end): _geneorder(begin,end){
143         pad();
144         verify();
145 }
146
147 #endif
148