Warning, /include/Geant4/tools/vmanip is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_vmanip
0005 #define tools_vmanip
0006
0007 #include <vector>
0008
0009 namespace tools {
0010
0011 //////////////////////////////////////////////////////////
0012 /// T* : /////////////////////////////////////////////////
0013 //////////////////////////////////////////////////////////
0014
0015 template <class T>
0016 inline void safe_clear(std::vector<T*>& a_vec){
0017 // the below takes into account the case in
0018 // which "delete entry" could modify a_vec.
0019 typedef typename std::vector<T*>::iterator it_t;
0020 while(!a_vec.empty()) {
0021 it_t it = a_vec.begin();
0022 T* entry = *it;
0023 a_vec.erase(it);
0024 delete entry;
0025 }
0026 }
0027
0028 template <class T>
0029 inline void safe_reverse_clear(std::vector<T*>& a_vec){ //used in sg/group.
0030 // the below takes into account the case in
0031 // which "delete entry" could modify a_vec.
0032 typedef typename std::vector<T*>::iterator it_t;
0033 while(!a_vec.empty()) {
0034 it_t it = a_vec.end();
0035 it--;
0036 T* entry = *it;
0037 a_vec.erase(it);
0038 delete entry;
0039 }
0040 }
0041
0042 #ifdef TOOLS_DEPRECATED
0043 template <class T> inline void clear(std::vector<T*>& a_vec){safe_clear<T>(a_vec);}
0044 #endif
0045
0046 template <class T>
0047 inline void raw_clear(std::vector<T*>& a_vec){
0048 typedef typename std::vector<T*>::iterator it_t;
0049 for(it_t it = a_vec.begin();it!=a_vec.end();++it) delete *it;
0050 a_vec.clear();
0051 }
0052
0053 template <class T>
0054 inline void copy(std::vector<T*>& a_to,const std::vector<T*>& a_from){
0055 raw_clear<T>(a_to);
0056 typedef typename std::vector<T*>::const_iterator it_t;
0057 for(it_t it = a_from.begin();it!=a_from.end();++it) {
0058 a_to.push_back((*it)->copy());
0059 }
0060 }
0061
0062 template <class T>
0063 inline void vcopy(std::vector<T*>& a_to,const std::vector<T*>& a_from) {return copy<T>(a_to,a_from);}
0064
0065 template <class T>
0066 inline void append(std::vector<T>& a_vec,const std::vector<T>& a_from) {
0067 typedef typename std::vector<T>::size_type sz_t;
0068 sz_t number = a_from.size();
0069 sz_t offset = a_vec.size();
0070 a_vec.resize(offset+number);
0071 for(sz_t index=0;index<number;index++,offset++) {
0072 a_vec[offset] = a_from[index];
0073 }
0074 }
0075
0076 template <class T>
0077 inline void append(std::vector<T>& a_vec,typename std::vector<T>::size_type a_num,const T* a_from) {
0078 typedef typename std::vector<T>::size_type sz_t;
0079 sz_t vsize = a_vec.size();
0080 a_vec.resize(vsize+a_num);
0081 sz_t offset = vsize;
0082 for(sz_t index=0;index<a_num;index++,offset++) {
0083 a_vec[offset] = a_from[index];
0084 }
0085 }
0086
0087 template <class T>
0088 inline void removep(std::vector<T*>& a_vec,const T* a_elem) {
0089 typedef typename std::vector<T*>::iterator it_t;
0090 for(it_t it=a_vec.begin();it!=a_vec.end();) {
0091 if(*it==a_elem) {
0092 it = a_vec.erase(it);
0093 } else {
0094 ++it;
0095 }
0096 }
0097 }
0098
0099 template <class T>
0100 inline bool is_inp(const std::vector<T*>& a_vec,const T* a_item){
0101 typedef typename std::vector<T*>::const_iterator it_t;
0102 for(it_t it=a_vec.begin();it!=a_vec.end();++it) {
0103 if(*it==a_item) return true;
0104 }
0105 return false;
0106 }
0107
0108 ////////////////////////////////////////////////////////////////////////////
0109 /// T : ////////////////////////////////////////////////////////////////////
0110 ////////////////////////////////////////////////////////////////////////////
0111 template <class T>
0112 inline void push_back_unique(std::vector<T>& a_vec,const T& a_v) {
0113 typedef typename std::vector<T>::const_iterator it_t;
0114 for(it_t it=a_vec.begin();it!=a_vec.end();++it) {if(*it==a_v) return;}
0115 a_vec.push_back(a_v);
0116 }
0117
0118 template <class T>
0119 inline bool remove(std::vector<T>& a_vals,const T& a_elem){
0120 bool found_some = false;
0121 //std::vector<T>::iterator it;
0122 //for(it=a_vals.begin();it!=a_vals.end();) {
0123 // if(*it==a_elem) {
0124 // it = a_vals.erase(it);
0125 // found_some = true;
0126 // } else {
0127 // ++it;
0128 // }
0129 //}
0130 //TOOLS_STL : brut force avoiding erase() :
0131 std::vector<T> vs;
0132 typedef typename std::vector<T>::iterator it_t;
0133 for(it_t it=a_vals.begin();it!=a_vals.end();++it) {
0134 if(*it==a_elem) {
0135 found_some = true;
0136 } else {
0137 vs.push_back(*it);
0138 }
0139 }
0140 a_vals = vs;
0141 return found_some;
0142 }
0143
0144 template <class T>
0145 inline void vfilter(std::vector<T>& a_vals,bool(*a_filter_func)(const T& a_elem)){
0146 std::vector<T> vs;
0147 typedef typename std::vector<T>::iterator it_t;
0148 for(it_t it=a_vals.begin();it!=a_vals.end();++it) {
0149 if(a_filter_func(*it)) vs.push_back(*it);
0150 }
0151 a_vals = vs;
0152 }
0153
0154 template <class T>
0155 inline void unique(std::vector<T>& a_vec) {
0156 typedef typename std::vector<T>::iterator it_t;
0157 it_t it,it2;
0158 for(it=a_vec.begin();it!=a_vec.end();++it) {
0159 it2 = it;it2++; //TOOLS_STL : it2=it+1 does not compile.
0160 for(;it2!=a_vec.end();) {
0161 if((*it2)==(*it)) {
0162 it2 = a_vec.erase(it2);
0163 } else {
0164 ++it2;
0165 }
0166 }
0167 }
0168 }
0169
0170 template <class T>
0171 inline bool is_in(const std::vector<T>& a_vec,const T& a_item){
0172 typedef typename std::vector<T>::const_iterator it_t;
0173 it_t it;
0174 for(it=a_vec.begin();it!=a_vec.end();++it) {
0175 if(*it==a_item) return true;
0176 }
0177 return false;
0178 }
0179
0180 template <class T>
0181 inline bool item_index(const std::vector<T>& a_vec,const T& a_item,unsigned int& a_index){
0182 a_index = 0;
0183 typedef typename std::vector<T>::const_iterator it_t;
0184 it_t it;
0185 for(it=a_vec.begin();it!=a_vec.end();++it,a_index++) {
0186 if(*it==a_item) return true;
0187 }
0188 a_index = 0;
0189 return false;
0190 }
0191
0192 template <class T>
0193 inline bool belong(const std::vector<T>& a_vec,const T& a_item){
0194 typedef typename std::vector<T>::const_iterator it_t;
0195 it_t it;
0196 for(it=a_vec.begin();it!=a_vec.end();++it) {
0197 if(*it==a_item) return true;
0198 }
0199 return false;
0200 }
0201
0202 template <class T>
0203 inline bool minimum(const std::vector<T>& a_vec,T& a_value) {
0204 if(a_vec.empty()) {a_value = T();return false;}
0205 a_value = a_vec[0];
0206 typedef typename std::vector<T>::const_iterator it_t;
0207 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0208 a_value = (a_value<(*it)?a_value:(*it));
0209 }
0210 return true;
0211 }
0212
0213 template <class T>
0214 inline bool maximum(const std::vector<T>& a_vec,T& a_value) {
0215 if(a_vec.empty()) {a_value = T();return false;}
0216 a_value = a_vec[0];
0217 typedef typename std::vector<T>::const_iterator it_t;
0218 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0219 a_value = (a_value>(*it)?a_value:(*it));
0220 }
0221 return true;
0222 }
0223
0224 template <class T>
0225 inline T sum(const std::vector<T>& a_vec) {
0226 T sum = T();
0227 typedef typename std::vector<T>::const_iterator it_t;
0228 for(it_t it = a_vec.begin();it!=a_vec.end();++it) sum += *it;
0229 return sum;
0230 }
0231
0232 template <class T>
0233 inline void filter(std::vector<T>& a_vec,unsigned int a_min,unsigned int a_max){
0234 unsigned int imx = a_vec.size()-1;
0235 unsigned int mx = a_max<imx?a_max:imx;
0236 unsigned int i = 0;
0237 for(unsigned int index=a_min;index<=mx;index++) {
0238 a_vec[i] = a_vec[index];i++;
0239 }
0240 a_vec.resize(i);
0241 }
0242
0243 template <class T>
0244 inline void steps(std::vector<T>& a_vec,unsigned int a_number){
0245 a_vec.resize(a_number);
0246 for(unsigned int index=0;index<a_number;index++) a_vec[index] = T(index);
0247 }
0248
0249 template <class T>
0250 inline bool add(std::vector<T>& a_vec,const std::vector<T>& a_v){
0251 if(a_vec.size()!=a_v.size()) return false;
0252 typedef typename std::vector<T>::iterator it_t;
0253 typedef typename std::vector<T>::const_iterator cit_t;
0254 it_t it = a_vec.begin();
0255 cit_t vit = a_v.begin();
0256 for(;it!=a_vec.end();++it,++vit) *it += *vit;
0257 return true;
0258 }
0259
0260 template <class T>
0261 inline bool vequ(const std::vector<T>& a_vec,const std::vector<T>& a_v){
0262 if(a_vec.size()!=a_v.size()) return false;
0263 typedef typename std::vector<T>::const_iterator it_t;
0264 typedef typename std::vector<T>::const_iterator cit_t;
0265 it_t it = a_vec.begin();
0266 cit_t vit = a_v.begin();
0267 //size_t count = 0;
0268 for(;it!=a_vec.end();++it,++vit
0269 // ,count++
0270 ) {
0271 if((*it)!=(*vit))
0272 //{::printf("debug : %lu : %d %d\n",count,(*it),(*vit));
0273 return false;
0274 //}
0275 }
0276 return true;
0277 }
0278
0279 template <class T>
0280 inline bool vadd(std::vector<T>& a_vec,const std::vector<T>& a_v) {return add<T>(a_vec,a_v);}
0281
0282 template <class T>
0283 inline bool sub(std::vector<T>& a_vec,const std::vector<T>& a_v){
0284 if(a_vec.size()!=a_v.size()) return false;
0285 typedef typename std::vector<T>::iterator it_t;
0286 typedef typename std::vector<T>::const_iterator cit_t;
0287 it_t it = a_vec.begin();
0288 cit_t vit = a_v.begin();
0289 for(;it!=a_vec.end();++it,++vit) *it -= *vit;
0290 return true;
0291 }
0292
0293 template <class T>
0294 inline bool div(std::vector<T>& a_vec,const std::vector<T>& a_v){
0295 if(a_vec.size()!=a_v.size()) return false;
0296 typedef typename std::vector<T>::iterator it_t;
0297 typedef typename std::vector<T>::const_iterator cit_t;
0298 it_t it = a_vec.begin();
0299 cit_t vit = a_v.begin();
0300 bool errors = false;
0301 for(;it!=a_vec.end();++it,++vit) {
0302 if(*vit==T()) {
0303 errors = true;
0304 } else {
0305 *it /= *vit;
0306 }
0307 }
0308 return errors;
0309 }
0310
0311 template <class T>
0312 inline void add(std::vector<T>& a_vec,const T& a_v){
0313 typedef typename std::vector<T>::iterator it_t;
0314 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it += a_v;
0315 }
0316
0317 template <class T>
0318 inline void sub(std::vector<T>& a_vec,const T& a_v){
0319 typedef typename std::vector<T>::iterator it_t;
0320 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it -= a_v;
0321 }
0322
0323 template <class T>
0324 inline void mul(std::vector<T>& a_vec,const T& a_v){
0325 typedef typename std::vector<T>::iterator it_t;
0326 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it *= a_v;
0327 }
0328 template <class T>
0329 inline bool vmul(std::vector<T>& a_vec,const std::vector<T>& a_v) {return vmul<T>(a_vec,a_v);}
0330
0331 template <class T>
0332 inline void div(std::vector<T>& a_vec,const T& a_v){
0333 typedef typename std::vector<T>::iterator it_t;
0334 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it /= a_v;
0335 }
0336
0337 /*
0338 template <class FROM,class TO>
0339 inline std::vector<TO> convert(const std::vector<FROM>& a_from){
0340 typedef typename std::vector<FROM>::const_iterator const_it_t;
0341 typedef typename std::vector<TO>::iterator it_t;
0342 std::vector<TO> to(a_from.size());
0343 const_it_t ait = a_from.begin();
0344 it_t toit = to.begin();
0345 for(;ait!=a_from.end();++ait,++toit) {*toit = (TO)*ait;}
0346 return to;
0347 }
0348 */
0349 template <class FROM,class TO>
0350 inline void convert(const std::vector<FROM>& a_from,std::vector<TO>& a_to) {
0351 typedef typename std::vector<FROM>::const_iterator const_it_t;
0352 typedef typename std::vector<TO>::iterator it_t;
0353 a_to.resize(a_from.size());
0354 const_it_t ait = a_from.begin();
0355 it_t toit = a_to.begin();
0356 for(;ait!=a_from.end();++ait,++toit) {*toit = (TO)*ait;}
0357 }
0358
0359 template <class T>
0360 inline bool min_max(const std::vector<T>& a_vec,T& a_min,T& a_max) {
0361 if(a_vec.empty()) {a_min=T();a_max=T();return false;}
0362 a_min = *(a_vec.begin());
0363 a_max = a_min;
0364 typedef typename std::vector<T>::const_iterator it_t;
0365 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0366 a_min = *it<a_min?*it:a_min;
0367 a_max = *it>a_max?*it:a_max;
0368 }
0369 return true;
0370 }
0371
0372 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
0373 /// T(*a_fabs)(T) : ///////////////////////////////////////////////////////////////////////////////////////////
0374 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
0375 template <class T>
0376 inline bool mean_rms(const std::vector<T>& a_vec,T& a_mean,T& a_rms,T(*a_sqrt)(T),T(*a_fabs)(T)) {
0377 if(a_vec.empty()) {a_mean=T();a_rms=T();return false;}
0378 T S = T();
0379 T S2 = T();
0380 typedef typename std::vector<T>::const_iterator it_t;
0381 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0382 S += *it;
0383 S2 += (*it) * (*it);
0384 }
0385 a_mean = S/T(a_vec.size());
0386 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a_mean * a_mean));
0387 return true;
0388 }
0389
0390 template <class T>
0391 inline bool min_max_mean_rms(const std::vector<T>& a_vec,T& a_min,T& a_max,T& a_mean,T& a_rms,
0392 T(*a_sqrt)(T),T(*a_fabs)(T)) {
0393 if(a_vec.empty()) {a_min=T();a_max=T();a_mean=T();a_rms=T();return false;}
0394 a_min = *(a_vec.begin());
0395 a_max = a_min;
0396 T S = T();
0397 T S2 = T();
0398 typedef typename std::vector<T>::const_iterator it_t;
0399 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0400 a_min = *it<a_min?*it:a_min;
0401 a_max = *it>a_max?*it:a_max;
0402 S += *it;
0403 S2 += (*it) * (*it);
0404 }
0405 a_mean = S/T(a_vec.size());
0406 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a_mean * a_mean));
0407 return true;
0408 }
0409
0410 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
0411 /// T(*a_fabs)(const T&) : ///////////////////////////////////////////////////////////////////////////////////////////
0412 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
0413 template <class T>
0414 inline bool mean_rms(const std::vector<T>& a_vec,T& a_mean,T& a_rms,T(*a_sqrt)(const T&),T(*a_fabs)(const T&)) {
0415 if(a_vec.empty()) {a_mean=T();a_rms=T();return false;}
0416 T S = T();
0417 T S2 = T();
0418 typedef typename std::vector<T>::const_iterator it_t;
0419 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0420 S += *it;
0421 S2 += (*it) * (*it);
0422 }
0423 a_mean = S/T(a_vec.size());
0424 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a_mean * a_mean));
0425 return true;
0426 }
0427
0428 template <class T>
0429 inline bool min_max_mean_rms(const std::vector<T>& a_vec,T& a_min,T& a_max,T& a_mean,T& a_rms,
0430 T(*a_sqrt)(const T&),T(*a_fabs)(const T&)) {
0431 if(a_vec.empty()) {a_min=T();a_max=T();a_mean=T();a_rms=T();return false;}
0432 a_min = *(a_vec.begin());
0433 a_max = a_min;
0434 T S = T();
0435 T S2 = T();
0436 typedef typename std::vector<T>::const_iterator it_t;
0437 for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
0438 a_min = *it<a_min?*it:a_min;
0439 a_max = *it>a_max?*it:a_max;
0440 S += *it;
0441 S2 += (*it) * (*it);
0442 }
0443 a_mean = S/T(a_vec.size());
0444 a_rms = a_sqrt(a_fabs(S2/T(a_vec.size()) - a_mean * a_mean));
0445 return true;
0446 }
0447
0448 }
0449
0450 ///////////////////////////////////////////////////////////
0451 /// manipulations that induces other includes : ///////////
0452 ///////////////////////////////////////////////////////////
0453 #include <ostream>
0454
0455 namespace tools {
0456
0457 //NOTE : print is a Python keyword.
0458 template <class T>
0459 inline void dump(const std::vector<T>& a_vec,std::ostream& a_out){
0460 typedef typename std::vector<T>::const_iterator it_t;
0461 it_t it;
0462 for(it=a_vec.begin();it!=a_vec.end();++it) {
0463 a_out << *it << std::endl;
0464 }
0465 }
0466
0467 }
0468
0469 #endif