ODIN
tjstd.h
1 /***************************************************************************
2  tjstd.h - description
3  -------------------
4  begin : Thu Dec 5 2002
5  copyright : (C) 2000-2021 by Thies H. 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 #ifndef TJSTD_H
19 #define TJSTD_H
20 
22 
23 #ifndef TJUTILS_CONFIG_H
24 #define TJUTILS_CONFIG_H
25 #include <tjutils/config.h>
26 #endif
27 
28 #include <tjutils/tjheap.h> // memory management
29 #include <tjutils/tjcstd.h> // C standard lib
30 
31 
33 // Macros for stream replacement
34 // Must be placed here to avoid circular dependencies
35 
36 #ifdef STREAM_REPLACEMENT
37 
38 #define STD_ostream tjstd_ostream
39 #define STD_istream tjstd_istream
40 #define STD_ostringstream tjstd_ostringstream
41 #define STD_ofstream tjstd_ofstream
42 #define STD_ifstream tjstd_ifstream
43 #define STD_cout tjstd_cout
44 #define STD_cerr tjstd_cerr
45 #define STD_cin tjstd_cin
46 #define STD_endl tjstd_endl
47 #define STD_flush tjstd_flush
48 #define STD_setprecision tjstd_setprecision
49 #define STD_getline tjstd_getline
50 #define STD_make_pair tjstd_make_pair
51 
52 class tjstd_ostream; // forward declaration
53 class tjstd_istream; // forward declaration
54 class tjstd_string; // forward declaration
55 
56 #else
57 
58 #define STD_ostream std::ostream
59 #define STD_istream std::istream
60 #define STD_ostringstream std::ostringstream
61 #define STD_ofstream std::ofstream
62 #define STD_ifstream std::ifstream
63 #define STD_cout std::cout
64 #define STD_cerr std::cerr
65 #define STD_cin std::cin
66 #define STD_endl std::endl
67 #define STD_flush std::flush
68 #define STD_setprecision std::setprecision
69 #define STD_getline std::getline
70 #define STD_make_pair std::make_pair
71 
72 #include <iostream>
73 #include <fstream>
74 #include <sstream>
75 
76 #endif // STREAM_REPLACEMENT
77 
79 
80 
81 #ifdef STL_REPLACEMENT
82 
83 #define STD_list tjstd_list
84 #define STD_find tjstd_find
85 #define STD_pair tjstd_pair
86 #define STD_map tjstd_map
87 #define STD_vector tjstd_vector
88 #define STD_complex tjstd_complex
89 #define STD_back_inserter tjstd_back_inserter
90 #define STD_unique_copy tjstd_unique_copy
91 #define STD_swap tjstd_swap
92 #define STD_max tjstd_max
93 #define STD_min tjstd_min
94 
95 // complex math funcs
96 #define STD_abs tjstd_abs
97 #define STD_arg tjstd_arg
98 #define STD_log tjstd_log
99 #define STD_exp tjstd_exp
100 #define STD_conj tjstd_conj
101 
102 #else // STL_REPLACEMENT
103 
104 #include <list>
105 #include <utility>
106 #include <map>
107 #include <complex>
108 #include <cmath>
109 #include <new>
110 #include <vector>
111 #include <algorithm> // For Visual Studio 2005
112 
113 #define STD_list std::list
114 #define STD_find std::find
115 #define STD_pair std::pair
116 #define STD_map std::map
117 #define STD_vector std::vector
118 #define STD_complex std::complex<float>
119 #define STD_back_inserter std::back_inserter
120 #define STD_unique_copy std::unique_copy
121 #define STD_swap std::swap
122 #define STD_max std::max
123 #define STD_min std::min
124 
125 // complex math funcs
126 #define STD_abs std::abs
127 #define STD_arg std::arg
128 #define STD_log std::log
129 #define STD_exp std::exp
130 #define STD_conj std::conj
131 
132 #endif // STL_REPLACEMENT
133 
135 
136 
137 #ifdef STRING_REPLACEMENT
138 
139 #define STD_string tjstd_string
140 
141 #else // STRING_REPLACEMENT
142 
143 #include <string>
144 #define STD_string std::string
145 
146 #endif // STRING_REPLACEMENT
147 
148 
149 
150 
157 
158 #ifdef STL_REPLACEMENT
159 
160 
161 template<class I>
162 class tjnode {
163 
164  public:
165  tjnode() : value(0), next(0), prev(0) {}
166  tjnode (const I& newval) : value(new I(newval)), next(0), prev(0) {}
167  ~tjnode() {if(value) delete value;}
168  I* value;
169  tjnode<I>* next;
170  tjnode<I>* prev;
171 
172  private:
173  // make these member functions private to prevent their usage
174  tjnode(const tjnode&) {}
175  tjnode& operator = (const tjnode&) {return *this;}
176 
177 };
178 
180 
181 
182 template<class I>
183 class tjiterator {
184 
185  public:
186  tjiterator(tjnode<I>* itemptr=0) : current(itemptr) {}
187 
188  operator I* () {
189  if(current) return (current->value);
190  else return 0;
191  }
192 
193  operator const I* () const {
194  if(current) return (current->value);
195  else return 0;
196  }
197 
198 
199  I& operator * () {return *(operator I* ());}
200  const I& operator * () const {return *(operator const I* ());}
201 
202  I* operator -> () {return operator I* ();}
203  const I* operator -> () const {return operator const I* ();}
204 
205 
206  tjiterator<I> operator ++ () {
207  if(current && current->next) current=current->next;
208  return *this;
209  }
210 
211  tjiterator<I> operator ++ (int) { // postfix
212  tjnode<I>* old=current;
213  if(current && current->next) current=current->next;
214  return tjiterator<I>(old);
215  }
216 
217  tjiterator<I> operator -- () {
218  if(current && current->prev) current=current->prev;
219  return *this;
220  }
221 
222  tjiterator<I> operator -- (int) { // postfix
223  tjnode<I>* old=current;
224  if(current && current->prev) current=current->prev;
225  return tjiterator<I>(old);
226  }
227 
228 
229  bool operator == (const tjiterator<I>& i2) const {
230  return (current==i2.current);
231  }
232 
233  bool operator != (const tjiterator<I>& i2) const {
234  return (current!=i2.current);
235  }
236 
237  tjnode<I>* current;
238 };
239 
241 
242 
243 template<class I>
244 class tjstd_list {
245 
246  public:
247 
248  typedef tjiterator<I> iterator;
249  typedef tjiterator<I> const_iterator;
250 
251  tjstd_list() : first(&end_node), size_cache(0) {}
252 
253  tjstd_list(unsigned int n, const I& value) : first(&end_node), size_cache(0) {
254  for(unsigned int i=0; i<n; i++) push_back(value);
255  }
256 
257  tjstd_list(const tjstd_list& l) : first(&end_node), size_cache(0) {tjstd_list::operator = (l);}
258 
259  ~tjstd_list() {clear();}
260 
261  tjstd_list& operator = (const tjstd_list& l) {
262  check("operator = (pre)");
263  clear();
264  for(const_iterator it=l.begin(); it!=l.end(); ++it) push_back(*it);
265  check("operator = (post)");
266  return *this;
267  }
268 
269 
270  tjiterator<I> begin() {return tjiterator<I>(first);}
271  tjiterator<I> end() {return tjiterator<I>(&end_node);}
272 
273  tjiterator<I> begin() const {return tjiterator<I>(first);}
274  tjiterator<I> end() const {return tjiterator<I>(&end_node);}
275 
276 
277  tjiterator<I> push_back(const I& newval) {
278  check("push_back(pre)");
279  tjnode<I>* newnode=new tjnode<I>(newval);
280  push_back_node(newnode);
281  check("push_back(post)");
282  return tjiterator<I>(newnode);
283  }
284 
285 
286  tjiterator<I> push_front(const I& newval) {
287  check("push_front(pre)");
288  tjnode<I>* newnode=new tjnode<I>(newval);
289  push_front_node(newnode);
290  check("push_front(post)");
291  return tjiterator<I>(newnode);
292  }
293 
294 
295 
296  tjiterator<I> insert(const tjiterator<I>& pos, const I& newval) {
297  check("insert(pre)");
298  tjnode<I>* newnode=new tjnode<I>(newval);
299  insert_node(pos.current, newnode);
300  return tjiterator<I>(newnode);
301  }
302 
303 
304  void remove(const I& remval) {
305  check("remove(pre)");
306  I value2Bremoved(remval); //create copy in the case it is an element of the list itself
307  iterator it=begin();
308  while(it!=end()) {
309  if((*it)==value2Bremoved) {
310  tjnode<I>* toberemoved=it.current;
311  ++it;
312  remove_node(toberemoved);
313  delete toberemoved;
314  } else ++it;
315  }
316  check("remove(post)");
317  }
318 
319 
320  tjiterator<I> erase(const tjiterator<I>& b,const tjiterator<I>& e) {
321  check("erase2(pre)");
322  iterator it=b;
323  while(it!=e) {
324  tjnode<I>* toberemoved=it.current;
325  it.current=toberemoved->next; // iterate to next node
326  remove_node(toberemoved);
327  delete toberemoved;
328  }
329  check("erase2(post)");
330  return e;
331  }
332 
333 
334  tjiterator<I> erase(const tjiterator<I>& p) {
335  check("erase1(pre)");
336  tjiterator<I> result=p; result++;
337  tjnode<I>* toberemoved=p.current;
338  remove_node(toberemoved);
339  delete toberemoved;
340  check("erase1(post)");
341  return result;
342  }
343 
344 
345  void clear() {erase(begin(),end());}
346 
347 
348  unsigned int size() const {return size_cache;}
349  bool empty() const {return !size_cache;}
350 
351 
352 
353  void sort() {
354  check("sort(pre)");
355 
356  // make list_unsrt a shallow copy of this
357  tjstd_list<I> list_unsrt;
358  if(first!=(&end_node)) {
359  list_unsrt.first=first;
360  list_unsrt.end_node.prev=end_node.prev;
361  list_unsrt.end_node.prev->next=&(list_unsrt.end_node);
362  list_unsrt.size_cache=size_cache;
363  }
364 
365  // reset me
366  first=&end_node;
367  end_node.prev=0;
368  size_cache=0;
369 
370  while(list_unsrt.begin()!=list_unsrt.end()) {
371  tjnode<I>* node2sort=list_unsrt.first;
372  list_unsrt.remove_node(node2sort);
373  iterator it=begin();
374  while(it!=end() && (*it)<(*(node2sort->value))) ++it;
375  insert_node(it.current,node2sort);
376  }
377 
378  list_unsrt.check("sort::list_unsrt");
379  check("sort(post)");
380  }
381 
382 
383 
384  void unique() {
385  check("unique(pre)");
386  for(iterator it=begin(); it!=end(); ++it) {
387  while(it.current->next && it.current->next->value && (*(it.current->value))==(*(it.current->next->value)) ) {
388  tjnode<I>* toberemoved=it.current->next;
389  remove_node(toberemoved);
390  delete toberemoved;
391  }
392  }
393  check("unique(post)");
394  }
395 
396  private:
397 
398  void push_back_node(tjnode<I>* newnode) {
399  check("push_back_node(pre)");
400  newnode->next=0;
401  newnode->prev=0;
402  if(first==(&end_node)) {
403  first=end_node.prev=newnode;
404  newnode->next=&end_node;
405  } else {
406  end_node.prev->next=newnode;
407  newnode->prev=end_node.prev;
408  end_node.prev=newnode;
409  newnode->next=&end_node;
410  }
411  size_cache++;
412  check("push_back_node(post)");
413  }
414 
415 
416  void push_front_node(tjnode<I>* newnode) {
417  check("push_front_node(pre)");
418  newnode->next=0;
419  newnode->prev=0;
420  if(first==(&end_node)) {
421  first=end_node.prev=newnode;
422  newnode->next=&end_node;
423  } else {
424  first->prev=newnode;
425  newnode->next=first;
426  first=newnode;
427  }
428  size_cache++;
429  check("push_front_node(post)");
430  }
431 
432 
433  void remove_node(tjnode<I>* toberemoved) {
434  check("remove_node(pre)");
435  if(toberemoved->prev) toberemoved->prev->next=toberemoved->next;
436  if(toberemoved->next) toberemoved->next->prev=toberemoved->prev;
437  if(toberemoved==first) first=toberemoved->next;
438  if(toberemoved==end_node.prev) end_node.prev= toberemoved->prev;
439  toberemoved->next=0;
440  toberemoved->prev=0;
441  size_cache--;
442  check("remove_node(post)");
443  }
444 
445 
446  // Insert before posnode
447  void insert_node(tjnode<I>* posnode, tjnode<I>* newnode) {
448  check("insert_node(pre)");
449  newnode->next=0;
450  newnode->prev=0;
451  if(posnode==(&end_node)) {
452  push_back_node(newnode);
453  } else {
454  if(posnode==first) push_front_node(newnode);
455  else {
456  // in the following the order is important
457  posnode->prev->next=newnode;
458  newnode->prev=posnode->prev;
459  newnode->next=posnode;
460  posnode->prev=newnode;
461  size_cache++;
462  }
463  }
464  check("insert_node(post)");
465  }
466 
467 
468  void check(const char* /*caller*/) const {
469 #ifdef ODIN_DEBUG
470 /*
471  if(!first) STD_cerr << "ERROR: tjstd_list::" << caller << " !first " << STD_endl;
472 
473  int size=0;
474  for(const_iterator it=begin(); it!=end(); ++it) size++;
475  if(size!=size_cache) STD_cerr << "ERROR: tjstd_list::" << caller << " size(" << size << ") != size_cache(" << size_cache << ")" << STD_endl;
476 */
477 #endif
478  }
479 
480 
481  tjnode<I>* first;
482  mutable tjnode<I> end_node;
483  int size_cache;
484 };
485 
486 
487 template<class I>
488  tjiterator<I> tjstd_find(const tjiterator<I>& b,const tjiterator<I>& e, const I& findval) {
489  tjiterator<I> it=b;
490  while(it!=e) {
491  if((*it)==findval) return it;
492  ++it;
493  }
494  return e;
495  }
496 
497 
499 
500 template<class K, class V>
501 class tjstd_pair {
502 
503  public:
504  tjstd_pair() {}
505  tjstd_pair(const K& key, const V& value) : first(key), second(value) {}
506 
507  bool operator == (const tjstd_pair<K,V>& i2) const {
508  // do not use == operator of K, otherwise it has to be defined
509  if( (first<i2.first) || (i2.first<first) ) return false;
510  return true;
511  }
512 
513  bool operator < (const tjstd_pair<K,V>& i2) const {
514  return (first<i2.first);
515  }
516 
517  K first;
518  V second;
519 };
520 
521 
522 template <class K, class V>
523 tjstd_pair<K,V> tjstd_make_pair(const K& k, const V& v) {
524  return tjstd_pair<K,V>(k,v);
525 }
526 
527 
529 
530 
531 template<class K, class V> class tjstd_map : public tjstd_list< tjstd_pair<K,V> > {
532 
533  public:
534  V& operator [] (const K& key) {
535  typename tjstd_map<K,V>::iterator it=find(key);
536  if(it==tjstd_list< tjstd_pair<K,V> >::end()) {
537  it=push_back(tjstd_pair<K,V>(key,V()));
538  tjstd_list< tjstd_pair<K,V> >::sort();
539  }
540  return it->second;
541  }
542 
543  typename tjstd_map<K,V>::iterator find(const K& key) {
544  for(typename tjstd_map<K,V>::iterator it=tjstd_list< tjstd_pair<K,V> >::begin(); it!=tjstd_list< tjstd_pair<K,V> >::end(); ++it) {
545  if( !( (key<(it->first)) || ((it->first)<key) ) ) return it;
546  }
547  return tjstd_list< tjstd_pair<K,V> >::end();
548  }
549 
550  typename tjstd_map<K,V>::iterator find(const K& key) const {
551  for(typename tjstd_map<K,V>::const_iterator it=tjstd_list< tjstd_pair<K,V> >::begin(); it!=tjstd_list< tjstd_pair<K,V> >::end(); ++it) {
552  if( !( (key<(it->first)) || ((it->first)<key) ) ) return it;
553  }
554  return tjstd_list< tjstd_pair<K,V> >::end();
555  }
556 
557  void insert(const tjstd_pair<K,V>& pair) {
558  if(find(pair.first)==tjstd_list< tjstd_pair<K,V> >::end()) {
559  (*this)[pair.first]=pair.second;
560  }
561  }
562 
563  typename tjstd_map<K,V>::iterator insert(typename tjstd_map<K,V>::iterator, const tjstd_pair<K,V>& pair) {
564  if(find(pair.first)==tjstd_list< tjstd_pair<K,V> >::end()) {
565  (*this)[pair.first]=pair.second;
566  }
567  return find(pair.first);
568  }
569 
570 
571 };
572 
574 
575 // Extra type for vector size to avoid type-conversion problems in arithmetic expressions on VxWorks
576 // which occur if a simple 'unsigned int' is used in the constructor of tjstd_vector
577 struct tjstd_vector_size {
578  tjstd_vector_size(unsigned int size) : s(size) {}
579  unsigned int s;
580 };
581 
582 
583 
584 template<class E>
585 class tjstd_vector {
586 
587  public:
588  tjstd_vector() : data(0), nelements(0), cap(0) {}
589 
590  tjstd_vector(tjstd_vector_size size, const E& initval=E()) : data(0), nelements(0), cap(0) {
591  resize(size.s);
592  for(unsigned int i=0; i<nelements; i++) {
593  data[i]=initval;
594  }
595  }
596 
597  tjstd_vector(const tjstd_vector<E>& v) : data(0), nelements(0), cap(0) {
598  tjstd_vector<E>::operator = (v);
599  }
600 
601  ~tjstd_vector() {
602  if(data) delete[] data;
603  }
604 
605  tjstd_vector<E>& operator = (const tjstd_vector<E>& v) {
606  if(data) delete[] data;
607  nelements=cap=v.nelements;
608  data=new E[nelements];
609  for(unsigned int i=0; i<nelements; i++) data[i]=v.data[i];
610  return *this;
611  }
612 
613  E& operator [] (unsigned long i) {
614  if(i>=nelements) {
615 // STD_cerr << "ERROR: tjstd_vector: operator[" << i << "] exceeds size=" << nelements << STD_endl;
616  return retdummy;
617  }
618  else return data[i];
619  }
620 
621  const E& operator [] (unsigned long i) const {
622  if(i>=nelements) {
623 // STD_cerr << "ERROR: tjstd_vector: operator[" << i << "] exceeds size=" << nelements << STD_endl;
624  return retdummy;
625  }
626  else return data[i];
627  }
628 
629  void clear() { resize(0);}
630 
631  void resize(unsigned long newsize) {
632  if(newsize>cap) {
633  cap=newsize*2;
634  E* newdata=new E[cap];
635  if(data) {
636  for(unsigned int i=0; i<nelements; i++) newdata[i]=data[i];
637  delete[] data;
638  }
639  data=newdata;
640  }
641  else if(newsize<cap/2) {
642  cap=newsize;
643  E* newdata=new E[cap];
644  if(data){
645  for(unsigned int i=0; i<newsize; i++) newdata[i]=data[i];
646  delete[] data;
647  }
648  data=newdata;
649  }
650  nelements=newsize;
651  }
652 
653  void reserve(unsigned long newcap) {
654  if(newcap>cap) {
655  cap=newcap;
656  E* newdata=new E[cap];
657  if(data) {
658  for(unsigned int i=0; i<nelements; i++) newdata[i]=data[i];
659  delete[] data;
660  }
661  data=newdata;
662  }
663  }
664 
665  unsigned long size() const {return nelements;}
666  unsigned long capacity() const {return cap;}
667 
668  void push_back(const E& val) {
669  resize(nelements+1);
670  data[nelements-1]=val;
671  }
672 
673  private:
674  E* data;
675  unsigned long nelements;
676  unsigned long cap;
677  E retdummy;
678 
679 };
680 
681 
682 // comparison operators
683 
684 template<class E>
685 bool operator == (const tjstd_vector<E>& v1, const tjstd_vector<E>& v2) {
686  if(v1.size()!=v2.size()) return false;
687  bool result=true;
688  for(unsigned int i=0; i<v1.size(); i++) {
689  if(v1[i]!=v2[i]) {
690  result=false;
691  break;
692  }
693  }
694  return result;
695 }
696 
697 
698 template<class E>
699 bool operator != (const tjstd_vector<E>& v1, const tjstd_vector<E>& v2) {
700  return !(v1==v2);
701 }
702 
703 
704 template<class E>
705 bool operator < (const tjstd_vector<E>& v1, const tjstd_vector<E>& v2) {
706  unsigned int v1size=v1.size();
707  unsigned int v2size=v2.size();
708  if(v1size!=v2size) return (v1size<v2size);
709  for(unsigned int i=0; i<v1size; i++) {
710  if(v1[i]!=v2[i]) return (v1[i]<v2[i]);
711  }
712  return false;
713 }
714 
716 
717 
718 class tjstd_complex {
719 
720  float re, im;
721 
722 public:
723 
724  tjstd_complex (float real=0, float imaginary=0) {
725  re = real;
726  im = imaginary;
727  }
728 
729  float real() const { return re;}
730  float imag() const { return im;}
731 
732 
733  friend STD_ostream & operator << (STD_ostream & s, tjstd_complex c);
734 
735  bool operator == (tjstd_complex c) const {
736  if (c.re == re && c.im == im)
737  return (1);
738  else
739  return (0);
740  }
741 
742  bool operator != (tjstd_complex c) const {
743  if (c.re != re || c.im != im)
744  return (1);
745  else
746  return (0);
747  }
748 
749 
750  friend tjstd_complex operator + (tjstd_complex c) {
751  return (c);
752  }
753 
754 
755  friend tjstd_complex operator + (tjstd_complex c1, tjstd_complex c2) {
756  tjstd_complex sum;
757  sum.re = c1.re + c2.re;
758  sum.im = c1.im + c2.im;
759  return (sum);
760  }
761 
762  tjstd_complex operator += (tjstd_complex c) {
763  re += c.re;
764  im += c.im;
765  return (*this);
766  }
767 
768 
769 
770  friend tjstd_complex operator - (tjstd_complex c) {
771  c.re = -c.re;
772  c.im = -c.im;
773  return (c);
774  }
775 
776 
777 
778  friend tjstd_complex operator - (tjstd_complex c1, tjstd_complex c2) {
779  tjstd_complex diff;
780  diff.re = c1.re - c2.re;
781  diff.im = c1.im - c2.im;
782  return (diff);
783  }
784 
785  tjstd_complex operator -= (tjstd_complex c) {
786  re -= c.re;
787  im -= c.im;
788  return (*this);
789  }
790 
791 
792  friend tjstd_complex operator *(tjstd_complex c1, tjstd_complex c2) {
793  tjstd_complex prod;
794  prod.re = c1.re * c2.re - c1.im * c2.im;
795  prod.im = c1.re * c2.im + c1.im * c2.re;
796  return (prod);
797  }
798 
799  tjstd_complex operator *= (tjstd_complex c) {
800  operator = ((*this) * c);
801  return (*this);
802  }
803 
804  friend tjstd_complex operator / (tjstd_complex c1, tjstd_complex c2) {
805  tjstd_complex div;
806  float denominator = c2.re * c2.re + c2.im * c2.im;
807  if (denominator == 0.0) {
808  div.re = 0.0;
809  div.im = 0.0;
810  } else {
811  div.re = (c1.re * c2.re + c1.im * c2.im) / denominator;
812  div.im = (c1.im * c2.re - c1.re * c2.im) / denominator;
813  }
814  return (div);
815  }
816 
817  tjstd_complex operator /= (tjstd_complex c) {
818  operator = ((*this) / c);
819  return (*this);
820  }
821 
822  friend tjstd_complex tjstd_conj (tjstd_complex c) {
823  return tjstd_complex (c.re, -c.im);
824  }
825  friend tjstd_complex conj (tjstd_complex c) {return tjstd_conj(c);}
826 
827  friend float tjstd_abs (tjstd_complex c) {
828  return float(sqrt (c.re * c.re + c.im * c.im));
829  }
830  friend float abs (tjstd_complex c) {return tjstd_abs(c);}
831 
832  friend float tjstd_arg (tjstd_complex c) {
833  return float(atan2 (c.im, c.re));
834  }
835  friend float arg (tjstd_complex c) {return tjstd_arg(c);}
836 
837  friend tjstd_complex tjstd_log (tjstd_complex c) {
838  return (tjstd_complex ((float)log(abs(c)),arg(c)));
839  }
840  friend tjstd_complex log (tjstd_complex c) {return tjstd_log(c);}
841 
842  friend tjstd_complex tjstd_exp (tjstd_complex c) {
843  return ( tjstd_complex ( (float)exp(c.re)*cos(c.im), (float)exp(c.re)*sin(c.im) ) );
844  }
845  friend tjstd_complex exp (tjstd_complex c) {return tjstd_exp(c);}
846 
847 };
848 
849 
851 
852 template<typename T>
853 void tjstd_swap(T& a1, T& a2){
854  T temp=a1;
855  a1=a2;
856  a2=temp;
857 }
858 
859 
860 template<typename T>
861 T tjstd_max(const T& a1, const T& a2){
862  if(a1>a2) return a1;
863  else return a2;
864 }
865 
866 template<typename T>
867 T tjstd_min(const T& a1, const T& a2){
868  if(a1<a2) return a1;
869  else return a2;
870 }
871 
872 
873 #endif // ifdef STL_REPLACEMENT
874 
875 
876 
878 #ifdef STRING_REPLACEMENT
879 
880 
881 
882 class tjstd_string {
883 
884  public:
885  tjstd_string ();
886  tjstd_string (unsigned int n, const char c=' ');
887  tjstd_string (const char *charptr) : sdata(0) {tjstd_string::operator = (charptr);}
888  tjstd_string (const tjstd_string& str) : sdata(0) {tjstd_string::operator = (str);}
889 
890  tjstd_string& operator = (const tjstd_string& str);
891  tjstd_string& operator = (const char *charptr);
892 
893  ~tjstd_string();
894 
895  unsigned int size() const {return length_cache;}
896  unsigned int length() const {return size();}
897 
898  const char * c_str() const;
899 
900  char& operator [] (unsigned int i);
901  const char& operator [] (unsigned int i) const;
902 
903  friend tjstd_string operator + (const tjstd_string& s1, const tjstd_string& s2);
904  friend tjstd_string operator + (const tjstd_string& s1, const char* s2);
905  friend tjstd_string operator + (const char* s1, const tjstd_string& s2);
906 
907  tjstd_string& operator += (const tjstd_string& s2);
908 
909  friend bool operator == (const tjstd_string& s, const tjstd_string& t);
910  friend bool operator == (const tjstd_string& s, const char* t);
911  friend bool operator == (const char* s, const tjstd_string& t);
912 
913 
914  friend bool operator != (const tjstd_string& s, const tjstd_string& t);
915  friend bool operator != (const tjstd_string& s, const char* t);
916  friend bool operator != (const char* s, const tjstd_string& t);
917 
918  friend bool operator < (const tjstd_string& s, const tjstd_string& t);
919  friend bool operator > (const tjstd_string& s, const tjstd_string& t);
920 
921  friend STD_ostream& operator << (STD_ostream& s,const tjstd_string& t);
922  friend STD_istream& operator >> (STD_istream& s, tjstd_string& t);
923 
924  tjstd_string substr(unsigned int begin,unsigned int sublength) const;
925  void erase(unsigned int begin,unsigned int end);
926 
927  unsigned int find(const tjstd_string& searchstring, int startpos=0) const;
928 
929  typedef unsigned int size_type;
930 
931  static unsigned int npos;
932 
933  private:
934 
935  void concat2strings(const tjstd_string& srcstring1, const tjstd_string& srcstring2);
936  static bool compare2strings(const char* str1, const char* str2);
937 
938  char* sdata;
939  unsigned int length_cache;
940 
941  static char nullchar;
942 };
943 
944 #endif // STRING_REPLACEMENT
946 
949 #endif
bool operator!=(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
Definition: utils.h:139
bool operator==(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
Definition: utils.h:129
TinyVector< T, N_rows > operator*(const TinyMatrix< T, N_rows, N_columns > &matrix, const TinyVector< T, N_columns > &vector)
Definition: utils.h:149
bool operator>(const STD_complex &c1, const STD_complex &c2)
Definition: tjcomplex.h:66
bool operator<(const STD_complex &c1, const STD_complex &c2)
Definition: tjcomplex.h:61
fvector real(const cvector &cv)
fvector imag(const cvector &cv)