/usr/include/sofa/helper/vector.h is in libsofa1-dev 1.0~beta4-12.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Framework *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_HELPER_VECTOR_H
#define SOFA_HELPER_VECTOR_H
#include <vector>
#include <string>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stdlib.h>
#include <typeinfo>
#include <sofa/helper/helper.h>
namespace sofa
{
namespace helper
{
void SOFA_HELPER_API vector_access_failure(const void* vec, unsigned size, unsigned i, const std::type_info& type);
//======================================================================
/** Same as std::vector, + range checking on operator[ ]
Range checking can be turned of using compile option -DNDEBUG
\author Francois Faure, 1999
*/
//======================================================================
template<
class T,
class Alloc = std::allocator<T>
>
class vector: public std::vector<T,Alloc>
{
public:
/// size_type
typedef typename std::vector<T,Alloc>::size_type size_type;
/// reference to a value (read-write)
typedef typename std::vector<T,Alloc>::reference reference;
/// const reference to a value (read only)
typedef typename std::vector<T,Alloc>::const_reference const_reference;
/// Basic onstructor
vector() : std::vector<T,Alloc>() {}
/// Constructor
vector(size_type n, const T& value): std::vector<T,Alloc>(n,value) {}
/// Constructor
vector(int n, const T& value): std::vector<T,Alloc>(n,value) {}
/// Constructor
vector(long n, const T& value): std::vector<T,Alloc>(n,value) {}
/// Constructor
explicit vector(size_type n): std::vector<T,Alloc>(n) {}
/// Constructor
vector(const std::vector<T, Alloc>& x): std::vector<T,Alloc>(x) {}
/// Constructor
vector<T, Alloc>& operator=(const std::vector<T, Alloc>& x)
{
std::vector<T,Alloc>::operator = (x);
return (*this);
}
#ifdef __STL_MEMBER_TEMPLATES
/// Constructor
template <class InputIterator>
vector(InputIterator first, InputIterator last): std::vector<T,Alloc>(first,last){}
#else /* __STL_MEMBER_TEMPLATES */
/// Constructor
vector(typename vector<T,Alloc>::const_iterator first, typename vector<T,Alloc>::const_iterator last): std::vector<T,Alloc>(first,last){}
#endif /* __STL_MEMBER_TEMPLATES */
/// Read/write random access
reference operator[](size_type n) {
#ifndef NDEBUG
if (n>=this->size())
vector_access_failure(this, this->size(), n, typeid(T));
//assert( n<this->size() );
#endif
return *(this->begin() + n);
}
/// Read-only random access
const_reference operator[](size_type n) const {
#ifndef NDEBUG
if (n>=this->size())
vector_access_failure(this, this->size(), n, typeid(T));
//assert( n<this->size() );
#endif
return *(this->begin() + n);
}
std::ostream& write(std::ostream& os) const
{
if( this->size()>0 ){
for( unsigned int i=0; i<this->size()-1; ++i ) os<<(*this)[i]<<" ";
os<<(*this)[this->size()-1];
}
return os;
}
std::istream& read(std::istream& in)
{
T t;
this->clear();
while(in>>t){
this->push_back(t);
}
if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
return in;
}
/// Output stream
inline friend std::ostream& operator<< ( std::ostream& os, const vector<T,Alloc>& vec )
{
return vec.write(os);
}
/// Input stream
inline friend std::istream& operator>> ( std::istream& in, vector<T,Alloc>& vec )
{
return vec.read(in);
}
/// Sets every element to 'value'
void fill( const T& value )
{
std::fill( this->begin(), this->end(), value );
}
};
/// Input stream
/// Specialization for reading vectors of int and unsigned int using "A-B" notation for all integers between A and B, optionnally specifying a step using "A-B-step" notation.
template<>
inline std::istream& vector<int, std::allocator<int> >::read( std::istream& in )
{
int t;
this->clear();
std::string s;
while(in>>s){
std::string::size_type hyphen = s.find_first_of('-',1);
if (hyphen == std::string::npos)
{
t = atoi(s.c_str());
this->push_back(t);
}
else
{
int t1,t2,tinc;
std::string s1(s,0,hyphen);
t1 = atoi(s1.c_str());
std::string::size_type hyphen2 = s.find_first_of('-',hyphen+2);
if (hyphen2 == std::string::npos)
{
std::string s2(s,hyphen+1);
t2 = atoi(s2.c_str());
tinc = (t1<t2) ? 1 : -1;
}
else
{
std::string s2(s,hyphen+1,hyphen2);
std::string s3(s,hyphen2+1);
t2 = atoi(s2.c_str());
tinc = atoi(s3.c_str());
if (tinc == 0)
{
std::cerr << "ERROR parsing \""<<s<<"\": increment is 0\n";
tinc = (t1<t2) ? 1 : -1;
}
if ((t2-t1)*tinc < 0)
{
// increment not of the same sign as t2-t1 : swap t1<->t2
t = t1;
t1 = t2;
t2 = t;
}
}
if (tinc < 0)
for (t=t1;t>=t2;t+=tinc)
this->push_back(t);
else
for (t=t1;t<=t2;t+=tinc)
this->push_back(t);
}
}
if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
return in;
}
/// Output stream
/// Specialization for writing vectors of unsigned char
template<>
inline std::ostream& vector<unsigned char, std::allocator<unsigned char> >::write(std::ostream& os) const
{
if( this->size()>0 ){
for( unsigned int i=0; i<this->size()-1; ++i ) os<<(int)(*this)[i]<<" ";
os<<(int)(*this)[this->size()-1];
}
return os;
}
/// Inpu stream
/// Specialization for writing vectors of unsigned char
template<>
inline std::istream& vector<unsigned char, std::allocator<unsigned char> >::read(std::istream& in)
{
int t;
this->clear();
while(in>>t){
this->push_back((unsigned char)t);
}
if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
return in;
}
/// Input stream
/// Specialization for reading vectors of int and unsigned int using "A-B" notation for all integers between A and B
template<>
inline std::istream& vector<unsigned int, std::allocator<unsigned int> >::read( std::istream& in )
{
unsigned int t;
this->clear();
std::string s;
while(in>>s){
std::string::size_type hyphen = s.find_first_of('-',1);
if (hyphen == std::string::npos)
{
t = atoi(s.c_str());
this->push_back(t);
}
else
{
unsigned int t1,t2;
int tinc;
std::string s1(s,0,hyphen);
t1 = (unsigned int)atoi(s1.c_str());
std::string::size_type hyphen2 = s.find_first_of('-',hyphen+2);
if (hyphen2 == std::string::npos)
{
std::string s2(s,hyphen+1);
t2 = (unsigned int)atoi(s2.c_str());
tinc = (t1<t2) ? 1 : -1;
}
else
{
std::string s2(s,hyphen+1,hyphen2);
std::string s3(s,hyphen2+1);
t2 = (unsigned int)atoi(s2.c_str());
tinc = atoi(s3.c_str());
if (tinc == 0)
{
std::cerr << "ERROR parsing \""<<s<<"\": increment is 0\n";
tinc = (t1<t2) ? 1 : -1;
}
if (((int)(t2-t1))*tinc < 0)
{
// increment not of the same sign as t2-t1 : swap t1<->t2
t = t1;
t1 = t2;
t2 = t;
}
}
if (tinc < 0)
for (t=t1;t>=t2;t=(unsigned int)((int)t+tinc))
this->push_back(t);
else
for (t=t1;t<=t2;t=(unsigned int)((int)t+tinc))
this->push_back(t);
}
}
if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
return in;
}
// ====================== operations on standard vectors
// -----------------------------------------------------------
//
/*! @name vector class-related methods
*/
//
// -----------------------------------------------------------
//@{
/** Remove the first occurence of a given value.
The remaining values are shifted.
*/
template<class T1, class T2>
void remove( T1& v, const T2& elem )
{
typename T1::iterator e = std::find( v.begin(), v.end(), elem );
if( e != v.end() ){
typename T1::iterator next = e;
next++;
for( ; next != v.end(); ++e, ++next )
*e = *next;
}
v.pop_back();
}
/** Remove the first occurence of a given value.
The last value is moved to where the value was found, and the other values are not shifted.
*/
template<class T1, class T2>
void removeValue( T1& v, const T2& elem )
{
typename T1::iterator e = std::find( v.begin(), v.end(), elem );
if( e != v.end() ){
*e = v.back();
v.pop_back();
}
}
/// Remove value at given index, replace it by the value at the last index, other values are not changed
template<class T, class TT>
void removeIndex( std::vector<T,TT>& v, size_t index )
{
#ifndef NDEBUG
//assert( 0<= static_cast<int>(index) && index <v.size() );
if (index>=v.size())
vector_access_failure(&v, v.size(), index, typeid(T));
#endif
v[index] = v.back();
v.pop_back();
}
//@}
} // namespace helper
} // namespace sofa
/*
/// Output stream
template<class T, class Alloc>
std::ostream& operator<< ( std::ostream& os, const std::vector<T,Alloc>& vec )
{
if( vec.size()>0 ){
for( unsigned int i=0; i<vec.size()-1; ++i ) os<<vec[i]<<" ";
os<<vec[vec.size()-1];
}
return os;
}
/// Input stream
template<class T, class Alloc>
std::istream& operator>> ( std::istream& in, std::vector<T,Alloc>& vec )
{
T t;
vec.clear();
while(in>>t){
vec.push_back(t);
}
if( in.rdstate() & std::ios_base::eofbit ) { in.clear(); }
return in;
}
/// Input a pair
template<class T, class U>
std::istream& operator>> ( std::istream& in, std::pair<T,U>& pair )
{
in>>pair.first>>pair.second;
return in;
}
/// Output a pair
template<class T, class U>
std::ostream& operator<< ( std::ostream& out, const std::pair<T,U>& pair )
{
out<<pair.first<<" "<<pair.second;
return out;
}
*/
#endif
|