4 // #define _USESTDVECTOR_
6 //#define _QPENRERRORCLASS_ 1
7 //#define _TURNONFPES_ 1
9 // all the system #include's we'll ever need
25 const double PI
= M_PI
;
26 // macro-like inline functions
29 inline T
SQR(const T a
) {return a
*a
;}
32 inline T
ABS(const T a
) {return a
< 0 ? (a
+2*PI
) : a
;}
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
));}
46 #ifndef _USEQPERRORCLASS_
47 #define throw(message) \
48 {printf("ERROR: %s\n in file %s at line %d\n", message,__FILE__,__LINE__); throw(1);}
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
);
65 #define QPvector vector
71 int nn
; // size of array. upper index is nn-1
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
89 // QPvector definitions
92 QPvector
<T
>::QPvector() : nn(0), v(NULL
) {}
95 QPvector
<T
>::QPvector(int n
) : nn(n
), v(n
>0 ? new T
[n
] : NULL
) {}
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
;
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
++;
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
];
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
124 if (v
!= NULL
) delete [] (v
);
126 v
= nn
>0 ? new T
[nn
] : NULL
;
128 for (int i
=0; i
<nn
; i
++)
135 inline T
& QPvector
<T
>::operator[](const int i
) //subscripting
139 throw("QPvector subscript out of bounds");
146 inline const T
& QPvector
<T
>::operator[](const int i
) const //subscripting
150 throw("QPvector subscript 2 out of bounds");
157 inline int QPvector
<T
>::size() const
163 void QPvector
<T
>::resize(int newn
)
166 if (v
!= NULL
) delete[] (v
);
168 v
= nn
> 0 ? new T
[nn
] : NULL
;
173 void QPvector
<T
>::assign(int newn
, const T
& a
)
176 if (v
!= NULL
) delete[] (v
);
178 v
= nn
> 0 ? new T
[nn
] : NULL
;
180 for (int i
=0;i
<nn
;i
++) v
[i
] = a
;
184 QPvector
<T
>::~QPvector()
186 if (v
!= NULL
) delete[] (v
);
189 // end of QPvector definitions
191 #endif //ifdef _USESTDVECTOR_
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
;
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
;