inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_vmanip 00005 #define inlib_vmanip 00006 00007 #include <vector> 00008 00009 namespace inlib { 00010 00014 00015 template <class T> 00016 inline void clear(std::vector<T*>& a_vec){ 00017 // the below takes into account the case in 00018 // which "delete entry" could modify a_vec. 00019 typedef typename std::vector<T*>::iterator it_t; 00020 while(!a_vec.empty()) { 00021 it_t it = a_vec.begin(); 00022 T* entry = *it; 00023 a_vec.erase(it); 00024 delete entry; 00025 } 00026 } 00027 00028 template <class T> 00029 inline void append(std::vector<T>& a_vec,const std::vector<T>& a_from) { 00030 typedef typename std::vector<T>::size_type sz_t; 00031 sz_t vsize = a_vec.size(); 00032 sz_t number = a_from.size(); 00033 a_vec.resize(vsize+number); 00034 sz_t offset = vsize; 00035 for(sz_t index=0;index<number;index++,offset++) { 00036 a_vec[offset] = a_from[index]; 00037 } 00038 } 00039 00040 template <class T> 00041 inline void unique(std::vector<T>& a_vec) { 00042 typedef typename std::vector<T>::iterator it_t; 00043 it_t it,it2; //Android : it2=it+1 does not compile with inlib/stl. 00044 for(it=a_vec.begin();it!=a_vec.end();++it) { 00045 it2 = it;it2++; //Android : it2=it+1 does not compile. 00046 for(;it2!=a_vec.end();) { 00047 if(*it2==*it) { 00048 it2 = a_vec.erase(it2); 00049 } else { 00050 ++it2; 00051 } 00052 } 00053 } 00054 } 00055 00056 template <class T> 00057 inline bool item_index(const std::vector<T>& a_vec,const T& a_item,unsigned int& a_index){ 00058 a_index = 0; 00059 typedef typename std::vector<T>::const_iterator it_t; 00060 it_t it; 00061 for(it=a_vec.begin();it!=a_vec.end();++it,a_index++) { 00062 if(*it==a_item) return true; 00063 } 00064 a_index = 0; 00065 return false; 00066 } 00067 00068 template <class T> 00069 inline bool minimum(const std::vector<T>& a_vec,T& a_value) { 00070 if(a_vec.empty()) {a_value = T();return false;} 00071 a_value = a_vec[0]; 00072 typedef typename std::vector<T>::const_iterator it_t; 00073 for(it_t it = a_vec.begin();it!=a_vec.end();++it) { 00074 a_value = (a_value<(*it)?a_value:(*it)); 00075 } 00076 return true; 00077 } 00078 00079 template <class T> 00080 inline bool maximum(const std::vector<T>& a_vec,T& a_value) { 00081 if(a_vec.empty()) {a_value = T();return false;} 00082 a_value = a_vec[0]; 00083 typedef typename std::vector<T>::const_iterator it_t; 00084 for(it_t it = a_vec.begin();it!=a_vec.end();++it) { 00085 a_value = (a_value>(*it)?a_value:(*it)); 00086 } 00087 return true; 00088 } 00089 00090 template <class T> 00091 inline T sum(const std::vector<T>& a_vec) { 00092 T sum = T(); 00093 typedef typename std::vector<T>::const_iterator it_t; 00094 for(it_t it = a_vec.begin();it!=a_vec.end();++it) sum += *it; 00095 return sum; 00096 } 00097 00098 template <class T> 00099 inline void filter(std::vector<T>& a_vec, 00100 unsigned int a_mn,unsigned int a_mx){ 00101 unsigned int imx = a_vec.size()-1; 00102 unsigned int mx = a_mx<imx?a_mx:imx; 00103 unsigned int i = 0; 00104 for(unsigned int index=a_mn;index<=mx;index++) { 00105 a_vec[i] = a_vec[index];i++; 00106 } 00107 a_vec.resize(i); 00108 } 00109 00110 template <class T> 00111 inline void steps(std::vector<T>& a_vec,unsigned int a_number){ 00112 a_vec.resize(a_number); 00113 for(unsigned int index=0;index<a_number;index++) a_vec[index] = T(index); 00114 } 00115 00116 template <class T> 00117 inline bool add(std::vector<T>& a_vec,const std::vector<T>& a_v){ 00118 if(a_vec.size()!=a_v.size()) return false; 00119 typedef typename std::vector<T>::iterator it_t; 00120 typedef typename std::vector<T>::const_iterator cit_t; 00121 it_t it = a_vec.begin(); 00122 cit_t vit = a_v.begin(); 00123 for(;it!=a_vec.end();++it,++vit) *it += *vit; 00124 return true; 00125 } 00126 00127 template <class T> 00128 inline bool sub(std::vector<T>& a_vec,const std::vector<T>& a_v){ 00129 if(a_vec.size()!=a_v.size()) return false; 00130 typedef typename std::vector<T>::iterator it_t; 00131 typedef typename std::vector<T>::const_iterator cit_t; 00132 it_t it = a_vec.begin(); 00133 cit_t vit = a_v.begin(); 00134 for(;it!=a_vec.end();++it,++vit) *it -= *vit; 00135 return true; 00136 } 00137 00138 template <class T> 00139 inline bool div(std::vector<T>& a_vec,const std::vector<T>& a_v){ 00140 if(a_vec.size()!=a_v.size()) return false; 00141 typedef typename std::vector<T>::iterator it_t; 00142 typedef typename std::vector<T>::const_iterator cit_t; 00143 it_t it = a_vec.begin(); 00144 cit_t vit = a_v.begin(); 00145 bool errors = false; 00146 for(;it!=a_vec.end();++it,++vit) { 00147 if(*vit==T()) { 00148 errors = true; 00149 } else { 00150 *it /= *vit; 00151 } 00152 } 00153 return errors; 00154 } 00155 00156 template <class T> 00157 inline void add(std::vector<T>& a_vec,const T& a_v){ 00158 typedef typename std::vector<T>::iterator it_t; 00159 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it += a_v; 00160 } 00161 00162 template <class T> 00163 inline void sub(std::vector<T>& a_vec,const T& a_v){ 00164 typedef typename std::vector<T>::iterator it_t; 00165 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it -= a_v; 00166 } 00167 00168 template <class T> 00169 inline void mul(std::vector<T>& a_vec,const T& a_v){ 00170 typedef typename std::vector<T>::iterator it_t; 00171 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it *= a_v; 00172 } 00173 00174 template <class T> 00175 inline void div(std::vector<T>& a_vec,const T& a_v){ 00176 typedef typename std::vector<T>::iterator it_t; 00177 for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it /= a_v; 00178 } 00179 00180 template <class FROM,class TO> 00181 inline std::vector<TO> convert(const std::vector<FROM>& a_from){ 00182 typedef typename std::vector<FROM>::const_iterator const_it_t; 00183 typedef typename std::vector<TO>::iterator it_t; 00184 std::vector<TO> to(a_from.size()); 00185 const_it_t ait = a_from.begin(); 00186 it_t toit = to.begin(); 00187 for(;ait!=a_from.end();++ait,++toit) {*toit = (TO)*ait;} 00188 return to; 00189 } 00190 00191 } 00192 00196 #include <ostream> 00197 00198 namespace inlib { 00199 00200 //NOTE : print is a Python keyword. 00201 template <class T> 00202 inline void dump(const std::vector<T>& a_vec,std::ostream& a_out){ 00203 typedef typename std::vector<T>::const_iterator it_t; 00204 it_t it; 00205 for(it=a_vec.begin();it!=a_vec.end();++it) { 00206 a_out << *it << std::endl; 00207 } 00208 } 00209 00210 } 00211 00212 #include <cmath> 00213 00214 namespace inlib { 00215 00216 template <class T> 00217 inline bool mean_rms(const std::vector<T>& a_vec,T& a_mean,T& a_rms) { 00218 if(a_vec.empty()) {a_mean=T();a_rms=T();return false;} 00219 T S = T(); 00220 T S2 = T(); 00221 typedef typename std::vector<T>::const_iterator it_t; 00222 for(it_t it = a_vec.begin();it!=a_vec.end();++it) { 00223 S += *it; 00224 S2 += (*it) * (*it); 00225 } 00226 a_mean = S/T(a_vec.size()); 00227 //NOTE : should use a templated sqrt and fabs. 00228 a_rms = ::sqrt(::fabs(S2/T(a_vec.size()) - a_mean * a_mean)); 00229 return true; 00230 } 00231 00232 } 00233 00234 #endif