// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_vmanip #define tools_vmanip #include namespace tools { ////////////////////////////////////////////////////////// /// manipulations that induces no intermediate vector : // ////////////////////////////////////////////////////////// template inline void clear(std::vector& a_vec){ // the below takes into account the case in // which "delete entry" could modify a_vec. typedef typename std::vector::iterator it_t; while(!a_vec.empty()) { it_t it = a_vec.begin(); T* entry = *it; a_vec.erase(it); delete entry; } } template inline void raw_clear(std::vector& a_vec){ typedef typename std::vector::iterator it_t; for(it_t it = a_vec.begin();it!=a_vec.end();++it) delete *it; a_vec.clear(); } template inline void copy(std::vector& a_to,const std::vector& a_from){ raw_clear(a_to); typedef typename std::vector::const_iterator it_t; for(it_t it = a_from.begin();it!=a_from.end();++it) { a_to.push_back((*it)->copy()); } } template inline void append(std::vector& a_vec,const std::vector& a_from) { typedef typename std::vector::size_type sz_t; sz_t vsize = a_vec.size(); sz_t number = a_from.size(); a_vec.resize(vsize+number); sz_t offset = vsize; for(sz_t index=0;index inline void removep(std::vector& a_vec,T* a_elem) { typedef typename std::vector::iterator it_t; it_t it; for(it=a_vec.begin();it!=a_vec.end();) { if(*it==a_elem) { it = a_vec.erase(it); } else { ++it; } } } template inline void push_back_unique(std::vector& a_vec,const T& a_v) { typedef typename std::vector::const_iterator it_t; for(it_t it=a_vec.begin();it!=a_vec.end();++it) {if(*it==a_v) return;} a_vec.push_back(a_v); } template inline bool remove(std::vector& a_vals,const T& a_elem){ bool found_some = false; //std::vector::iterator it; //for(it=a_vals.begin();it!=a_vals.end();) { // if(*it==a_elem) { // it = a_vals.erase(it); // found_some = true; // } else { // ++it; // } //} //TOOLS_STL : brut force avoiding erase() : std::vector vs; typedef typename std::vector::iterator it_t; for(it_t it=a_vals.begin();it!=a_vals.end();++it) { if(*it==a_elem) { found_some = true; } else { vs.push_back(*it); } } a_vals = vs; return found_some; } template inline void unique(std::vector& a_vec) { typedef typename std::vector::iterator it_t; it_t it,it2; for(it=a_vec.begin();it!=a_vec.end();++it) { it2 = it;it2++; //TOOLS_STL : it2=it+1 does not compile. for(;it2!=a_vec.end();) { if(*it2==*it) { it2 = a_vec.erase(it2); } else { ++it2; } } } } template inline bool item_index(const std::vector& a_vec,const T& a_item,unsigned int& a_index){ a_index = 0; typedef typename std::vector::const_iterator it_t; it_t it; for(it=a_vec.begin();it!=a_vec.end();++it,a_index++) { if(*it==a_item) return true; } a_index = 0; return false; } template inline bool minimum(const std::vector& a_vec,T& a_value) { if(a_vec.empty()) {a_value = T();return false;} a_value = a_vec[0]; typedef typename std::vector::const_iterator it_t; for(it_t it = a_vec.begin();it!=a_vec.end();++it) { a_value = (a_value<(*it)?a_value:(*it)); } return true; } template inline bool maximum(const std::vector& a_vec,T& a_value) { if(a_vec.empty()) {a_value = T();return false;} a_value = a_vec[0]; typedef typename std::vector::const_iterator it_t; for(it_t it = a_vec.begin();it!=a_vec.end();++it) { a_value = (a_value>(*it)?a_value:(*it)); } return true; } template inline T sum(const std::vector& a_vec) { T sum = T(); typedef typename std::vector::const_iterator it_t; for(it_t it = a_vec.begin();it!=a_vec.end();++it) sum += *it; return sum; } template inline void filter(std::vector& a_vec, unsigned int a_mn,unsigned int a_mx){ unsigned int imx = a_vec.size()-1; unsigned int mx = a_mx inline void steps(std::vector& a_vec,unsigned int a_number){ a_vec.resize(a_number); for(unsigned int index=0;index inline bool add(std::vector& a_vec,const std::vector& a_v){ if(a_vec.size()!=a_v.size()) return false; typedef typename std::vector::iterator it_t; typedef typename std::vector::const_iterator cit_t; it_t it = a_vec.begin(); cit_t vit = a_v.begin(); for(;it!=a_vec.end();++it,++vit) *it += *vit; return true; } template inline bool sub(std::vector& a_vec,const std::vector& a_v){ if(a_vec.size()!=a_v.size()) return false; typedef typename std::vector::iterator it_t; typedef typename std::vector::const_iterator cit_t; it_t it = a_vec.begin(); cit_t vit = a_v.begin(); for(;it!=a_vec.end();++it,++vit) *it -= *vit; return true; } template inline bool div(std::vector& a_vec,const std::vector& a_v){ if(a_vec.size()!=a_v.size()) return false; typedef typename std::vector::iterator it_t; typedef typename std::vector::const_iterator cit_t; it_t it = a_vec.begin(); cit_t vit = a_v.begin(); bool errors = false; for(;it!=a_vec.end();++it,++vit) { if(*vit==T()) { errors = true; } else { *it /= *vit; } } return errors; } template inline void add(std::vector& a_vec,const T& a_v){ typedef typename std::vector::iterator it_t; for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it += a_v; } template inline void sub(std::vector& a_vec,const T& a_v){ typedef typename std::vector::iterator it_t; for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it -= a_v; } template inline void mul(std::vector& a_vec,const T& a_v){ typedef typename std::vector::iterator it_t; for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it *= a_v; } template inline void div(std::vector& a_vec,const T& a_v){ typedef typename std::vector::iterator it_t; for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it /= a_v; } template inline std::vector convert(const std::vector& a_from){ typedef typename std::vector::const_iterator const_it_t; typedef typename std::vector::iterator it_t; std::vector to(a_from.size()); const_it_t ait = a_from.begin(); it_t toit = to.begin(); for(;ait!=a_from.end();++ait,++toit) {*toit = (TO)*ait;} return to; } } /////////////////////////////////////////////////////////// /// manipulations that induces other includes : /////////// /////////////////////////////////////////////////////////// #include namespace tools { //NOTE : print is a Python keyword. template inline void dump(const std::vector& a_vec,std::ostream& a_out){ typedef typename std::vector::const_iterator it_t; it_t it; for(it=a_vec.begin();it!=a_vec.end();++it) { a_out << *it << std::endl; } } } #include namespace tools { template inline bool mean_rms(const std::vector& a_vec,T& a_mean,T& a_rms) { if(a_vec.empty()) {a_mean=T();a_rms=T();return false;} T S = T(); T S2 = T(); typedef typename std::vector::const_iterator it_t; for(it_t it = a_vec.begin();it!=a_vec.end();++it) { S += *it; S2 += (*it) * (*it); } a_mean = S/T(a_vec.size()); //NOTE : should use a templated sqrt and fabs. a_rms = ::sqrt(::fabs(S2/T(a_vec.size()) - a_mean * a_mean)); return true; } } #endif