00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00090 vector(typename vector<T,Alloc>::const_iterator first, typename vector<T,Alloc>::const_iterator last): std::vector<T,Alloc>(first,last){}
00091 #endif
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
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
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
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 }
00325
00326 }
00327
00328
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00354
00355
00356
00357
00358
00359
00360
00362
00363
00364
00365
00366
00367
00368
00369
00370 #endif
00371
00372