Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_vmanip 5 #define tools_vmanip 6 7 #include <vector> 8 9 namespace tools { 10 11 ////////////////////////////////////////////// 12 /// T* : ///////////////////////////////////// 13 ////////////////////////////////////////////// 14 15 template <class T> 16 inline void safe_clear(std::vector<T*>& a_vec) 17 // the below takes into account the case in 18 // which "delete entry" could modify a_vec. 19 typedef typename std::vector<T*>::iterator i 20 while(!a_vec.empty()) { 21 it_t it = a_vec.begin(); 22 T* entry = *it; 23 a_vec.erase(it); 24 delete entry; 25 } 26 } 27 28 template <class T> 29 inline void safe_reverse_clear(std::vector<T*> 30 // the below takes into account the case in 31 // which "delete entry" could modify a_vec. 32 typedef typename std::vector<T*>::iterator i 33 while(!a_vec.empty()) { 34 it_t it = a_vec.end(); 35 it--; 36 T* entry = *it; 37 a_vec.erase(it); 38 delete entry; 39 } 40 } 41 42 #ifdef TOOLS_DEPRECATED 43 template <class T> inline void clear(std::vect 44 #endif 45 46 template <class T> 47 inline void raw_clear(std::vector<T*>& a_vec){ 48 typedef typename std::vector<T*>::iterator i 49 for(it_t it = a_vec.begin();it!=a_vec.end(); 50 a_vec.clear(); 51 } 52 53 template <class T> 54 inline void copy(std::vector<T*>& a_to,const s 55 raw_clear<T>(a_to); 56 typedef typename std::vector<T*>::const_iter 57 for(it_t it = a_from.begin();it!=a_from.end( 58 a_to.push_back((*it)->copy()); 59 } 60 } 61 62 template <class T> 63 inline void vcopy(std::vector<T*>& a_to,const 64 65 template <class T> 66 inline void append(std::vector<T>& a_vec,const 67 typedef typename std::vector<T>::size_type s 68 sz_t number = a_from.size(); 69 sz_t offset = a_vec.size(); 70 a_vec.resize(offset+number); 71 for(sz_t index=0;index<number;index++,offset 72 a_vec[offset] = a_from[index]; 73 } 74 } 75 76 template <class T> 77 inline void append(std::vector<T>& a_vec,typen 78 typedef typename std::vector<T>::size_type s 79 sz_t vsize = a_vec.size(); 80 a_vec.resize(vsize+a_num); 81 sz_t offset = vsize; 82 for(sz_t index=0;index<a_num;index++,offset+ 83 a_vec[offset] = a_from[index]; 84 } 85 } 86 87 template <class T> 88 inline void removep(std::vector<T*>& a_vec,con 89 typedef typename std::vector<T*>::iterator i 90 for(it_t it=a_vec.begin();it!=a_vec.end();) 91 if(*it==a_elem) { 92 it = a_vec.erase(it); 93 } else { 94 ++it; 95 } 96 } 97 } 98 99 template <class T> 100 inline bool is_inp(const std::vector<T*>& a_ve 101 typedef typename std::vector<T*>::const_iter 102 for(it_t it=a_vec.begin();it!=a_vec.end();++ 103 if(*it==a_item) return true; 104 } 105 return false; 106 } 107 108 ////////////////////////////////////////////// 109 /// T : ////////////////////////////////////// 110 ////////////////////////////////////////////// 111 template <class T> 112 inline void push_back_unique(std::vector<T>& a 113 typedef typename std::vector<T>::const_itera 114 for(it_t it=a_vec.begin();it!=a_vec.end();++ 115 a_vec.push_back(a_v); 116 } 117 118 template <class T> 119 inline bool remove(std::vector<T>& a_vals,cons 120 bool found_some = false; 121 //std::vector<T>::iterator it; 122 //for(it=a_vals.begin();it!=a_vals.end();) { 123 // if(*it==a_elem) { 124 // it = a_vals.erase(it); 125 // found_some = true; 126 // } else { 127 // ++it; 128 // } 129 //} 130 //TOOLS_STL : brut force avoiding erase() : 131 std::vector<T> vs; 132 typedef typename std::vector<T>::iterator it 133 for(it_t it=a_vals.begin();it!=a_vals.end(); 134 if(*it==a_elem) { 135 found_some = true; 136 } else { 137 vs.push_back(*it); 138 } 139 } 140 a_vals = vs; 141 return found_some; 142 } 143 144 template <class T> 145 inline void vfilter(std::vector<T>& a_vals,boo 146 std::vector<T> vs; 147 typedef typename std::vector<T>::iterator it 148 for(it_t it=a_vals.begin();it!=a_vals.end(); 149 if(a_filter_func(*it)) vs.push_back(*it); 150 } 151 a_vals = vs; 152 } 153 154 template <class T> 155 inline void unique(std::vector<T>& a_vec) { 156 typedef typename std::vector<T>::iterator it 157 it_t it,it2; 158 for(it=a_vec.begin();it!=a_vec.end();++it) { 159 it2 = it;it2++; //TOOLS_STL : it2=it+1 doe 160 for(;it2!=a_vec.end();) { 161 if((*it2)==(*it)) { 162 it2 = a_vec.erase(it2); 163 } else { 164 ++it2; 165 } 166 } 167 } 168 } 169 170 template <class T> 171 inline bool is_in(const std::vector<T>& a_vec, 172 typedef typename std::vector<T>::const_itera 173 it_t it; 174 for(it=a_vec.begin();it!=a_vec.end();++it) { 175 if(*it==a_item) return true; 176 } 177 return false; 178 } 179 180 template <class T> 181 inline bool item_index(const std::vector<T>& a 182 a_index = 0; 183 typedef typename std::vector<T>::const_itera 184 it_t it; 185 for(it=a_vec.begin();it!=a_vec.end();++it,a_ 186 if(*it==a_item) return true; 187 } 188 a_index = 0; 189 return false; 190 } 191 192 template <class T> 193 inline bool belong(const std::vector<T>& a_vec 194 typedef typename std::vector<T>::const_itera 195 it_t it; 196 for(it=a_vec.begin();it!=a_vec.end();++it) { 197 if(*it==a_item) return true; 198 } 199 return false; 200 } 201 202 template <class T> 203 inline bool minimum(const std::vector<T>& a_ve 204 if(a_vec.empty()) {a_value = T();return fals 205 a_value = a_vec[0]; 206 typedef typename std::vector<T>::const_itera 207 for(it_t it = a_vec.begin();it!=a_vec.end(); 208 a_value = (a_value<(*it)?a_value:(*it)); 209 } 210 return true; 211 } 212 213 template <class T> 214 inline bool maximum(const std::vector<T>& a_ve 215 if(a_vec.empty()) {a_value = T();return fals 216 a_value = a_vec[0]; 217 typedef typename std::vector<T>::const_itera 218 for(it_t it = a_vec.begin();it!=a_vec.end(); 219 a_value = (a_value>(*it)?a_value:(*it)); 220 } 221 return true; 222 } 223 224 template <class T> 225 inline T sum(const std::vector<T>& a_vec) { 226 T sum = T(); 227 typedef typename std::vector<T>::const_itera 228 for(it_t it = a_vec.begin();it!=a_vec.end(); 229 return sum; 230 } 231 232 template <class T> 233 inline void filter(std::vector<T>& a_vec,unsig 234 unsigned int imx = a_vec.size()-1; 235 unsigned int mx = a_max<imx?a_max:imx; 236 unsigned int i = 0; 237 for(unsigned int index=a_min;index<=mx;index 238 a_vec[i] = a_vec[index];i++; 239 } 240 a_vec.resize(i); 241 } 242 243 template <class T> 244 inline void steps(std::vector<T>& a_vec,unsign 245 a_vec.resize(a_number); 246 for(unsigned int index=0;index<a_number;inde 247 } 248 249 template <class T> 250 inline bool add(std::vector<T>& a_vec,const st 251 if(a_vec.size()!=a_v.size()) return false; 252 typedef typename std::vector<T>::iterator it 253 typedef typename std::vector<T>::const_itera 254 it_t it = a_vec.begin(); 255 cit_t vit = a_v.begin(); 256 for(;it!=a_vec.end();++it,++vit) *it += *vit 257 return true; 258 } 259 260 template <class T> 261 inline bool vequ(const std::vector<T>& a_vec,c 262 if(a_vec.size()!=a_v.size()) return false; 263 typedef typename std::vector<T>::const_itera 264 typedef typename std::vector<T>::const_itera 265 it_t it = a_vec.begin(); 266 cit_t vit = a_v.begin(); 267 //size_t count = 0; 268 for(;it!=a_vec.end();++it,++vit 269 // ,count++ 270 ) { 271 if((*it)!=(*vit)) 272 //{::printf("debug : %lu : %d %d\n",count, 273 return false; 274 //} 275 } 276 return true; 277 } 278 279 template <class T> 280 inline bool vadd(std::vector<T>& a_vec,const s 281 282 template <class T> 283 inline bool sub(std::vector<T>& a_vec,const st 284 if(a_vec.size()!=a_v.size()) return false; 285 typedef typename std::vector<T>::iterator it 286 typedef typename std::vector<T>::const_itera 287 it_t it = a_vec.begin(); 288 cit_t vit = a_v.begin(); 289 for(;it!=a_vec.end();++it,++vit) *it -= *vit 290 return true; 291 } 292 293 template <class T> 294 inline bool div(std::vector<T>& a_vec,const st 295 if(a_vec.size()!=a_v.size()) return false; 296 typedef typename std::vector<T>::iterator it 297 typedef typename std::vector<T>::const_itera 298 it_t it = a_vec.begin(); 299 cit_t vit = a_v.begin(); 300 bool errors = false; 301 for(;it!=a_vec.end();++it,++vit) { 302 if(*vit==T()) { 303 errors = true; 304 } else { 305 *it /= *vit; 306 } 307 } 308 return errors; 309 } 310 311 template <class T> 312 inline void add(std::vector<T>& a_vec,const T& 313 typedef typename std::vector<T>::iterator it 314 for(it_t it=a_vec.begin();it!=a_vec.end();++ 315 } 316 317 template <class T> 318 inline void sub(std::vector<T>& a_vec,const T& 319 typedef typename std::vector<T>::iterator it 320 for(it_t it=a_vec.begin();it!=a_vec.end();++ 321 } 322 323 template <class T> 324 inline void mul(std::vector<T>& a_vec,const T& 325 typedef typename std::vector<T>::iterator it 326 for(it_t it=a_vec.begin();it!=a_vec.end();++ 327 } 328 template <class T> 329 inline bool vmul(std::vector<T>& a_vec,const s 330 331 template <class T> 332 inline void div(std::vector<T>& a_vec,const T& 333 typedef typename std::vector<T>::iterator it 334 for(it_t it=a_vec.begin();it!=a_vec.end();++ 335 } 336 337 /* 338 template <class FROM,class TO> 339 inline std::vector<TO> convert(const std::vect 340 typedef typename std::vector<FROM>::const_it 341 typedef typename std::vector<TO>::iterator i 342 std::vector<TO> to(a_from.size()); 343 const_it_t ait = a_from.begin(); 344 it_t toit = to.begin(); 345 for(;ait!=a_from.end();++ait,++toit) {*toit 346 return to; 347 } 348 */ 349 template <class FROM,class TO> 350 inline void convert(const std::vector<FROM>& a 351 typedef typename std::vector<FROM>::const_it 352 typedef typename std::vector<TO>::iterator i 353 a_to.resize(a_from.size()); 354 const_it_t ait = a_from.begin(); 355 it_t toit = a_to.begin(); 356 for(;ait!=a_from.end();++ait,++toit) {*toit 357 } 358 359 template <class T> 360 inline bool min_max(const std::vector<T>& a_ve 361 if(a_vec.empty()) {a_min=T();a_max=T();retur 362 a_min = *(a_vec.begin()); 363 a_max = a_min; 364 typedef typename std::vector<T>::const_itera 365 for(it_t it = a_vec.begin();it!=a_vec.end(); 366 a_min = *it<a_min?*it:a_min; 367 a_max = *it>a_max?*it:a_max; 368 } 369 return true; 370 } 371 372 ////////////////////////////////////////////// 373 /// T(*a_fabs)(T) : ////////////////////////// 374 ////////////////////////////////////////////// 375 template <class T> 376 inline bool mean_rms(const std::vector<T>& a_v 377 if(a_vec.empty()) {a_mean=T();a_rms=T();retu 378 T S = T(); 379 T S2 = T(); 380 typedef typename std::vector<T>::const_itera 381 for(it_t it = a_vec.begin();it!=a_vec.end(); 382 S += *it; 383 S2 += (*it) * (*it); 384 } 385 a_mean = S/T(a_vec.size()); 386 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a 387 return true; 388 } 389 390 template <class T> 391 inline bool min_max_mean_rms(const std::vector 392 T(*a_sqrt)(T),T(* 393 if(a_vec.empty()) {a_min=T();a_max=T();a_mea 394 a_min = *(a_vec.begin()); 395 a_max = a_min; 396 T S = T(); 397 T S2 = T(); 398 typedef typename std::vector<T>::const_itera 399 for(it_t it = a_vec.begin();it!=a_vec.end(); 400 a_min = *it<a_min?*it:a_min; 401 a_max = *it>a_max?*it:a_max; 402 S += *it; 403 S2 += (*it) * (*it); 404 } 405 a_mean = S/T(a_vec.size()); 406 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a 407 return true; 408 } 409 410 ////////////////////////////////////////////// 411 /// T(*a_fabs)(const T&) : /////////////////// 412 ////////////////////////////////////////////// 413 template <class T> 414 inline bool mean_rms(const std::vector<T>& a_v 415 if(a_vec.empty()) {a_mean=T();a_rms=T();retu 416 T S = T(); 417 T S2 = T(); 418 typedef typename std::vector<T>::const_itera 419 for(it_t it = a_vec.begin();it!=a_vec.end(); 420 S += *it; 421 S2 += (*it) * (*it); 422 } 423 a_mean = S/T(a_vec.size()); 424 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a 425 return true; 426 } 427 428 template <class T> 429 inline bool min_max_mean_rms(const std::vector 430 T(*a_sqrt)(const 431 if(a_vec.empty()) {a_min=T();a_max=T();a_mea 432 a_min = *(a_vec.begin()); 433 a_max = a_min; 434 T S = T(); 435 T S2 = T(); 436 typedef typename std::vector<T>::const_itera 437 for(it_t it = a_vec.begin();it!=a_vec.end(); 438 a_min = *it<a_min?*it:a_min; 439 a_max = *it>a_max?*it:a_max; 440 S += *it; 441 S2 += (*it) * (*it); 442 } 443 a_mean = S/T(a_vec.size()); 444 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a 445 return true; 446 } 447 448 } 449 450 ////////////////////////////////////////////// 451 /// manipulations that induces other includes 452 ////////////////////////////////////////////// 453 #include <ostream> 454 455 namespace tools { 456 457 //NOTE : print is a Python keyword. 458 template <class T> 459 inline void dump(const std::vector<T>& a_vec,s 460 typedef typename std::vector<T>::const_itera 461 it_t it; 462 for(it=a_vec.begin();it!=a_vec.end();++it) { 463 a_out << *it << std::endl; 464 } 465 } 466 467 } 468 469 #endif