23 #ifndef TJUTILS_CONFIG_H
24 #define TJUTILS_CONFIG_H
25 #include <tjutils/config.h>
28 #include <tjutils/tjheap.h>
29 #include <tjutils/tjcstd.h>
36 #ifdef STREAM_REPLACEMENT
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
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
81 #ifdef STL_REPLACEMENT
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
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
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
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
137 #ifdef STRING_REPLACEMENT
139 #define STD_string tjstd_string
144 #define STD_string std::string
158 #ifdef STL_REPLACEMENT
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;}
174 tjnode(
const tjnode&) {}
175 tjnode& operator = (
const tjnode&) {
return *
this;}
186 tjiterator(tjnode<I>* itemptr=0) : current(itemptr) {}
189 if(current)
return (current->value);
193 operator const I* ()
const {
194 if(current)
return (current->value);
200 const I&
operator * ()
const {
return *(
operator const I* ());}
202 I* operator -> () {
return operator I* ();}
203 const I* operator -> ()
const {
return operator const I* ();}
206 tjiterator<I> operator ++ () {
207 if(current && current->next) current=current->next;
211 tjiterator<I> operator ++ (
int) {
212 tjnode<I>* old=current;
213 if(current && current->next) current=current->next;
214 return tjiterator<I>(old);
217 tjiterator<I> operator -- () {
218 if(current && current->prev) current=current->prev;
222 tjiterator<I> operator -- (
int) {
223 tjnode<I>* old=current;
224 if(current && current->prev) current=current->prev;
225 return tjiterator<I>(old);
230 return (current==i2.current);
234 return (current!=i2.current);
248 typedef tjiterator<I> iterator;
249 typedef tjiterator<I> const_iterator;
251 tjstd_list() : first(&end_node), size_cache(0) {}
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);
257 tjstd_list(
const tjstd_list& l) : first(&end_node), size_cache(0) {tjstd_list::operator = (l);}
259 ~tjstd_list() {clear();}
261 tjstd_list& operator = (
const tjstd_list& l) {
262 check(
"operator = (pre)");
264 for(const_iterator it=l.begin(); it!=l.end(); ++it) push_back(*it);
265 check(
"operator = (post)");
270 tjiterator<I> begin() {
return tjiterator<I>(first);}
271 tjiterator<I> end() {
return tjiterator<I>(&end_node);}
273 tjiterator<I> begin()
const {
return tjiterator<I>(first);}
274 tjiterator<I> end()
const {
return tjiterator<I>(&end_node);}
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);
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);
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);
304 void remove(
const I& remval) {
305 check(
"remove(pre)");
306 I value2Bremoved(remval);
309 if((*it)==value2Bremoved) {
310 tjnode<I>* toberemoved=it.current;
312 remove_node(toberemoved);
316 check(
"remove(post)");
320 tjiterator<I> erase(
const tjiterator<I>& b,
const tjiterator<I>& e) {
321 check(
"erase2(pre)");
324 tjnode<I>* toberemoved=it.current;
325 it.current=toberemoved->next;
326 remove_node(toberemoved);
329 check(
"erase2(post)");
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);
340 check(
"erase1(post)");
345 void clear() {erase(begin(),end());}
348 unsigned int size()
const {
return size_cache;}
349 bool empty()
const {
return !size_cache;}
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;
370 while(list_unsrt.begin()!=list_unsrt.end()) {
371 tjnode<I>* node2sort=list_unsrt.first;
372 list_unsrt.remove_node(node2sort);
374 while(it!=end() && (*it)<(*(node2sort->value))) ++it;
375 insert_node(it.current,node2sort);
378 list_unsrt.check(
"sort::list_unsrt");
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);
393 check(
"unique(post)");
398 void push_back_node(tjnode<I>* newnode) {
399 check(
"push_back_node(pre)");
402 if(first==(&end_node)) {
403 first=end_node.prev=newnode;
404 newnode->next=&end_node;
406 end_node.prev->next=newnode;
407 newnode->prev=end_node.prev;
408 end_node.prev=newnode;
409 newnode->next=&end_node;
412 check(
"push_back_node(post)");
416 void push_front_node(tjnode<I>* newnode) {
417 check(
"push_front_node(pre)");
420 if(first==(&end_node)) {
421 first=end_node.prev=newnode;
422 newnode->next=&end_node;
429 check(
"push_front_node(post)");
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;
442 check(
"remove_node(post)");
447 void insert_node(tjnode<I>* posnode, tjnode<I>* newnode) {
448 check(
"insert_node(pre)");
451 if(posnode==(&end_node)) {
452 push_back_node(newnode);
454 if(posnode==first) push_front_node(newnode);
457 posnode->prev->next=newnode;
458 newnode->prev=posnode->prev;
459 newnode->next=posnode;
460 posnode->prev=newnode;
464 check(
"insert_node(post)");
468 void check(
const char* )
const {
482 mutable tjnode<I> end_node;
488 tjiterator<I> tjstd_find(
const tjiterator<I>& b,
const tjiterator<I>& e,
const I& findval) {
491 if((*it)==findval)
return it;
500 template<
class K,
class V>
505 tjstd_pair(
const K& key,
const V& value) : first(key), second(value) {}
507 bool operator == (
const tjstd_pair<K,V>& i2)
const {
509 if( (first<i2.first) || (i2.first<first) )
return false;
513 bool operator < (
const tjstd_pair<K,V>& i2)
const {
514 return (first<i2.first);
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);
531 template<
class K,
class V>
class tjstd_map :
public tjstd_list< tjstd_pair<K,V> > {
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();
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;
547 return tjstd_list< tjstd_pair<K,V> >::end();
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;
554 return tjstd_list< tjstd_pair<K,V> >::end();
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;
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;
567 return find(pair.first);
577 struct tjstd_vector_size {
578 tjstd_vector_size(
unsigned int size) : s(size) {}
588 tjstd_vector() : data(0), nelements(0), cap(0) {}
590 tjstd_vector(tjstd_vector_size size,
const E& initval=E()) : data(0), nelements(0), cap(0) {
592 for(
unsigned int i=0; i<nelements; i++) {
597 tjstd_vector(
const tjstd_vector<E>& v) : data(0), nelements(0), cap(0) {
598 tjstd_vector<E>::operator = (v);
602 if(data)
delete[] data;
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];
613 E& operator [] (
unsigned long i) {
621 const E& operator [] (
unsigned long i)
const {
629 void clear() { resize(0);}
631 void resize(
unsigned long newsize) {
634 E* newdata=
new E[cap];
636 for(
unsigned int i=0; i<nelements; i++) newdata[i]=data[i];
641 else if(newsize<cap/2) {
643 E* newdata=
new E[cap];
645 for(
unsigned int i=0; i<newsize; i++) newdata[i]=data[i];
653 void reserve(
unsigned long newcap) {
656 E* newdata=
new E[cap];
658 for(
unsigned int i=0; i<nelements; i++) newdata[i]=data[i];
665 unsigned long size()
const {
return nelements;}
666 unsigned long capacity()
const {
return cap;}
668 void push_back(
const E& val) {
670 data[nelements-1]=val;
675 unsigned long nelements;
685 bool operator == (
const tjstd_vector<E>& v1,
const tjstd_vector<E>& v2) {
686 if(v1.size()!=v2.size())
return false;
688 for(
unsigned int i=0; i<v1.size(); i++) {
699 bool operator != (
const tjstd_vector<E>& v1,
const tjstd_vector<E>& v2) {
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]);
718 class tjstd_complex {
724 tjstd_complex (
float real=0,
float imaginary=0) {
729 float real()
const {
return re;}
730 float imag()
const {
return im;}
733 friend STD_ostream & operator << (STD_ostream & s, tjstd_complex c);
736 if (c.re == re && c.im == im)
743 if (c.re != re || c.im != im)
750 friend tjstd_complex operator + (tjstd_complex c) {
755 friend tjstd_complex operator + (tjstd_complex c1, tjstd_complex c2) {
757 sum.re = c1.re + c2.re;
758 sum.im = c1.im + c2.im;
762 tjstd_complex operator += (tjstd_complex c) {
770 friend tjstd_complex operator - (tjstd_complex c) {
778 friend tjstd_complex operator - (tjstd_complex c1, tjstd_complex c2) {
780 diff.re = c1.re - c2.re;
781 diff.im = c1.im - c2.im;
785 tjstd_complex operator -= (tjstd_complex c) {
792 friend tjstd_complex
operator *(tjstd_complex c1, tjstd_complex c2) {
794 prod.re = c1.re * c2.re - c1.im * c2.im;
795 prod.im = c1.re * c2.im + c1.im * c2.re;
799 tjstd_complex operator *= (tjstd_complex c) {
800 operator = ((*
this) * c);
804 friend tjstd_complex operator / (tjstd_complex c1, tjstd_complex c2) {
806 float denominator = c2.re * c2.re + c2.im * c2.im;
807 if (denominator == 0.0) {
811 div.re = (c1.re * c2.re + c1.im * c2.im) / denominator;
812 div.im = (c1.im * c2.re - c1.re * c2.im) / denominator;
817 tjstd_complex operator /= (tjstd_complex c) {
818 operator = ((*
this) / c);
822 friend tjstd_complex tjstd_conj (tjstd_complex c) {
823 return tjstd_complex (c.re, -c.im);
825 friend tjstd_complex conj (tjstd_complex c) {
return tjstd_conj(c);}
827 friend float tjstd_abs (tjstd_complex c) {
828 return float(sqrt (c.re * c.re + c.im * c.im));
830 friend float abs (tjstd_complex c) {
return tjstd_abs(c);}
832 friend float tjstd_arg (tjstd_complex c) {
833 return float(atan2 (c.im, c.re));
835 friend float arg (tjstd_complex c) {
return tjstd_arg(c);}
837 friend tjstd_complex tjstd_log (tjstd_complex c) {
838 return (tjstd_complex ((
float)log(abs(c)),arg(c)));
840 friend tjstd_complex log (tjstd_complex c) {
return tjstd_log(c);}
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) ) );
845 friend tjstd_complex exp (tjstd_complex c) {
return tjstd_exp(c);}
853 void tjstd_swap(T& a1, T& a2){
861 T tjstd_max(
const T& a1,
const T& a2){
867 T tjstd_min(
const T& a1,
const T& a2){
878 #ifdef STRING_REPLACEMENT
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);}
890 tjstd_string& operator = (
const tjstd_string& str);
891 tjstd_string& operator = (
const char *charptr);
895 unsigned int size()
const {
return length_cache;}
896 unsigned int length()
const {
return size();}
898 const char * c_str()
const;
900 char& operator [] (
unsigned int i);
901 const char& operator [] (
unsigned int i)
const;
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);
907 tjstd_string& operator += (
const tjstd_string& s2);
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);
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);
918 friend bool operator < (
const tjstd_string& s,
const tjstd_string& t);
919 friend bool operator > (
const tjstd_string& s,
const tjstd_string& t);
921 friend STD_ostream& operator << (STD_ostream& s,
const tjstd_string& t);
922 friend STD_istream& operator >> (STD_istream& s, tjstd_string& t);
924 tjstd_string substr(
unsigned int begin,
unsigned int sublength)
const;
925 void erase(
unsigned int begin,
unsigned int end);
927 unsigned int find(
const tjstd_string& searchstring,
int startpos=0)
const;
929 typedef unsigned int size_type;
931 static unsigned int npos;
935 void concat2strings(
const tjstd_string& srcstring1,
const tjstd_string& srcstring2);
936 static bool compare2strings(
const char* str1,
const char* str2);
939 unsigned int length_cache;
941 static char nullchar;
bool operator!=(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
bool operator==(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
TinyVector< T, N_rows > operator*(const TinyMatrix< T, N_rows, N_columns > &matrix, const TinyVector< T, N_columns > &vector)
bool operator>(const STD_complex &c1, const STD_complex &c2)
bool operator<(const STD_complex &c1, const STD_complex &c2)
fvector real(const cvector &cv)
fvector imag(const cvector &cv)