00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef TJVECTOR_H
00020 #define TJVECTOR_H
00021
00022 #include <tjutils/tjutils.h>
00023 #include <tjutils/tjcomplex.h>
00024 #include <tjutils/tjtools.h>
00025 #include <tjutils/tjstring.h>
00026 #include <tjutils/tjlog.h>
00027
00035 class VectorComp {
00036 public:
00037 static const char* get_compName();
00038 };
00039
00040
00041
00042
00050 template<class T> class tjvector : public STD_vector<T> {
00051
00052 public:
00053
00059 tjvector(unsigned int n=0);
00060
00061
00068 tjvector(const T *array, unsigned int n);
00069
00070
00075 tjvector(const STD_vector<T>& v);
00076
00081 tjvector(const tjvector<T>& tv);
00082
00087 virtual ~tjvector();
00088
00089
00094 tjvector<T>& operator = (const T& value);
00095
00100 tjvector<T>& operator = (const STD_vector<T>& vec);
00101
00106 tjvector<T>& operator = (const tjvector<T>& tv);
00107
00108
00113 tjvector<T> operator + (const STD_vector<T>& w) const {
00114 tjvector<T> result(*this);
00115 for(unsigned int i=0; i<length(); i++) result[i]+=w[i];
00116 return result;
00117 }
00118
00123 tjvector<T> operator - (const STD_vector<T>& w) const {
00124 tjvector<T> result(*this);
00125 for(unsigned int i=0; i<length(); i++) result[i]-=w[i];
00126 return result;
00127 }
00128
00133 tjvector<T> operator * (const STD_vector<T>& w) const {
00134 tjvector<T> result(*this);
00135 for(unsigned int i=0; i<length(); i++) result[i]*=w[i];
00136 return result;
00137 }
00138
00143 tjvector<T> operator / (const STD_vector<T>& w) const {
00144 tjvector<T> result(*this);
00145 for(unsigned int i=0; i<length(); i++) result[i]/=w[i];
00146 return result;
00147 }
00148
00153 tjvector<T> operator - () const {
00154 tjvector<T> result(*this);
00155 for(unsigned int i=0; i<length(); i++) result[i]=-result[i];
00156 return result;
00157 }
00158
00163 friend tjvector<T> operator + (const T& s, const STD_vector<T>& v) {
00164 tjvector<T> result(v);
00165 for(unsigned int i=0; i<v.size(); i++) result[i]+=s;
00166 return result;
00167 }
00168
00173 tjvector<T> operator + (const T& s) const {
00174 return s+(*this);
00175 }
00176
00181 friend tjvector<T> operator - (const T& s, const STD_vector<T>& v) {
00182 tjvector<T> result(v);
00183 for(unsigned int i=0; i<v.size(); i++) result[i]=s-result[i];
00184 return result;
00185 }
00186
00191 tjvector<T> operator - (const T& s) const {
00192 return -s+(*this);
00193 }
00194
00195
00200 friend tjvector<T> operator * (const T& s, const STD_vector<T>& v) {
00201 tjvector<T> result(v);
00202 for(unsigned int i=0; i<v.size(); i++) result[i]=result[i]*s;
00203 return result;
00204 }
00205
00210 tjvector<T> operator * (const T& s) const {
00211 return s*(*this);
00212 }
00213
00214
00219 friend tjvector<T> operator / (const T& s, const STD_vector<T>& v) {
00220 tjvector<T> result(v);
00221 for(unsigned int i=0; i<v.size(); i++) result[i]=s/result[i];
00222 return result;
00223 }
00224
00229 tjvector<T> operator / ( const T& s) const {
00230 return (T(1)/s)*(*this);
00231 }
00232
00233
00237 tjvector<T>& operator += ( const STD_vector<T>& v) {
00238 (*this)=(*this)+v;
00239 return (*this);
00240 }
00241
00245 tjvector<T>& operator += ( const T& s) {
00246 (*this)=(*this)+s;
00247 return (*this);
00248 }
00249
00253 tjvector<T>& operator -= ( const STD_vector<T>& v) {
00254 (*this)=(*this)-v;
00255 return (*this);
00256 }
00257
00261 tjvector<T>& operator -= ( const T& s) {
00262 (*this)=(*this)-s;
00263 return (*this);
00264 }
00265
00266
00270 tjvector<T>& operator *= ( const STD_vector<T>& v) {
00271 (*this)=(*this)*v;
00272 return (*this);
00273 }
00274
00278 tjvector<T>& operator *= ( const T& s) {
00279 (*this)=(*this)*s;
00280 return (*this);
00281 }
00282
00283
00287 tjvector<T>& operator /= ( const STD_vector<T>& v) {
00288 (*this)=(*this)/v;
00289 return (*this);
00290 }
00291
00295 tjvector<T>& operator /= ( const T& s) {
00296 (*this)=(*this)/s;
00297 return (*this);
00298 }
00299
00300
00309 virtual tjvector<T>& resize(unsigned int newsize);
00310
00311
00316 unsigned int length () const;
00317
00318
00323 unsigned int fill_linear(const T& min,const T& max);
00324
00325
00330 T sum() const;
00331
00332
00337 tjvector<T> range(unsigned int startindex, unsigned int endindex) const;
00338
00339
00344 T maxvalue() const;
00345
00346
00351 T minvalue() const;
00352
00353
00358 T maxabs() const;
00359
00360
00367 const T* c_array() const;
00368
00376 tjvector& set_c_array(const unsigned char* array, unsigned int n);
00377
00378
00384 T normalize();
00385
00386
00392 tjvector& interpolate(unsigned int newsize, float subpixel_shift=0.0);
00393
00394
00401 int load(const STD_string& fname);
00402
00410 int write(const STD_string& fname, fopenMode mode=overwriteMode, LONGEST_INT nelements=-1) const;
00411
00412
00417 STD_string printbody() const;
00418
00423 friend STD_ostream& operator << (STD_ostream& s,const tjvector<T>& v) {
00424 return s << "(" << v.size() << ")=" << v.printbody();
00425 }
00426
00427
00428 protected:
00429
00430
00431 static T* allocate_memory(unsigned int nelements) {return new T[nelements];}
00432
00433
00434 private:
00435 mutable T* c_array_cache;
00436
00437 };
00438
00439
00441
00442
00446 typedef tjvector<float> fvector;
00447
00451 typedef tjvector<double> dvector;
00452
00456 typedef tjvector<int> ivector;
00457
00458
00462 typedef tjvector<STD_complex> cvector;
00463
00464
00468 struct svector : public STD_vector<STD_string> {
00469
00470
00471 svector() {}
00472 svector(const STD_vector<STD_string>& sv) {svector::operator = (sv);}
00473 svector& operator = (const STD_vector<STD_string>& sv) {STD_vector<STD_string>::operator = (sv); return *this;}
00474
00475
00476 unsigned char* c_array() const {return 0;}
00477 svector& set_c_array(const unsigned char*, unsigned int) {return *this;}
00478 static unsigned char* allocate_memory(unsigned int) {return 0;}
00479
00480
00481 STD_string printbody() const;
00482 };
00483
00485
00486
00487 STD_complex* interpolate1D(const STD_complex* data,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
00488 float* interpolate1D(const float* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
00489 double* interpolate1D(const double* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
00490 int* interpolate1D(const int* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
00491
00493
00494
00499 template<class T>
00500 STD_vector<T> list2vector(const STD_list<T>& src) {
00501 STD_vector<T> result; result.resize(src.size());
00502 unsigned int i=0;
00503 for(typename STD_list<T>::const_iterator it=src.begin(); it!=src.end(); ++it) {
00504 result[i]=(*it);
00505 i++;
00506 }
00507 return result;
00508 }
00509
00514 fvector real(const cvector& cv);
00515
00520 fvector imag(const cvector& cv);
00521
00526 fvector amplitude(const cvector& cv);
00527
00532 fvector phase(const cvector& cv);
00533
00538 cvector real2complex(const fvector& fv);
00539
00544 dvector fvector2dvector(const fvector& fv);
00545
00550 fvector dvector2fvector(const dvector& dv);
00551
00552
00559 svector tokens(const STD_string& tokenstring, char custom_separator=0, char escape_begin='"', char escape_end='"');
00560
00561
00567 STD_string tokenstring(const svector& tokens,unsigned int linewidth=_DEFAULT_LINEWIDTH_);
00568
00569
00577 svector browse_dir(const STD_string& dirname,bool only_dirs=false, bool discard_dotfiles=false);
00578
00579
00580
00583 #endif