g4tools  5.4.0
vmanip
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
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 
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 it_t;
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*>& a_vec){ //used in sg/group.
30  // the below takes into account the case in
31  // which "delete entry" could modify a_vec.
32  typedef typename std::vector<T*>::iterator it_t;
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::vector<T*>& a_vec){safe_clear<T>(a_vec);}
44 #endif
45 
46 template <class T>
47 inline void raw_clear(std::vector<T*>& a_vec){
48  typedef typename std::vector<T*>::iterator it_t;
49  for(it_t it = a_vec.begin();it!=a_vec.end();++it) delete *it;
50  a_vec.clear();
51 }
52 
53 template <class T>
54 inline void copy(std::vector<T*>& a_to,const std::vector<T*>& a_from){
55  raw_clear<T>(a_to);
56  typedef typename std::vector<T*>::const_iterator it_t;
57  for(it_t it = a_from.begin();it!=a_from.end();++it) {
58  a_to.push_back((*it)->copy());
59  }
60 }
61 
62 template <class T>
63 inline void vcopy(std::vector<T*>& a_to,const std::vector<T*>& a_from) {return copy<T>(a_to,a_from);}
64 
65 template <class T>
66 inline void append(std::vector<T>& a_vec,const std::vector<T>& a_from) {
67  typedef typename std::vector<T>::size_type sz_t;
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,typename std::vector<T>::size_type a_num,const T* a_from) {
78  typedef typename std::vector<T>::size_type sz_t;
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,const T* a_elem) {
89  typedef typename std::vector<T*>::iterator it_t;
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_vec,const T* a_item){
101  typedef typename std::vector<T*>::const_iterator it_t;
102  for(it_t it=a_vec.begin();it!=a_vec.end();++it) {
103  if(*it==a_item) return true;
104  }
105  return false;
106 }
107 
111 template <class T>
112 inline void push_back_unique(std::vector<T>& a_vec,const T& a_v) {
113  typedef typename std::vector<T>::const_iterator it_t;
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);
116 }
117 
118 template <class T>
119 inline bool remove(std::vector<T>& a_vals,const T& a_elem){
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_t;
133  for(it_t it=a_vals.begin();it!=a_vals.end();++it) {
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,bool(*a_filter_func)(const T& a_elem)){
146  std::vector<T> vs;
147  typedef typename std::vector<T>::iterator it_t;
148  for(it_t it=a_vals.begin();it!=a_vals.end();++it) {
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_t;
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 does not compile.
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,const T& a_item){
172  typedef typename std::vector<T>::const_iterator it_t;
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_vec,const T& a_item,unsigned int& a_index){
182  a_index = 0;
183  typedef typename std::vector<T>::const_iterator it_t;
184  it_t it;
185  for(it=a_vec.begin();it!=a_vec.end();++it,a_index++) {
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,const T& a_item){
194  typedef typename std::vector<T>::const_iterator it_t;
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_vec,T& a_value) {
204  if(a_vec.empty()) {a_value = T();return false;}
205  a_value = a_vec[0];
206  typedef typename std::vector<T>::const_iterator it_t;
207  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_vec,T& a_value) {
215  if(a_vec.empty()) {a_value = T();return false;}
216  a_value = a_vec[0];
217  typedef typename std::vector<T>::const_iterator it_t;
218  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_iterator it_t;
228  for(it_t it = a_vec.begin();it!=a_vec.end();++it) sum += *it;
229  return sum;
230 }
231 
232 template <class T>
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;
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,unsigned int a_number){
245  a_vec.resize(a_number);
246  for(unsigned int index=0;index<a_number;index++) a_vec[index] = T(index);
247 }
248 
249 template <class T>
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;
252  typedef typename std::vector<T>::iterator it_t;
253  typedef typename std::vector<T>::const_iterator cit_t;
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,const std::vector<T>& a_v){
262  if(a_vec.size()!=a_v.size()) return false;
263  typedef typename std::vector<T>::const_iterator it_t;
264  typedef typename std::vector<T>::const_iterator cit_t;
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,(*it),(*vit));
273  return false;
274  //}
275  }
276  return true;
277 }
278 
279 template <class T>
280 inline bool vadd(std::vector<T>& a_vec,const std::vector<T>& a_v) {return add<T>(a_vec,a_v);}
281 
282 template <class T>
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;
285  typedef typename std::vector<T>::iterator it_t;
286  typedef typename std::vector<T>::const_iterator cit_t;
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 std::vector<T>& a_v){
295  if(a_vec.size()!=a_v.size()) return false;
296  typedef typename std::vector<T>::iterator it_t;
297  typedef typename std::vector<T>::const_iterator cit_t;
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& a_v){
313  typedef typename std::vector<T>::iterator it_t;
314  for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it += a_v;
315 }
316 
317 template <class T>
318 inline void sub(std::vector<T>& a_vec,const T& a_v){
319  typedef typename std::vector<T>::iterator it_t;
320  for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it -= a_v;
321 }
322 
323 template <class T>
324 inline void mul(std::vector<T>& a_vec,const T& a_v){
325  typedef typename std::vector<T>::iterator it_t;
326  for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it *= a_v;
327 }
328 template <class T>
329 inline bool vmul(std::vector<T>& a_vec,const std::vector<T>& a_v) {return vmul<T>(a_vec,a_v);}
330 
331 template <class T>
332 inline void div(std::vector<T>& a_vec,const T& a_v){
333  typedef typename std::vector<T>::iterator it_t;
334  for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it /= a_v;
335 }
336 
337 /*
338 template <class FROM,class TO>
339 inline std::vector<TO> convert(const std::vector<FROM>& a_from){
340  typedef typename std::vector<FROM>::const_iterator const_it_t;
341  typedef typename std::vector<TO>::iterator it_t;
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 = (TO)*ait;}
346  return to;
347 }
348 */
349 template <class FROM,class TO>
350 inline void convert(const std::vector<FROM>& a_from,std::vector<TO>& a_to) {
351  typedef typename std::vector<FROM>::const_iterator const_it_t;
352  typedef typename std::vector<TO>::iterator it_t;
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 = (TO)*ait;}
357 }
358 
359 template <class T>
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();return false;}
362  a_min = *(a_vec.begin());
363  a_max = a_min;
364  typedef typename std::vector<T>::const_iterator it_t;
365  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
366  a_min = *it<a_min?*it:a_min;
367  a_max = *it>a_max?*it:a_max;
368  }
369  return true;
370 }
371 
375 template <class T>
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();return false;}
378  T S = T();
379  T S2 = T();
380  typedef typename std::vector<T>::const_iterator it_t;
381  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_mean * a_mean));
387  return true;
388 }
389 
390 template <class T>
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(*a_fabs)(T)) {
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());
395  a_max = a_min;
396  T S = T();
397  T S2 = T();
398  typedef typename std::vector<T>::const_iterator it_t;
399  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_mean * a_mean));
407  return true;
408 }
409 
413 template <class T>
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();return false;}
416  T S = T();
417  T S2 = T();
418  typedef typename std::vector<T>::const_iterator it_t;
419  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_mean * a_mean));
425  return true;
426 }
427 
428 template <class T>
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 T&),T(*a_fabs)(const T&)) {
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());
433  a_max = a_min;
434  T S = T();
435  T S2 = T();
436  typedef typename std::vector<T>::const_iterator it_t;
437  for(it_t it = a_vec.begin();it!=a_vec.end();++it) {
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_mean * a_mean));
445  return true;
446 }
447 
448 }
449 
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,std::ostream& a_out){
460  typedef typename std::vector<T>::const_iterator it_t;
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
tools::div
bool div(std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:294
tools::remove
bool remove(std::vector< T > &a_vals, const T &a_elem)
Definition: vmanip:119
tools::sub
bool sub(std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:283
tools::item_index
bool item_index(const std::vector< T > &a_vec, const T &a_item, unsigned int &a_index)
Definition: vmanip:181
tools::add
bool add(std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:250
tools::vadd
bool vadd(std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:280
tools::convert
void convert(const std::vector< FROM > &a_from, std::vector< TO > &a_to)
Definition: vmanip:350
tools::removep
void removep(std::vector< T * > &a_vec, const T *a_elem)
Definition: vmanip:88
tools::vfilter
void vfilter(std::vector< T > &a_vals, bool(*a_filter_func)(const T &a_elem))
Definition: vmanip:145
tools::mx
T mx(const T &a, const T &b)
Definition: mnmx:13
tools::raw_clear
void raw_clear(std::map< K, V * > &a_m)
Definition: mapmanip:56
tools::mean_rms
bool mean_rms(const std::vector< T > &a_vec, T &a_mean, T &a_rms, T(*a_sqrt)(T), T(*a_fabs)(T))
T(*a_fabs)(T) : /////////////////////////////////////////////////////////////////////////////////////...
Definition: vmanip:376
tools::dump
void dump(std::ostream &a_out, const tools::array< T > &a_array, const std::string &a_title)
Definition: array:519
tools::copy
bool copy(T *&a_v, I a_n, const T *a_from)
Definition: carray:30
tools::unique
void unique(std::vector< T > &a_vec)
Definition: vmanip:155
tools::safe_reverse_clear
void safe_reverse_clear(std::vector< T * > &a_vec)
Definition: vmanip:29
tools::safe_clear
void safe_clear(std::map< K, V * > &a_m)
Definition: mapmanip:12
tools::vcopy
void vcopy(std::vector< T * > &a_to, const std::vector< T * > &a_from)
Definition: vmanip:63
tools::append
void append(std::vector< T > &a_vec, const std::vector< T > &a_from)
Definition: vmanip:66
tools::push_back_unique
void push_back_unique(std::vector< T > &a_vec, const T &a_v)
T : ////////////////////////////////////////////////////////////////////.
Definition: vmanip:112
tools::is_inp
bool is_inp(const std::vector< T * > &a_vec, const T *a_item)
Definition: vmanip:100
tools::steps
void steps(std::vector< T > &a_vec, unsigned int a_number)
Definition: vmanip:244
tools::mul
void mul(std::vector< T > &a_vec, const T &a_v)
Definition: vmanip:324
tools::minimum
bool minimum(const std::vector< T > &a_vec, T &a_value)
Definition: vmanip:203
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::min_max_mean_rms
bool min_max_mean_rms(I a_n, const void *a_v, TO &a_mn, TO &a_mx, TO &a_mean, TO &a_rms, TO(*a_sqrt)(TO), TO(*a_fabs)(TO))
Definition: carray:102
tools::vequ
bool vequ(const std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:261
tools::vmul
bool vmul(std::vector< T > &a_vec, const std::vector< T > &a_v)
Definition: vmanip:329
tools::maximum
bool maximum(const std::vector< T > &a_vec, T &a_value)
Definition: vmanip:214
tools::sum
T sum(const std::vector< T > &a_vec)
Definition: vmanip:225
tools::is_in
bool is_in(const std::vector< T > &a_vec, const T &a_item)
Definition: vmanip:171
tools::filter
void filter(std::vector< std::string > &a_v, const std::string &a_pattern, bool a_check_for_wilds=true)
Definition: smatch:91
tools::min_max
bool min_max(I a_n, const void *a_v, TO &a_mn, TO &a_mx)
Definition: carray:72
tools::belong
bool belong(const std::string &a_string, char a_c)
Definition: schar:11