EvolvingObjects
eoDetSelect.h
00001 
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _eoDetSelect_h
00028 #define _eoDetSelect_h
00029 
00030 
00031 //-----------------------------------------------------------------------------
00032 #include <eoSelect.h>
00033 #include <utils/eoHowMany.h>
00034 #include <math.h>
00035 //-----------------------------------------------------------------------------
00036 
00041 template<class EOT>
00042 class eoDetSelect : public eoSelect<EOT>
00043 {
00044  public:
00046      eoDetSelect(double _rate = 1.0, bool _interpret_as_rate = true)
00047          : howMany(_rate, _interpret_as_rate)  {}
00048 
00055   virtual void operator()(const eoPop<EOT>& _source, eoPop<EOT>& _dest)
00056   {
00057     unsigned int pSize = _source.size();
00058     size_t target = howMany(pSize);
00059 
00060     if ( target == 0 )
00061         {
00062             eo::log << eo::warnings << "Call to a eoHowMany instance returns 0 (target=" << target << ") it will be replaced by 1 to continue." << std::endl;
00063             target = 1;
00064         }
00065 
00066     _dest.resize(target);
00067 
00068     unsigned remain = target % pSize;
00069     unsigned entireCopy = target / pSize;
00070     typename eoPop<EOT>::iterator it = _dest.begin();
00071 
00072     if (target >= pSize)
00073       {
00074         for (unsigned i=0; i<entireCopy; i++)
00075           {
00076               std::copy(_source.begin(), _source.end(), it);
00077             it += pSize;
00078           }
00079       }
00080     // the last ones
00081     if (remain)
00082       {
00083           std::copy(_source.begin(), _source.begin()+remain, it);
00084       }
00085   }
00086 
00087 private :
00088   eoHowMany howMany;
00089 };
00090 
00091 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends