vector.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #ifndef SOFA_HELPER_VECTOR_H
00028 #define SOFA_HELPER_VECTOR_H
00029 
00030 #include <vector>
00031 #include <string>
00032 #include <algorithm>
00033 #include <cassert>
00034 #include <iostream>
00035 #include <stdlib.h>
00036 
00037 namespace sofa
00038 {
00039 
00040 namespace helper
00041 {
00042 
00043 //======================================================================
00049 //======================================================================
00050 template<
00051     class T, 
00052     class Alloc = std::allocator<T>
00053     > 
00054 class vector: public std::vector<T,Alloc> 
00055 {
00056 public:
00057 
00059     typedef typename std::vector<T,Alloc>::size_type size_type;
00061     typedef typename std::vector<T,Alloc>::reference reference;
00063     typedef typename std::vector<T,Alloc>::const_reference const_reference;
00064 
00066   vector() : std::vector<T,Alloc>() {}
00068   vector(size_type n, const T& value): std::vector<T,Alloc>(n,value) {}
00070   vector(int n, const T& value): std::vector<T,Alloc>(n,value) {}
00072   vector(long n, const T& value): std::vector<T,Alloc>(n,value) {}
00074   explicit vector(size_type n): std::vector<T,Alloc>(n) {}
00076   vector(const std::vector<T, Alloc>& x): std::vector<T,Alloc>(x) {}
00078   vector<T, Alloc>& operator=(const std::vector<T, Alloc>& x) 
00079     {
00080         std::vector<T,Alloc>::operator = (x);
00081         return (*this);
00082   }
00083 
00084 #ifdef __STL_MEMBER_TEMPLATES
00086   template <class InputIterator>
00087   vector(InputIterator first, InputIterator last): std::vector<T,Alloc>(first,last){}
00088 #else /* __STL_MEMBER_TEMPLATES */
00090   vector(typename vector<T,Alloc>::const_iterator first, typename vector<T,Alloc>::const_iterator last): std::vector<T,Alloc>(first,last){}
00091 #endif /* __STL_MEMBER_TEMPLATES */
00092 
00093 
00095 reference operator[](size_type n) { 
00096 #ifndef NDEBUG
00097     assert( n<this->size() );
00098 #endif
00099     return *(this->begin() + n); 
00100   }
00101 
00103 const_reference operator[](size_type n) const { 
00104 #ifndef NDEBUG
00105     assert( n<this->size() );
00106 #endif
00107     return *(this->begin() + n); 
00108   }
00109 
00110   std::ostream& write(std::ostream& os) const
00111   {
00112       if( this->size()>0 ){
00113           for( unsigned int i=0; i<this->size()-1; ++i ) os<<(*this)[i]<<" ";
00114           os<<(*this)[this->size()-1];
00115       }
00116       return os;
00117   }
00118 
00119   std::istream& read(std::istream& in)
00120   {
00121       T t;
00122       this->clear();
00123       while(in>>t){
00124           this->push_back(t);
00125       }
00126       if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
00127       return in;
00128   }
00129 
00131   inline friend std::ostream& operator<< ( std::ostream& os, const vector<T,Alloc>& vec )
00132   {
00133       return vec.write(os);
00134   }
00135 
00137   inline friend std::istream& operator>> ( std::istream& in, vector<T,Alloc>& vec )
00138   {
00139       return vec.read(in);
00140   }
00141   
00143   void fill( const T& value )
00144   {
00145       std::fill( this->begin(), this->end(), value );
00146   }
00147 
00148 };
00149 
00152 template<>
00153 inline std::istream& vector<int, std::allocator<int> >::read( std::istream& in )
00154 {
00155     int t;
00156     this->clear();
00157     std::string s;
00158     while(in>>s){
00159         std::string::size_type hyphen = s.find_first_of('-',1);
00160         if (hyphen == std::string::npos)
00161         {
00162             t = atoi(s.c_str());
00163             this->push_back(t);
00164         }
00165         else
00166         {
00167             int t1,t2,tinc;
00168             std::string s1(s,0,hyphen);
00169             t1 = atoi(s1.c_str());
00170             std::string::size_type hyphen2 = s.find_first_of('-',hyphen+2);
00171             if (hyphen2 == std::string::npos)
00172             {
00173                 std::string s2(s,hyphen+1);
00174                 t2 = atoi(s2.c_str());
00175                 tinc = (t1<t2) ? 1 : -1;
00176             }
00177             else
00178             {
00179                 std::string s2(s,hyphen+1,hyphen2);
00180                 std::string s3(s,hyphen2+1);
00181                 t2 = atoi(s2.c_str());
00182                 tinc = atoi(s3.c_str());
00183                 if (tinc == 0)
00184                 {
00185                     std::cerr << "ERROR parsing \""<<s<<"\": increment is 0\n";
00186                     tinc = (t1<t2) ? 1 : -1;
00187                 }
00188                 if ((t2-t1)*tinc < 0)
00189                 {
00190                     // increment not of the same sign as t2-t1 : swap t1<->t2
00191                     t = t1;
00192                     t1 = t2;
00193                     t2 = t;
00194                 }
00195             }
00196             if (tinc < 0)
00197                 for (t=t1;t>=t2;t+=tinc)
00198                     this->push_back(t);
00199             else
00200                 for (t=t1;t<=t2;t+=tinc)
00201                     this->push_back(t);
00202         }
00203     }
00204     if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
00205     return in;
00206 }
00207 
00210 template<>
00211 inline std::istream& vector<unsigned int, std::allocator<unsigned int> >::read( std::istream& in )
00212 {
00213     unsigned int t;
00214     this->clear();
00215     std::string s;
00216     while(in>>s){
00217         std::string::size_type hyphen = s.find_first_of('-',1);
00218         if (hyphen == std::string::npos)
00219         {
00220             t = atoi(s.c_str());
00221             this->push_back(t);
00222         }
00223         else
00224         {
00225             unsigned int t1,t2;
00226             int tinc;
00227             std::string s1(s,0,hyphen);
00228             t1 = (unsigned int)atoi(s1.c_str());
00229             std::string::size_type hyphen2 = s.find_first_of('-',hyphen+2);
00230             if (hyphen2 == std::string::npos)
00231             {
00232                 std::string s2(s,hyphen+1);
00233                 t2 = (unsigned int)atoi(s2.c_str());
00234                 tinc = (t1<t2) ? 1 : -1;
00235             }
00236             else
00237             {
00238                 std::string s2(s,hyphen+1,hyphen2);
00239                 std::string s3(s,hyphen2+1);
00240                 t2 = (unsigned int)atoi(s2.c_str());
00241                 tinc = atoi(s3.c_str());
00242                 if (tinc == 0)
00243                 {
00244                     std::cerr << "ERROR parsing \""<<s<<"\": increment is 0\n";
00245                     tinc = (t1<t2) ? 1 : -1;
00246                 }
00247                 if (((int)(t2-t1))*tinc < 0)
00248                 {
00249                     // increment not of the same sign as t2-t1 : swap t1<->t2
00250                     t = t1;
00251                     t1 = t2;
00252                     t2 = t;
00253                 }
00254             }
00255             if (tinc < 0)
00256                 for (t=t1;t>=t2;t=(unsigned int)((int)t+tinc))
00257                     this->push_back(t);
00258             else
00259                 for (t=t1;t<=t2;t=(unsigned int)((int)t+tinc))
00260                     this->push_back(t);
00261         }
00262     }
00263     if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
00264     return in;
00265 }
00266 
00267 
00268 // ======================  operations on standard vectors
00269 
00270 // -----------------------------------------------------------
00271 //  
00275 //  
00276 // -----------------------------------------------------------
00278 
00282 template<class T1, class T2> 
00283 void remove( T1& v, const T2& elem )
00284 {
00285     typename T1::iterator e = std::find( v.begin(), v.end(), elem );
00286     if( e != v.end() ){
00287         typename T1::iterator next = e;
00288         next++;
00289         for( ; next != v.end(); ++e, ++next )
00290             *e = *next;
00291     }
00292         v.pop_back();
00293 }
00294 
00299 template<class T1, class T2> 
00300 void removeValue( T1& v, const T2& elem )
00301 {
00302     typename T1::iterator e = std::find( v.begin(), v.end(), elem );
00303     if( e != v.end() ){
00304         *e = v.back();
00305         v.pop_back();
00306     }
00307 }
00308 
00310 template<class T, class TT> 
00311 void removeIndex( std::vector<T,TT>& v, size_t index )
00312 {
00313 #ifndef NDEBUG
00314     assert( 0<= static_cast<int>(index) && index <v.size() );
00315 #endif
00316     v[index] = v.back();
00317     v.pop_back();
00318 }
00319 
00320 
00321 
00323 
00324 } // namespace helper
00325 
00326 } // namespace sofa
00327 
00328 /*
00330 template<class T, class Alloc>
00331   std::ostream& operator<< ( std::ostream& os, const std::vector<T,Alloc>& vec )
00332 {
00333   if( vec.size()>0 ){
00334     for( unsigned int i=0; i<vec.size()-1; ++i ) os<<vec[i]<<" ";
00335     os<<vec[vec.size()-1];
00336   }
00337   return os;
00338 }
00339 
00341 template<class T, class Alloc>
00342     std::istream& operator>> ( std::istream& in, std::vector<T,Alloc>& vec )
00343 {
00344   T t;
00345   vec.clear();
00346   while(in>>t){
00347     vec.push_back(t);
00348   }
00349   if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
00350   return in;
00351 }
00352 
00354 template<class T, class U>
00355     std::istream& operator>> ( std::istream& in, std::pair<T,U>& pair )
00356 {
00357   in>>pair.first>>pair.second;
00358   return in;
00359 }
00360 
00362 template<class T, class U>
00363     std::ostream& operator<< ( std::ostream& out, const std::pair<T,U>& pair )
00364 {
00365   out<<pair.first<<" "<<pair.second;
00366   return out;
00367 }
00368 */
00369 
00370 #endif
00371 
00372 

Generated on Sun Nov 23 02:29:31 2008 for Sofa by  doxygen 1.5.7.1