Merge branch 'master' of ssh://repo.or.cz/srv/git/quplot
[quplot.git] / qp3.h
blob44fe1699a84d323a1c316d80fa15cd16fc857d09
1 #ifndef QP3_H
2 #define QP3_H
4 // #define _USESTDVECTOR_
5 #define _CHECKBOUNDS_
6 //#define _QPENRERRORCLASS_ 1
7 //#define _TURNONFPES_ 1
9 // all the system #include's we'll ever need
10 #include <fstream>
11 #include <cmath>
12 #include <complex>
13 #include <iostream>
14 #include <iomanip>
15 #include <vector>
16 #include <limits>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <time.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <ctype.h>
24 using namespace std;
25 const double PI = M_PI;
26 // macro-like inline functions
28 template<class T>
29 inline T SQR(const T a) {return a*a;}
31 template<class T>
32 inline T ABS(const T a) {return a < 0 ? (a+2*PI) : a;}
34 template<class T>
35 inline T SIGN(const T &a, const T &b)
36 {return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);}
38 inline float SIGN(const float &a, const double &b)
39 {return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);}
41 inline float SIGN(const double &a, const float &b)
42 {return (float)(b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a));}
44 // exception handling
46 #ifndef _USEQPERRORCLASS_
47 #define throw(message) \
48 {printf("ERROR: %s\n in file %s at line %d\n", message,__FILE__,__LINE__); throw(1);}
49 #else
50 struct QPerror {
51 char *message;
52 char *file;
53 int line;
54 QPerror(char *m, char *f, int l) : message(m), file(f), line(l) {}
56 #define throw(message) throw(QPerror(message,__FILE__,__LINE__));
57 void QPcatch(QPerror err) {
58 printf("ERROR: %s\n in file %s at line %d\n",
59 err.message, err.file, err.line);
60 exit(1);
62 #endif
64 #ifdef _USESTDVECTOR_
65 #define QPvector vector
66 #else
68 template<class T>
69 class QPvector {
70 private:
71 int nn; // size of array. upper index is nn-1
72 T *v;
73 public:
74 QPvector();
75 explicit QPvector(int n); // Zero-based array;
76 QPvector(int n, const T &a); //initialize to constant value
77 QPvector(int n, const T *a); // Initialize to array
78 QPvector(const QPvector &rhs); // Copy constructor
79 QPvector & operator=(const QPvector &rhs); //assignment
80 typedef T value_type; // make T available externally
81 inline T & operator[](const int i); //i'th element
82 inline const T & operator[](const int i) const;
83 inline int size() const;
84 void resize(int newn); // resize (contents not preserved)
85 void assign(int newn, const T &a); // resize and assign a constant value
86 ~QPvector();
89 // QPvector definitions
91 template <class T>
92 QPvector<T>::QPvector() : nn(0), v(NULL) {}
94 template <class T>
95 QPvector<T>::QPvector(int n) : nn(n), v(n>0 ? new T[n] : NULL) {}
97 template <class T>
98 QPvector<T>::QPvector(int n, const T& a) : nn(n), v(n>0 ? new T[n] : NULL)
100 for(int i=0; i<n; i++) v[i] = a;
103 template <class T>
104 QPvector<T>::QPvector(int n, const T *a) : nn(n), v(n>0 ? new T[n] : NULL)
106 for(int i=0; i<n; i++) v[i] = *a++;
109 template <class T>
110 QPvector<T>::QPvector(const QPvector<T> &rhs) : nn(rhs.nn), v(nn>0 ? new T[nn] : NULL)
112 for (int i=0; i<nn; i++) v[i] = rhs[i];
115 template <class T>
116 QPvector<T> & QPvector<T>::operator=(const QPvector<T> &rhs)
117 // postcondition: normal assignment via copying has been performed;
118 // if vector and rhs were different sizes, vector
119 // has been resized to match the size of rhs
121 if (this != &rhs)
123 if (nn != rhs.nn) {
124 if (v != NULL) delete [] (v);
125 nn=rhs.nn;
126 v= nn>0 ? new T[nn] : NULL;
128 for (int i=0; i<nn; i++)
129 v[i]=rhs[i];
131 return *this;
134 template <class T>
135 inline T & QPvector<T>::operator[](const int i) //subscripting
137 #ifdef _CHECKBOUNDS_
138 if (i<0 || i>=nn) {
139 throw("QPvector subscript out of bounds");
141 #endif
142 return v[i];
145 template <class T>
146 inline const T & QPvector<T>::operator[](const int i) const //subscripting
148 #ifdef _CHECKBOUNDS_
149 if (i<0 || i >=nn) {
150 throw("QPvector subscript 2 out of bounds");
152 #endif
153 return v[i];
156 template <class T>
157 inline int QPvector<T>::size() const
159 return nn;
162 template <class T>
163 void QPvector<T>::resize(int newn)
165 if (newn != nn) {
166 if (v != NULL) delete[] (v);
167 nn = newn;
168 v = nn > 0 ? new T[nn] : NULL;
172 template <class T>
173 void QPvector<T>::assign(int newn, const T& a)
175 if (newn != nn) {
176 if (v != NULL) delete[] (v);
177 nn = newn;
178 v = nn > 0 ? new T[nn] : NULL;
180 for (int i=0;i<nn;i++) v[i] = a;
183 template <class T>
184 QPvector<T>::~QPvector()
186 if (v != NULL) delete[] (v);
189 // end of QPvector definitions
191 #endif //ifdef _USESTDVECTOR_
193 // basic type names
195 typedef int Int; // 32 bit integer
196 typedef unsigned int Uint;
198 typedef long long int Llong; // 64 bit integer
199 typedef unsigned long long int Ullong;
201 typedef double Doub; // default floating type
202 typedef long double Ldoub;
204 typedef bool Bool;
206 // vector types
208 typedef const QPvector<Int> VecInt_I;
209 typedef QPvector<Int> VecInt, VecInt_O, VecInt_IO;
211 typedef const QPvector<Doub> VecDoub_I;
212 typedef QPvector<Doub> VecDoub, VecDoub_O, VecDoub_IO;
214 #endif // QP3_H