ODIN
tjvector.h
1  /***************************************************************************
2  tjvector.h - description
3  -------------------
4  begin : Thu Feb 22 2001
5  copyright : (C) 2000-2021 by Thies Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #ifndef TJVECTOR_H
20 #define TJVECTOR_H
21 
22 #include <tjutils/tjutils.h>
23 #include <tjutils/tjcomplex.h>
24 #include <tjutils/tjtools.h>
25 #include <tjutils/tjstring.h>
26 #include <tjutils/tjlog.h>
27 
35 class VectorComp {
36  public:
37  static const char* get_compName();
38 };
39 
40 
41 
42 
50 template<class T> class tjvector : public STD_vector<T> {
51 
52  public:
53 
59  tjvector(unsigned int n=0);
60 
61 
68  tjvector(const T *array, unsigned int n);
69 
70 
75  tjvector(const STD_vector<T>& v);
76 
81  tjvector(const tjvector<T>& tv);
82 
87  virtual ~tjvector();
88 
89 
94  tjvector<T>& operator = (const T& value);
95 
100  tjvector<T>& operator = (const STD_vector<T>& vec);
101 
107 
108 
113  tjvector<T> operator + (const STD_vector<T>& w) const {
114  tjvector<T> result(*this);
115  for(unsigned int i=0; i<length(); i++) result[i]+=w[i];
116  return result;
117  }
118 
123  tjvector<T> operator - (const STD_vector<T>& w) const {
124  tjvector<T> result(*this);
125  for(unsigned int i=0; i<length(); i++) result[i]-=w[i];
126  return result;
127  }
128 
133  tjvector<T> operator * (const STD_vector<T>& w) const {
134  tjvector<T> result(*this);
135  for(unsigned int i=0; i<length(); i++) result[i]*=w[i];
136  return result;
137  }
138 
143  tjvector<T> operator / (const STD_vector<T>& w) const {
144  tjvector<T> result(*this);
145  for(unsigned int i=0; i<length(); i++) result[i]/=w[i];
146  return result;
147  }
148 
154  tjvector<T> result(*this);
155  for(unsigned int i=0; i<length(); i++) result[i]=-result[i];
156  return result;
157  }
158 
163  friend tjvector<T> operator + (const T& s, const STD_vector<T>& v) {
164  tjvector<T> result(v);
165  for(unsigned int i=0; i<v.size(); i++) result[i]+=s;
166  return result;
167  }
168 
173  tjvector<T> operator + (const T& s) const {
174  return s+(*this);
175  }
176 
181  friend tjvector<T> operator - (const T& s, const STD_vector<T>& v) {
182  tjvector<T> result(v);
183  for(unsigned int i=0; i<v.size(); i++) result[i]=s-result[i];
184  return result;
185  }
186 
191  tjvector<T> operator - (const T& s) const {
192  return -s+(*this);
193  }
194 
195 
200  friend tjvector<T> operator * (const T& s, const STD_vector<T>& v) {
201  tjvector<T> result(v);
202  for(unsigned int i=0; i<v.size(); i++) result[i]=result[i]*s;
203  return result;
204  }
205 
210  tjvector<T> operator * (const T& s) const {
211  return s*(*this);
212  }
213 
214 
219  friend tjvector<T> operator / (const T& s, const STD_vector<T>& v) {
220  tjvector<T> result(v);
221  for(unsigned int i=0; i<v.size(); i++) result[i]=s/result[i];
222  return result;
223  }
224 
229  tjvector<T> operator / ( const T& s) const {
230  return (T(1)/s)*(*this);
231  }
232 
233 
237  tjvector<T>& operator += ( const STD_vector<T>& v) {
238  (*this)=(*this)+v;
239  return (*this);
240  }
241 
245  tjvector<T>& operator += ( const T& s) {
246  (*this)=(*this)+s;
247  return (*this);
248  }
249 
253  tjvector<T>& operator -= ( const STD_vector<T>& v) {
254  (*this)=(*this)-v;
255  return (*this);
256  }
257 
261  tjvector<T>& operator -= ( const T& s) {
262  (*this)=(*this)-s;
263  return (*this);
264  }
265 
266 
270  tjvector<T>& operator *= ( const STD_vector<T>& v) {
271  (*this)=(*this)*v;
272  return (*this);
273  }
274 
278  tjvector<T>& operator *= ( const T& s) {
279  (*this)=(*this)*s;
280  return (*this);
281  }
282 
283 
287  tjvector<T>& operator /= ( const STD_vector<T>& v) {
288  (*this)=(*this)/v;
289  return (*this);
290  }
291 
295  tjvector<T>& operator /= ( const T& s) {
296  (*this)=(*this)/s;
297  return (*this);
298  }
299 
300 
309  virtual tjvector<T>& resize(unsigned int newsize);
310 
311 
316  unsigned int length () const;
317 
318 
323  unsigned int fill_linear(const T& min,const T& max);
324 
325 
330  T sum() const;
331 
332 
337  tjvector<T> range(unsigned int startindex, unsigned int endindex) const;
338 
339 
344  T maxvalue() const;
345 
346 
351  T minvalue() const;
352 
353 
358  T maxabs() const;
359 
360 
367  const T* c_array() const;
368 
376  tjvector& set_c_array(const unsigned char* array, unsigned int n);
377 
378 
385 
386 
392  tjvector& interpolate(unsigned int newsize, float subpixel_shift=0.0);
393 
394 
401  int load(const STD_string& fname);
402 
410  int write(const STD_string& fname, fopenMode mode=overwriteMode, LONGEST_INT nelements=-1) const;
411 
412 
417  STD_string printbody() const;
418 
423  friend STD_ostream& operator << (STD_ostream& s,const tjvector<T>& v) {
424  return s << "(" << v.size() << ")=" << v.printbody();
425  }
426 
427 
428  protected:
429 
430  // allocate aligned memory for type T (to avoid bus errors)
431  static T* allocate_memory(unsigned int nelements) {return new T[nelements];}
432 
433 
434  private:
435  mutable T* c_array_cache;
436 
437 };
438 
439 
441 // Aliases:
442 
447 
452 
457 
458 
463 
464 
468 struct svector : public STD_vector<STD_string> {
469 
470  // some extra stuff to be compatible with STD_vector
471  svector() {}
472  svector(const STD_vector<STD_string>& sv) {svector::operator = (sv);}
473  svector& operator = (const STD_vector<STD_string>& sv) {STD_vector<STD_string>::operator = (sv); return *this;}
474 
475  // interface functions for LDRarray
476  unsigned char* c_array() const {return 0;}
477  svector& set_c_array(const unsigned char*, unsigned int) {return *this;}
478  static unsigned char* allocate_memory(unsigned int) {return 0;}
479 
480  // other useful functions, imitating a tjvector
481  STD_string printbody() const;
482 };
483 
485 
486 // Stuff to re-used in odindata
487 STD_complex* interpolate1D(const STD_complex* data,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
488 float* interpolate1D(const float* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
489 double* interpolate1D(const double* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
490 int* interpolate1D(const int* olddata,unsigned int oldsize,unsigned int newsize, float subpixel_shift);
491 
493 // Specialised helper functions:
494 
499 template<class T>
500 STD_vector<T> list2vector(const STD_list<T>& src) {
501  STD_vector<T> result; result.resize(src.size());
502  unsigned int i=0;
503  for(typename STD_list<T>::const_iterator it=src.begin(); it!=src.end(); ++it) {
504  result[i]=(*it);
505  i++;
506  }
507  return result;
508 }
509 
514  fvector real(const cvector& cv);
515 
520  fvector imag(const cvector& cv);
521 
527 
532  fvector phase(const cvector& cv);
533 
539 
545 
551 
552 
559  svector tokens(const STD_string& tokenstring, char custom_separator=0, char escape_begin='"', char escape_end='"');
560 
561 
567  STD_string tokenstring(const svector& tokens,unsigned int linewidth=_DEFAULT_LINEWIDTH_);
568 
569 
570 #ifndef NO_FILEHANDLING
578  svector browse_dir(const STD_string& dirname,bool only_dirs=false, bool discard_dotfiles=false);
579 #endif
580 
581 
582 
585 #endif
tjvector< T > & operator+=(const STD_vector< T > &v)
Definition: tjvector.h:237
tjvector(unsigned int n=0)
tjvector(const tjvector< T > &tv)
const T * c_array() const
tjvector< T > operator+(const STD_vector< T > &w) const
Definition: tjvector.h:113
T maxabs() const
T minvalue() const
int load(const STD_string &fname)
tjvector< T > & operator-=(const STD_vector< T > &v)
Definition: tjvector.h:253
unsigned int fill_linear(const T &min, const T &max)
int write(const STD_string &fname, fopenMode mode=overwriteMode, LONGEST_INT nelements=-1) const
tjvector< T > & operator=(const T &value)
tjvector(const T *array, unsigned int n)
tjvector< T > operator/(const STD_vector< T > &w) const
Definition: tjvector.h:143
tjvector< T > range(unsigned int startindex, unsigned int endindex) const
T maxvalue() const
tjvector< T > & operator*=(const STD_vector< T > &v)
Definition: tjvector.h:270
friend STD_ostream & operator<<(STD_ostream &s, const tjvector< T > &v)
Definition: tjvector.h:423
tjvector(const STD_vector< T > &v)
STD_string printbody() const
tjvector & interpolate(unsigned int newsize, float subpixel_shift=0.0)
T sum() const
virtual tjvector< T > & resize(unsigned int newsize)
unsigned int length() const
T normalize()
tjvector< T > operator*(const STD_vector< T > &w) const
Definition: tjvector.h:133
tjvector< T > & operator/=(const STD_vector< T > &v)
Definition: tjvector.h:287
tjvector & set_c_array(const unsigned char *array, unsigned int n)
tjvector< T > operator-() const
Definition: tjvector.h:153
virtual ~tjvector()
tjvector< float > fvector
Definition: tjvector.h:446
STD_vector< T > list2vector(const STD_list< T > &src)
Definition: tjvector.h:500
tjvector< double > dvector
Definition: tjvector.h:451
cvector real2complex(const fvector &fv)
svector browse_dir(const STD_string &dirname, bool only_dirs=false, bool discard_dotfiles=false)
fopenMode
Definition: tjtools.h:107
tjvector< int > ivector
Definition: tjvector.h:456
fvector dvector2fvector(const dvector &dv)
fvector phase(const cvector &cv)
dvector fvector2dvector(const fvector &fv)
fvector real(const cvector &cv)
fvector amplitude(const cvector &cv)
STD_string tokenstring(const svector &tokens, unsigned int linewidth=_DEFAULT_LINEWIDTH_)
tjvector< STD_complex > cvector
Definition: tjvector.h:462
svector tokens(const STD_string &tokenstring, char custom_separator=0, char escape_begin='"', char escape_end='"')
fvector imag(const cvector &cv)