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