… | |
… | |
34 | // in range excluding end |
34 | // in range excluding end |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
37 | |
37 | |
38 | void fork_abort (const char *msg); |
38 | void fork_abort (const char *msg); |
|
|
39 | |
|
|
40 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
41 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
42 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
43 | |
|
|
44 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
39 | |
45 | |
40 | // this is much faster than crossfires original algorithm |
46 | // this is much faster than crossfires original algorithm |
41 | // on modern cpus |
47 | // on modern cpus |
42 | inline int |
48 | inline int |
43 | isqrt (int n) |
49 | isqrt (int n) |
… | |
… | |
183 | { |
189 | { |
184 | p->~Tp (); |
190 | p->~Tp (); |
185 | } |
191 | } |
186 | }; |
192 | }; |
187 | |
193 | |
|
|
194 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
195 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
196 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
197 | struct tausworthe_random_generator |
|
|
198 | { |
|
|
199 | uint32_t state [4]; |
|
|
200 | |
|
|
201 | tausworthe_random_generator (uint32_t seed); |
|
|
202 | uint32_t next (); |
|
|
203 | |
|
|
204 | uint32_t operator ()(uint32_t r_max) |
|
|
205 | { |
|
|
206 | return next () % r_max; |
|
|
207 | } |
|
|
208 | |
|
|
209 | // return a number within (min .. max) |
|
|
210 | int operator () (int r_min, int r_max) |
|
|
211 | { |
|
|
212 | return r_min + (*this) (max (r_max - r_min + 1, 1)); |
|
|
213 | } |
|
|
214 | |
|
|
215 | double operator ()() |
|
|
216 | { |
|
|
217 | return next () / (double)0xFFFFFFFFU; |
|
|
218 | } |
|
|
219 | }; |
|
|
220 | |
|
|
221 | typedef tausworthe_random_generator rand_gen; |
|
|
222 | |
|
|
223 | extern rand_gen rndm; |
|
|
224 | |
188 | template<class T> |
225 | template<class T> |
189 | struct refptr |
226 | struct refptr |
190 | { |
227 | { |
191 | T *p; |
228 | T *p; |
192 | |
229 | |
… | |
… | |
309 | { |
346 | { |
310 | errase (&obj); |
347 | errase (&obj); |
311 | } |
348 | } |
312 | }; |
349 | }; |
313 | |
350 | |
314 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
315 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
316 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
317 | |
|
|
318 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
319 | |
|
|
320 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
351 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
321 | void assign (char *dst, const char *src, int maxlen); |
352 | void assign (char *dst, const char *src, int maxlen); |
322 | |
353 | |
323 | // type-safe version of assign |
354 | // type-safe version of assign |
324 | template<int N> |
355 | template<int N> |