EvolvingObjects
eoVector.h
00001 /* (C) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 2000
00002 
00003 This library is free software; you can redistribute it and/or
00004 modify it under the terms of the GNU Lesser General Public
00005 License as published by the Free Software Foundation; either
00006 version 2 of the License, or (at your option) any later version.
00007 
00008 This library is distributed in the hope that it will be useful,
00009 but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 Lesser General Public License for more details.
00012 
00013 You should have received a copy of the GNU Lesser General Public
00014 License along with this library; if not, write to the Free Software
00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 Contact:     eodev-main@lists.sourceforge.net
00018 Old contact: todos@geneura.ugr.es, http://geneura.ugr.es
00019              Marc.Schoenauer@polytechnique.fr
00020              mak@dhi.dk
00021 */
00022 
00023 #ifndef _eoVector_h
00024 #define _eoVector_h
00025 
00026 #include <vector>
00027 #include <iterator>
00028 #include <EO.h>
00029 #include <utils/eoLogger.h>
00030 
00031 
00052 template <class FitT, class GeneType>
00053 class eoVector : public EO<FitT>, public std::vector<GeneType>
00054 {
00055 public:
00056 
00057     using EO<FitT>::invalidate;
00058     using std::vector<GeneType>::operator[];
00059     using std::vector<GeneType>::begin;
00060     using std::vector<GeneType>::end;
00061     using std::vector<GeneType>::resize;
00062     using std::vector<GeneType>::size;
00063 
00064     typedef GeneType                AtomType;
00065     typedef std::vector<GeneType>   ContainerType;
00066 
00072     eoVector(unsigned _size = 0, GeneType _value = GeneType())
00073         : EO<FitT>(), std::vector<GeneType>(_size, _value)
00074         {}
00075 
00077     template <class OtherFitnessType>
00078     eoVector(const eoVector<OtherFitnessType, GeneType>& _vec) : std::vector<GeneType>(_vec)
00079         {}
00080 
00081     // we can't have a Ctor from a std::vector, it would create ambiguity
00082     //  with the copy Ctor
00083     void value(const std::vector<GeneType>& _v)
00084         {
00085             if (_v.size() != size())       // safety check
00086             {
00087                 if (size())                // NOT an initial empty std::vector
00088                     eo::log << eo::warnings << "Warning: Changing size in eoVector assignation" << std::endl;
00089                 resize(_v.size());
00090             }
00091 
00092             std::copy(_v.begin(), _v.end(), begin());
00093             invalidate();
00094         }
00095 
00097     bool operator<(const eoVector<FitT, GeneType>& _eo) const
00098         {
00099             return EO<FitT>::operator<(_eo);
00100         }
00101 
00103     virtual void printOn(std::ostream& os) const
00104         {
00105             EO<FitT>::printOn(os);
00106             os << ' ';
00107 
00108             os << size() << ' ';
00109 
00110             std::copy(begin(), end(), std::ostream_iterator<AtomType>(os, " "));
00111         }
00112 
00114     virtual void readFrom(std::istream& is)
00115         {
00116             EO<FitT>::readFrom(is);
00117 
00118             unsigned sz;
00119             is >> sz;
00120 
00121             resize(sz);
00122             unsigned i;
00123 
00124             for (i = 0; i < sz; ++i)
00125             {
00126                 AtomType atom;
00127                 is >> atom;
00128                 operator[](i) = atom;
00129             }
00130         }
00131 };
00140 template <class FitT, class GeneType>
00141 bool operator<(const eoVector<FitT, GeneType>& _eo1, const eoVector<FitT, GeneType>& _eo2)
00142 {
00143     return _eo1.operator<(_eo2);
00144 }
00145 
00146 
00152 template <class FitT, class GeneType>
00153 bool operator>(const eoVector<FitT, GeneType>& _eo1, const eoVector<FitT, GeneType>& _eo2)
00154 {
00155     return _eo1.operator>(_eo2);
00156 }
00157 
00158 
00159 #endif
00160 
00163 // Local Variables:
00164 // coding: iso-8859-1
00165 // mode: C++
00166 // c-file-offsets: ((c . 0))
00167 // c-file-style: "Stroustrup"
00168 // fill-column: 80
00169 // End:
 All Classes Namespaces Files Functions Variables Typedefs Friends