Documentation


vector.h

Go to the documentation of this file.
00001 #ifndef ANIMAL_VECTOR_H
00002 #define ANIMAL_VECTOR_H
00003 
00004 #include <vector>
00005 #include <algorithm>
00006 #include <cassert>
00007 
00008 namespace animal {
00009 
00010 //======================================================================
00016 //======================================================================
00017 template<
00018     class T, 
00019     class Alloc = std::allocator<T>
00020     > 
00021 class vector: public std::vector<T,Alloc> 
00022 {
00023 public:
00024 
00026     typedef typename std::vector<T,Alloc>::size_type size_type;
00028     typedef typename std::vector<T,Alloc>::reference reference;
00030     typedef typename std::vector<T,Alloc>::const_reference const_reference;
00031 
00033   vector() : std::vector<T,Alloc>() {}
00035   vector(size_type n, const T& value): std::vector<T,Alloc>(n,value) {}
00037   vector(int n, const T& value): std::vector<T,Alloc>(n,value) {}
00039   vector(long n, const T& value): std::vector<T,Alloc>(n,value) {}
00041   explicit vector(size_type n): std::vector<T,Alloc>(n) {}
00043   vector(const std::vector<T, Alloc>& x): std::vector<T,Alloc>(x) {}
00045   vector<T, Alloc>& operator=(const std::vector<T, Alloc>& x) 
00046     {
00047         std::vector<T,Alloc>::operator = (x);
00048         return (*this);
00049   }
00050 
00051 #ifdef __STL_MEMBER_TEMPLATES
00052 
00053   template <class InputIterator>
00054   vector(InputIterator first, InputIterator last): std::vector<T,Alloc>(first,last){}
00055 #else /* __STL_MEMBER_TEMPLATES */
00056 
00057   vector(typename animal::vector<T,Alloc>::const_iterator first, typename animal::vector<T,Alloc>::const_iterator last): std::vector<T,Alloc>(first,last){}
00058 #endif /* __STL_MEMBER_TEMPLATES */
00059 
00060 
00062 reference operator[](size_type n) { 
00063 #ifndef NDEBUG
00064     assert( n<this->size() );
00065 #endif
00066     return *(this->begin() + n); 
00067   }
00068 
00070 const_reference operator[](size_type n) const { 
00071 #ifndef NDEBUG
00072     assert( n<this->size() );
00073 #endif
00074     return *(this->begin() + n); 
00075   }
00076 
00078 inline friend std::ostream& operator<< ( std::ostream& os, const std::vector<T,Alloc>& vec )
00079 {
00080     if( vec.size()>0 ){
00081         for( unsigned int i=0; i<vec.size()-1; ++i ) os<<vec[i]<<" ";
00082         os<<vec[vec.size()-1];
00083     }
00084     return os;
00085 }
00086 
00087 };
00091 // ======================  operations on standard vectors
00092 
00093 // -----------------------------------------------------------
00094 //  
00098 //  
00099 // -----------------------------------------------------------
00101 
00105 template<class T, class TT> 
00106 void remove( std::vector<T,TT>& v, const T& elem )
00107 {
00108     typename vector<T>::iterator e = std::find( v.begin(), v.end(), elem );
00109     if( e != v.end() ){
00110         typename vector<T>::iterator next = e;
00111         next++;
00112         for( ; next != v.end(); ++e, ++next )
00113             *e = *next;
00114     }
00115         v.pop_back();
00116 }
00117 
00122 template<class T, class TT> 
00123 void removeValue( std::vector<T,TT>& v, const T& elem )
00124 {
00125     typename vector<T>::iterator e = std::find( v.begin(), v.end(), elem );
00126     if( e != v.end() ){
00127         *e = v.back();
00128         v.pop_back();
00129     }
00130 }
00131 
00133 template<class T, class TT> 
00134 void removeIndex( std::vector<T,TT>& v, size_t index )
00135 {
00136 #ifndef NDEBUG
00137     assert( 0<= static_cast<int>(index) && index <v.size() );
00138 #endif
00139     v[index] = v.back();
00140     v.pop_back();
00141 }
00142 
00143 
00144 // /// Dot product of two containers
00145 // template<class C>
00146 // C dot( const C& v1, const C& v2 )
00147 // {
00148 //  C res = 0;
00149 //  typename C::const_iterator i1=v1.begin(), iend, i2=v2.begin();
00150 //  for( iend=v1.end(); i1!=iend; ++i1, ++i2 )
00151 //      res += *i1 * *i2;
00152 //  return res;
00153 // }
00154 
00156 
00157 
00158 
00159 
00160 } // ==============================   end namespace animal
00161 
00162 
00163 
00164 #endif
00165 
00166 

Generated on Thu Dec 23 13:52:27 2004 by doxygen 1.3.6