/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- ----------------------------------------------------------------------------- eoVector.h Turns an STL vector into an EO (c) GeNeura Team, 1998 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: todos@geneura.ugr.es, http://geneura.ugr.es */ //----------------------------------------------------------------------------- #ifndef _eoVector_H #define _eoVector_H // STL libraries #include // For vector #include #include #include #include /** Adaptor that turns an STL vector into an EO with the same gene type as the type with which the vector has been instantiated */ template class eoVector: public eo1d, public vector { public: typedef T Type ; /// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator //@{ /** Ctor. @param _size Lineal length of the object @param _val Common initial value */ eoVector( unsigned _size = 0, T _val = 0) : eo1d(), vector( _size, _val ){ }; /** Ctor using a random number generator @param _size Lineal length of the object @param _rnd a random number generator, which returns a random value each time it´s called */ eoVector( unsigned _size, eoRnd& _rnd ); /** Ctor from a istream. The T class should accept reading from a istream. It doesn't read fitness, which is supposed to be dynamic and dependent on environment. @param _is the input stream; should have all values in a single line, separated by whitespace */ eoVector( istream& _is); /// copy ctor eoVector( const eoVector & _eo ) : eo1d( _eo ), vector( _eo ){ }; /// Assignment operator const eoVector& operator =( const eoVector & _eo ) { if ( this != &_eo ){ eo1d::operator=( _eo ); vector::operator=( _eo ); } return *this; } /// dtor virtual ~eoVector() {}; //@} /** methods that implement the eo1d protocol @exception out_of_range if _i is larger than EO´s size */ virtual T getGene( unsigned _i ) const { if ( _i >= length() ) throw out_of_range( "out_of_range when reading gene"); return (*this)[_i]; }; /** methods that implement the eo1d protocol @exception out_of_range if _i is larger than EO´s size */ virtual void setGene( unsigned _i, const T& _value ) { if ( _i >= size() ) throw out_of_range( "out_of_range when writing a gene"); (*this)[_i] = _value; }; /** methods that implement the eo1d protocol @exception out_of_range if _i is larger than EO´s size */ virtual void insertGene( unsigned _i, T _val ) { if (_i <= size() ) { vector::iterator i = begin()+_i; insert( i, _val ); } else { throw out_of_range( "out_of_range when inserting a gene"); } }; /** Eliminates the gene at position _i @exception out_of_range if _i is larger than EO´s size */ virtual void deleteGene( unsigned _i ) { if (_i < this->size() ) { vector::iterator i = this->begin()+_i; this->erase( i ); } else { throw out_of_range( "out_of_range when deleting a gene"); }; }; /// methods that implement the EO protocol virtual unsigned length() const { return this->size(); }; /** @name Methods from eoObject readFrom and printOn are directly inherited from eo1d */ //@{ /** Inherited from eoObject @see eoObject */ string className() const {return "eoVector";}; //@} }; //____________________________ Some method implementation _______________________ // Ctors______________________________________________________________________________ //____________________________________________________________________________________ template eoVector::eoVector( unsigned _size, eoRnd& _rnd ) : eo1d(), vector( _size ){ for ( iterator i = begin(); i != end(); i ++ ) { *i = _rnd(); } }; //____________________________________________________________________________________ template eoVector::eoVector( istream& _is) : eo1d(), vector( ){ while (_is ) { T tmp; _is >> tmp; push_back( tmp ); } }; #endif