1 | |
---|
2 | #ifndef NTL_tools__H |
---|
3 | #define NTL_tools__H |
---|
4 | |
---|
5 | #include <NTL/ctools.h> |
---|
6 | |
---|
7 | #if (defined(NTL_STD_CXX) || defined(NTL_PSTD_NHF)) |
---|
8 | |
---|
9 | // new header files |
---|
10 | |
---|
11 | #include <cstdlib> |
---|
12 | #include <cmath> |
---|
13 | #ifndef NOSTREAMIO |
---|
14 | #include <iostream> |
---|
15 | #endif |
---|
16 | |
---|
17 | #else |
---|
18 | |
---|
19 | // old header files |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <math.h> |
---|
23 | #ifndef NOSTREAMIO |
---|
24 | #include <iostream.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #endif |
---|
28 | |
---|
29 | #if (defined(NTL_STD_CXX) || defined(NTL_PSTD_NHF)) |
---|
30 | |
---|
31 | #define NTL_SNS std :: |
---|
32 | #define NTL_USE_SNS using namespace std; |
---|
33 | |
---|
34 | #elif (defined(NTL_PSTD_NNS)) |
---|
35 | |
---|
36 | #define NTL_SNS :: |
---|
37 | #define NTL_USE_SNS |
---|
38 | |
---|
39 | #else |
---|
40 | |
---|
41 | #define NTL_SNS |
---|
42 | #define NTL_USE_SNS |
---|
43 | |
---|
44 | #endif |
---|
45 | |
---|
46 | #if (defined(NTL_STD_CXX) || defined(NTL_PSTD_NNS)) |
---|
47 | |
---|
48 | #define NTL_NAMESPACE NTL |
---|
49 | #define NTL_OPEN_NNS namespace NTL_NAMESPACE { |
---|
50 | #define NTL_CLOSE_NNS } |
---|
51 | #define NTL_USE_NNS using namespace NTL_NAMESPACE; |
---|
52 | #define NTL_NNS NTL_NAMESPACE :: |
---|
53 | |
---|
54 | // To make things work, we have to apply using declarations of all std |
---|
55 | // functions that are both overloaded by NTL and are used in |
---|
56 | // the implementation of NTL. |
---|
57 | |
---|
58 | #define NTL_START_IMPL NTL_USE_SNS NTL_OPEN_NNS \ |
---|
59 | using NTL_SNS abs; \ |
---|
60 | using NTL_SNS ceil; \ |
---|
61 | using NTL_SNS exp; \ |
---|
62 | using NTL_SNS fabs; \ |
---|
63 | using NTL_SNS floor; \ |
---|
64 | using NTL_SNS ldexp; \ |
---|
65 | using NTL_SNS log; \ |
---|
66 | using NTL_SNS sqrt; |
---|
67 | |
---|
68 | #define NTL_END_IMPL NTL_CLOSE_NNS |
---|
69 | |
---|
70 | #else |
---|
71 | |
---|
72 | #define NTL_NAMESPACE |
---|
73 | #define NTL_OPEN_NNS |
---|
74 | #define NTL_CLOSE_NNS |
---|
75 | #define NTL_USE_NNS |
---|
76 | #define NTL_NNS |
---|
77 | |
---|
78 | #define NTL_START_IMPL |
---|
79 | #define NTL_END_IMPL |
---|
80 | |
---|
81 | #endif |
---|
82 | |
---|
83 | #define NTL_CLIENT NTL_USE_SNS NTL_USE_NNS |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | #if 0 |
---|
88 | |
---|
89 | // This is for debugging purposes only. |
---|
90 | |
---|
91 | namespace foo_bar { |
---|
92 | |
---|
93 | class ostream; |
---|
94 | class istream; |
---|
95 | |
---|
96 | typedef unsigned int size_t; |
---|
97 | |
---|
98 | double floor(double); |
---|
99 | float floor(float); |
---|
100 | |
---|
101 | } |
---|
102 | #endif |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | #if (!defined(NTL_CXX_ONLY)) |
---|
107 | extern "C" |
---|
108 | #endif |
---|
109 | double _ntl_GetTime(); |
---|
110 | |
---|
111 | typedef unsigned long _ntl_ulong; |
---|
112 | typedef _ntl_ulong *_ntl_ulong_ptr; |
---|
113 | // I made these have "obscure" names to avoid conflict with |
---|
114 | // (non-standard but common) definitions in standard headers. |
---|
115 | // Putting u_long inside namespace NTL only tends to creates ambiguities, |
---|
116 | // for no good reason. |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | NTL_OPEN_NNS |
---|
121 | |
---|
122 | struct INIT_SIZE_STRUCT { }; |
---|
123 | const INIT_SIZE_STRUCT INIT_SIZE = INIT_SIZE_STRUCT(); |
---|
124 | typedef const INIT_SIZE_STRUCT& INIT_SIZE_TYPE; |
---|
125 | |
---|
126 | struct INIT_VAL_STRUCT { }; |
---|
127 | const INIT_VAL_STRUCT INIT_VAL = INIT_VAL_STRUCT(); |
---|
128 | typedef const INIT_VAL_STRUCT& INIT_VAL_TYPE; |
---|
129 | |
---|
130 | struct INIT_TRANS_STRUCT { }; |
---|
131 | const INIT_TRANS_STRUCT INIT_TRANS = INIT_TRANS_STRUCT(); |
---|
132 | typedef const INIT_TRANS_STRUCT& INIT_TRANS_TYPE; |
---|
133 | |
---|
134 | |
---|
135 | struct INIT_LOOP_HOLE_STRUCT { }; |
---|
136 | const INIT_LOOP_HOLE_STRUCT INIT_LOOP_HOLE = INIT_LOOP_HOLE_STRUCT(); |
---|
137 | typedef const INIT_LOOP_HOLE_STRUCT& INIT_LOOP_HOLE_TYPE; |
---|
138 | |
---|
139 | struct INIT_FFT_STRUCT { }; |
---|
140 | const INIT_FFT_STRUCT INIT_FFT = INIT_FFT_STRUCT(); |
---|
141 | typedef const INIT_FFT_STRUCT& INIT_FFT_TYPE; |
---|
142 | |
---|
143 | |
---|
144 | #ifdef NTL_NO_INIT_TRANS |
---|
145 | #define NTL_OPT_RETURN(t, x) return x |
---|
146 | #else |
---|
147 | #define NTL_OPT_RETURN(t, x) return t(x, INIT_TRANS) |
---|
148 | #endif |
---|
149 | |
---|
150 | |
---|
151 | #ifndef NTL_NO_MIN_MAX |
---|
152 | |
---|
153 | inline int min(int a, int b) { return (a < b) ? a : b; } |
---|
154 | inline int max(int a, int b) { return (a < b) ? b : a; } |
---|
155 | |
---|
156 | inline long min(long a, long b) { return (a < b) ? a : b; } |
---|
157 | inline long max(long a, long b) { return (a < b) ? b : a; } |
---|
158 | |
---|
159 | inline long min(int a, long b) { return (a < b) ? long(a) : b; } |
---|
160 | inline long max(int a, long b) { return (a < b) ? b : long(a); } |
---|
161 | |
---|
162 | inline long min(long a, int b) { return (a < b) ? a : long(b); } |
---|
163 | inline long max(long a, int b) { return (a < b) ? long(b) : a; } |
---|
164 | |
---|
165 | #endif |
---|
166 | |
---|
167 | |
---|
168 | inline void swap(long& a, long& b) { long t; t = a; a = b; b = t; } |
---|
169 | inline void swap(int& a, int& b) { int t; t = a; a = b; b = t; } |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | inline void conv(int& x, int a) { x = a; } |
---|
174 | inline void conv(int& x, long a) |
---|
175 | { unsigned y = (unsigned) a; x = NTL_UINT_TO_INT(y); } |
---|
176 | inline void conv(int& x, float a) { x = int(NTL_SNS floor(double(a))); } |
---|
177 | inline void conv(int& x, double a) { x = int(NTL_SNS floor(a)); } |
---|
178 | |
---|
179 | inline void conv(int& x, unsigned a) |
---|
180 | { x = NTL_UINT_TO_INT(a); } |
---|
181 | |
---|
182 | inline void conv(int& x, unsigned long a) |
---|
183 | { unsigned y = (unsigned) a; x = NTL_UINT_TO_INT(y); } |
---|
184 | |
---|
185 | inline int to_int(int a) { return a; } |
---|
186 | inline int to_int(long a) |
---|
187 | { unsigned y = (unsigned) a; return NTL_UINT_TO_INT(y); } |
---|
188 | inline int to_int(float a) { return int(NTL_SNS floor(double(a))); } |
---|
189 | inline int to_int(double a) { return int(NTL_SNS floor(a)); } |
---|
190 | |
---|
191 | inline int to_int(unsigned a) |
---|
192 | { return NTL_UINT_TO_INT(a); } |
---|
193 | |
---|
194 | inline int to_int(unsigned long a) |
---|
195 | { unsigned y = (unsigned) a; return NTL_UINT_TO_INT(y); } |
---|
196 | |
---|
197 | |
---|
198 | inline void conv(long& x, int a) { x = a; } |
---|
199 | inline void conv(long& x, long a) { x = a; } |
---|
200 | inline void conv(long& x, float a) { x = long(NTL_SNS floor(double(a))); } |
---|
201 | inline void conv(long& x, double a) { x = long(NTL_SNS floor(a)); } |
---|
202 | |
---|
203 | inline void conv(long& x, unsigned a) |
---|
204 | { unsigned long y = a; x = NTL_ULONG_TO_LONG(y); } |
---|
205 | |
---|
206 | inline void conv(long& x, unsigned long a) |
---|
207 | { x = NTL_ULONG_TO_LONG(a); } |
---|
208 | |
---|
209 | inline long to_long(int a) { return a; } |
---|
210 | inline long to_long(long a) { return a; } |
---|
211 | inline long to_long(float a) { return long(NTL_SNS floor(double(a))); } |
---|
212 | inline long to_long(double a) { return long(NTL_SNS floor(a)); } |
---|
213 | |
---|
214 | inline long to_long(unsigned a) |
---|
215 | { unsigned long y = a; return NTL_ULONG_TO_LONG(y); } |
---|
216 | |
---|
217 | inline long to_long(unsigned long a) |
---|
218 | { return NTL_ULONG_TO_LONG(a); } |
---|
219 | |
---|
220 | inline void conv(float& x, int a) { x = float(a); } |
---|
221 | inline void conv(float& x, long a) { x = float(a); } |
---|
222 | inline void conv(float& x, unsigned a) { x = float(a); } |
---|
223 | inline void conv(float& x, unsigned long a) { x = float(a); } |
---|
224 | inline void conv(float& x, float a) { x = a; } |
---|
225 | inline void conv(float& x, double a) { x = float(a); } |
---|
226 | |
---|
227 | inline float to_float(int a) { return float(a); } |
---|
228 | inline float to_float(long a) { return float(a); } |
---|
229 | inline float to_float(unsigned a) { return float(a); } |
---|
230 | inline float to_float(unsigned long a) { return float(a); } |
---|
231 | inline float to_float(float a) { return a; } |
---|
232 | inline float to_float(double a) { return float(a); } |
---|
233 | |
---|
234 | inline void conv(double& x, int a) { x = double(a); } |
---|
235 | inline void conv(double& x, long a) { x = double(a); } |
---|
236 | inline void conv(double& x, unsigned a) { x = double(a); } |
---|
237 | inline void conv(double& x, unsigned long a) { x = double(a); } |
---|
238 | inline void conv(double& x, float a) { x = double(a); } |
---|
239 | inline void conv(double& x, double a) { x = a; } |
---|
240 | |
---|
241 | inline double to_double(int a) { return double(a); } |
---|
242 | inline double to_double(long a) { return double(a); } |
---|
243 | inline double to_double(unsigned a) { return double(a); } |
---|
244 | inline double to_double(unsigned long a) { return double(a); } |
---|
245 | inline double to_double(float a) { return double(a); } |
---|
246 | inline double to_double(double a) { return a; } |
---|
247 | |
---|
248 | #ifndef NOSTREAMIO |
---|
249 | long SkipWhiteSpace(NTL_SNS istream& s); |
---|
250 | #endif |
---|
251 | long IsWhiteSpace(long c); |
---|
252 | |
---|
253 | long CharToIntVal(long c); |
---|
254 | char IntValToChar(long a); |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | void Error(const char *s); |
---|
259 | |
---|
260 | |
---|
261 | inline double GetTime() { return _ntl_GetTime(); } |
---|
262 | |
---|
263 | inline long IsFinite(double *p) { return _ntl_IsFinite(p); } |
---|
264 | inline void ForceToMem(double *p) { _ntl_ForceToMem(p); } |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | void PrintTime(double t); |
---|
269 | |
---|
270 | NTL_CLOSE_NNS |
---|
271 | |
---|
272 | |
---|
273 | #endif |
---|
274 | |
---|