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10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 
 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 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 
108 ////////////////////////////////////////////////////////////////////////////
109 /// T : ////////////////////////////////////////////////////////////////////
110 ////////////////////////////////////////////////////////////////////////////
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 
372 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
373 /// T(*a_fabs)(T) : ///////////////////////////////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 
410 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
411 /// T(*a_fabs)(const T&) : ///////////////////////////////////////////////////////////////////////////////////////////
412 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 
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,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