… | |
… | |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
|
|
11 | #include <cmath> |
11 | #include <new> |
12 | #include <new> |
12 | #include <vector> |
13 | #include <vector> |
13 | |
14 | |
14 | #include <glib.h> |
15 | #include <glib.h> |
15 | |
16 | |
… | |
… | |
31 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
32 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
32 | |
33 | |
33 | // in range excluding end |
34 | // in range excluding end |
34 | #define IN_RANGE_EXC(val,beg,end) \ |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
35 | ((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 | |
|
|
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; } |
|
|
45 | |
|
|
46 | // this is much faster than crossfires original algorithm |
|
|
47 | // on modern cpus |
|
|
48 | inline int |
|
|
49 | isqrt (int n) |
|
|
50 | { |
|
|
51 | return (int)sqrtf ((float)n); |
|
|
52 | } |
|
|
53 | |
|
|
54 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
55 | #if 0 |
|
|
56 | // and has a max. error of 6 in the range -100..+100. |
|
|
57 | #else |
|
|
58 | // and has a max. error of 9 in the range -100..+100. |
|
|
59 | #endif |
|
|
60 | inline int |
|
|
61 | idistance (int dx, int dy) |
|
|
62 | { |
|
|
63 | unsigned int dx_ = abs (dx); |
|
|
64 | unsigned int dy_ = abs (dy); |
|
|
65 | |
|
|
66 | #if 0 |
|
|
67 | return dx_ > dy_ |
|
|
68 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
69 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
70 | #else |
|
|
71 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
72 | #endif |
|
|
73 | } |
|
|
74 | |
|
|
75 | /* |
|
|
76 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
77 | * the "absolute" direction of a number (it actually takes care of |
|
|
78 | * "overflow" in previous calculations of a direction). |
|
|
79 | */ |
|
|
80 | inline int |
|
|
81 | absdir (int d) |
|
|
82 | { |
|
|
83 | return ((d - 1) & 7) + 1; |
|
|
84 | } |
36 | |
85 | |
37 | // makes dynamically allocated objects zero-initialised |
86 | // makes dynamically allocated objects zero-initialised |
38 | struct zero_initialised |
87 | struct zero_initialised |
39 | { |
88 | { |
40 | void *operator new (size_t s, void *p) |
89 | void *operator new (size_t s, void *p) |
… | |
… | |
139 | void destroy (pointer p) |
188 | void destroy (pointer p) |
140 | { |
189 | { |
141 | p->~Tp (); |
190 | p->~Tp (); |
142 | } |
191 | } |
143 | }; |
192 | }; |
|
|
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 | // generator |
|
|
200 | uint32_t state [4]; |
|
|
201 | |
|
|
202 | void operator =(const tausworthe_random_generator &src) |
|
|
203 | { |
|
|
204 | state [0] = src.state [0]; |
|
|
205 | state [1] = src.state [1]; |
|
|
206 | state [2] = src.state [2]; |
|
|
207 | state [3] = src.state [3]; |
|
|
208 | } |
|
|
209 | |
|
|
210 | void seed (uint32_t seed); |
|
|
211 | uint32_t next (); |
|
|
212 | |
|
|
213 | // uniform distribution |
|
|
214 | uint32_t operator ()(uint32_t r_max) |
|
|
215 | { |
|
|
216 | return is_constant (r_max) |
|
|
217 | ? this->next () % r_max |
|
|
218 | : get_range (r_max); |
|
|
219 | } |
|
|
220 | |
|
|
221 | // return a number within (min .. max) |
|
|
222 | int operator () (int r_min, int r_max) |
|
|
223 | { |
|
|
224 | return is_constant (r_min) && is_constant (r_max) |
|
|
225 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
|
|
226 | : get_range (r_min, r_max); |
|
|
227 | } |
|
|
228 | |
|
|
229 | double operator ()() |
|
|
230 | { |
|
|
231 | return this->next () / (double)0xFFFFFFFFU; |
|
|
232 | } |
|
|
233 | |
|
|
234 | protected: |
|
|
235 | uint32_t get_range (uint32_t r_max); |
|
|
236 | int get_range (int r_min, int r_max); |
|
|
237 | }; |
|
|
238 | |
|
|
239 | typedef tausworthe_random_generator rand_gen; |
|
|
240 | |
|
|
241 | extern rand_gen rndm; |
144 | |
242 | |
145 | template<class T> |
243 | template<class T> |
146 | struct refptr |
244 | struct refptr |
147 | { |
245 | { |
148 | T *p; |
246 | T *p; |
… | |
… | |
266 | { |
364 | { |
267 | errase (&obj); |
365 | errase (&obj); |
268 | } |
366 | } |
269 | }; |
367 | }; |
270 | |
368 | |
271 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
272 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
273 | 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; } |
|
|
274 | |
|
|
275 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
276 | |
|
|
277 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
369 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
278 | void assign (char *dst, const char *src, int maxlen); |
370 | void assign (char *dst, const char *src, int maxlen); |
279 | |
371 | |
280 | // type-safe version of assign |
372 | // type-safe version of assign |
281 | template<int N> |
373 | template<int N> |