00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _eoPopulator_H
00027 #define _eoPopulator_H
00028
00029 #include <eoPop.h>
00030 #include <eoSelectOne.h>
00031
00038 template <class EOT>
00039 class eoPopulator
00040 {
00041 public :
00042
00043 eoPopulator(const eoPop<EOT>& _src, eoPop<EOT>& _dest) : dest(_dest), current(dest.end()), src(_src)
00044 {
00045 dest.reserve(src.size());
00046 current = dest.end();
00047 }
00048
00050 virtual ~eoPopulator() {};
00051
00052 struct OutOfIndividuals {};
00053
00058 EOT& operator*(void)
00059 {
00060 if (current == dest.end())
00061 get_next();
00062
00063 return *current;
00064 }
00065
00070 eoPopulator& operator++()
00071 {
00072 if (current == dest.end())
00073 {
00074 return *this;
00075 }
00076
00077 ++current;
00078 return *this;
00079 }
00080
00084 void insert(const EOT& _eo)
00085 {
00086 current = dest.insert(current, _eo);
00087 }
00088
00091 void reserve(int how_many)
00092 {
00093 size_t sz = current - dest.begin();
00094 if (dest.capacity() < dest.size() + how_many)
00095 {
00096 dest.reserve(dest.size() + how_many);
00097 }
00098
00099 current = dest.begin() + sz;
00100 }
00101
00105 const eoPop<EOT>& source(void) { return src; }
00106
00110 eoPop<EOT>& offspring(void) { return dest; }
00111
00112 typedef unsigned position_type;
00113
00115 position_type tellp() { return current - dest.begin(); }
00117 void seekp(position_type pos) { current = dest.begin() + pos; }
00119 bool exhausted(void) { return current == dest.end(); }
00120
00124 virtual const EOT& select() = 0;
00125
00126 protected:
00127 eoPop<EOT>& dest;
00128 typename eoPop<EOT>::iterator current;
00129 const eoPop<EOT>& src;
00130
00131 private:
00132
00133 void get_next() {
00134 if(current == dest.end())
00135 {
00136 dest.push_back(select());
00137 current = dest.end();
00138 --current;
00139 return;
00140 }
00141
00142 ++current;
00143 return;
00144 }
00145
00146 };
00147
00148
00153 template <class EOT>
00154 class eoSeqPopulator : public eoPopulator<EOT>
00155 {
00156 public:
00157
00158 using eoPopulator< EOT >::src;
00159
00160 eoSeqPopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest) :
00161 eoPopulator<EOT>(_pop, _dest), current(0) {}
00162
00164 const EOT& select(void) {
00165 if(current >= eoPopulator< EOT >::src.size()) {
00166 throw OutOfIndividuals();
00167 }
00168
00169 const EOT& res = src[current++];
00170 return res;
00171 }
00172
00173
00174 private:
00175
00176 struct OutOfIndividuals {};
00177
00178 unsigned current;
00179 };
00180
00181
00185 template <class EOT>
00186 class eoSelectivePopulator : public eoPopulator<EOT>
00187 {
00188 public :
00189
00190 using eoPopulator< EOT >::src;
00191
00192 eoSelectivePopulator(const eoPop<EOT>& _pop, eoPop<EOT>& _dest, eoSelectOne<EOT>& _sel)
00193 : eoPopulator<EOT>(_pop, _dest), sel(_sel)
00194 { sel.setup(_pop); };
00195
00197 const EOT& select() {
00198 return sel(src);
00199 }
00200
00201
00202 private:
00203
00204 eoSelectOne<EOT>& sel;
00205 };
00206
00207 #endif
00208