eoVector.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoVector.h
00005 // (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 2000
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mak@dhi.dk
00024 
00025     CVS Info: $Date: 2005/10/06 17:22:40 $ $Header: /cvsroot/eodev/eo/src/eoVector.h,v 1.17 2005/10/06 17:22:40 kuepper Exp $ $Author: kuepper $
00026 
00027  */
00028 //-----------------------------------------------------------------------------
00029 
00030 #ifndef _eoVector_h
00031 #define _eoVector_h
00032 
00033 #include <vector>
00034 #include <iterator>
00035 #include <EO.h>
00036 
00045 template <class FitT, class GeneType>
00046 class eoVector : public EO<FitT>, public std::vector<GeneType>
00047 {
00048 public:
00049 
00050     using EO<FitT>::invalidate;
00051     using std::vector<GeneType>::operator[];
00052     using std::vector<GeneType>::begin;
00053     using std::vector<GeneType>::end;
00054     using std::vector<GeneType>::resize;
00055     using std::vector<GeneType>::size;
00056 
00057     typedef GeneType                AtomType;
00058     typedef std::vector<GeneType>   ContainerType;
00059 
00065     eoVector(unsigned size = 0, GeneType value = GeneType())
00066         : EO<FitT>(), std::vector<GeneType>(size, value)
00067         {}
00068 
00070     template <class OtherFitnessType>
00071     eoVector(const eoVector<OtherFitnessType, GeneType>& _vec) : std::vector<GeneType>(_vec)
00072     {}
00073 
00074   // we can't have a Ctor from a std::vector, it would create ambiguity
00075   //  with the copy Ctor
00076   void value(const std::vector<GeneType>& _v)
00077   {
00078     if (_v.size() != size())       // safety check
00079       {
00080         if (size())                // NOT an initial empty std::vector
00081           std::cout << "Warning: Changing size in eoVector assignation"<<std::endl;
00082         resize(_v.size());
00083       }
00084 
00085     std::copy(_v.begin(), _v.end(), begin());
00086     invalidate();
00087   }
00088 
00090     bool operator<(const eoVector<FitT, GeneType>& _eo) const
00091     {
00092         return EO<FitT>::operator<(_eo);
00093     }
00094 
00096     virtual void printOn(std::ostream& os) const
00097     {
00098         EO<FitT>::printOn(os);
00099         os << ' ';
00100 
00101         os << size() << ' ';
00102 
00103         std::copy(begin(), end(), std::ostream_iterator<AtomType>(os, " "));
00104     }
00105 
00107     virtual void readFrom(std::istream& is)
00108     {
00109         EO<FitT>::readFrom(is);
00110 
00111         unsigned sz;
00112         is >> sz;
00113 
00114         resize(sz);
00115         unsigned i;
00116 
00117         for (i = 0; i < sz; ++i)
00118         {
00119             AtomType atom;
00120             is >> atom;
00121             operator[](i) = atom;
00122         }
00123     }
00124 };
00125 
00127 template <class FitT, class GeneType>
00128 bool operator<(const eoVector<FitT, GeneType>& _eo1, const eoVector<FitT, GeneType>& _eo2)
00129 {
00130     return _eo1.operator<(_eo2);
00131 }
00132 template <class FitT, class GeneType>
00133 bool operator>(const eoVector<FitT, GeneType>& _eo1, const eoVector<FitT, GeneType>& _eo2)
00134 {
00135     return _eo1.operator>(_eo2);
00136 }
00137 
00138 #endif

Generated on Thu Oct 19 05:06:39 2006 for EO by  doxygen 1.3.9.1